Lisa Lisa - 9 months ago 83
MySQL Question

Can't connect to MySQL server on 'localhost' (10055)

I am running three perl scripts sequentially. Each of them connects to a Mysql database multiple times, and either selects or updates the information contained in the database. After running these three scripts about 20~ times (& connecting/disconnecting from database many more times than this), I get the message 'Can't connect to MySQL server on 'localhost' (10055).

I have read elsewhere that I could get it to reconnect to the database using mysql_auto_reconnect=1, however I'm not entirely sure that that is the problem. The Mysql server on my computer is still connected when the program crashes - I don't have to restart it. I wanted to understand why it is doing this, and also I'm not entirely sure how I put in '"mysql_auto_reconnect=1", as when I do this it tells me DBI->connect using old style syntax is deprecated.

my $dbh_m= DBI->connect("dbi:mysql:XXX","root","XXX","mysql_auto_reconnect=1")

or die("Error at select Trans: $DBI::errstr");

Do I also need to write this in every time I connect to the database? (Putting that into one of the connections doesn't solve the problem, I still get error messages from all the others - it doesn't begin to run again and then re-crash later). Is this a problem with me connecting/disconnecting to the database to many times?

Thank you!


What error message do you get?

You should probably do something like:

my $dbh;
    local $@;
    do {
        eval { 
            $dbh = DBI->connect("dbi:mysql:XXX", "root", "XXX", { mysql_auto_reconnect => 1 })
        if ( $@ ) {
            warn "Error: $DBI::errstr\n";
            warn "Trying to reconnect in 5 sec.\n";
            sleep 5;
    } while ($@);

Just in case that the database is not responding or there are too many connections.