Thursday, August 22, 2013

mysql_auto_reconnect

I prefer PostgreSQL, which is the core of our business at pgExperts. But I still have some legacy clients on MySQL. For one of them, I'm writing new features in Perl/MySQL. The mysql_auto_reconnect option just saved my day. I'd been getting errors like "DBD::mysql::st execute failed: MySQL server has gone away." To use it in DBI, do
my $dbh = DBI->connect(
    "DBI:mysql:database=${MY_DATABASE};host=${MY_HOST};port=${MY_PORT}",
    $MY_USER,
    $MY_PASSWORD,
    { mysql_auto_reconnect => 1 },
);
To use it in Rose::DB::Object, do
File MyApp::DB;
our @ISA = qw( Rose::DB );

__PACKAGE__->default_connect_options({
    …,
    mysql_auto_reconnect => 1,
});