How do I exit after the error "MySQL server is gone"?

I have a cron job that runs every minute. From time to time, cron crashes with the error:

MySQL server is gone

The problem is that the entire query below this error is being executed and can sometimes give unwanted results.

So how do I detect that an error has occurred and terminate the rest of the code?

Or any hint of how to fix the bug in the first place would be great.

+2


a source to share


2 answers


It looks like the error is caused by connecting to a server that is not responding, or you are using a persistent connection that is disconnected.

You should be able to get some kind of error code from the client API and catch to make sure the connection is still valid.

If that didn't work, you can throw an exception and then stop the program.



$query_id = mysqli_query($conn_id, $query);
if(!$query_id )
{
    // throw an exception
    throw new DatabaseQueryException("An error occurred accessing the database.\nError: \n".mysqli_error($conn_id));
}

      

I am using on my site

+1


a source


Quick fix is ​​to disable persistent mysql connections. The best solution is to catch this particular error and call reconnect ( PDO::__construct

) until you get an instant live connection. I find I often get this error when the server computer goes to sleep or if the mysql connection timeout is low.



0


a source







All Articles