How to exit after "MySQL server has gone away" error? - php

I have a cron job that runs every minute. Every once in a while the cron fails with the error:
MySQL server has gone away
The problem is that all the query below this error are executed and may sometimes give undesirable result.
So how do I detect that the error has occurred and exit execution of rest of the code?
Or any hint on how to fix the error in the first place would be great.

The error seems to be caused by connecting to a server that isnt responding, or you are using a persistent connection that is timing out.
You should be able to get some kind of error code from the client API, and catch to see if the connection is still valid.
If it has failed, 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));
}
Is what I use on my site

A quick fix is to turn off persistent mysql connections. A better fix is to catch that specific error and call a reconnect (PDO::__construct) until you get an instant that has a live connection. I find I often get this error when the server computer goes to sleep or if the mysql connection timeout is to low.

Related

ssh2_connect(): Error starting up SSH connection(-43): Failed getting banner

Have a docker container build using php-ssh2. php version 7.2 When trying to use
$con = ssh2_connect('hostname');
I am getting Error starting up SSH connection(-43): Failed getting banner . Interesting thing is 43 here. Whats the significance of 43. What does that mean? Also any idea how to fix this? There is no heavy load, running connection manually.
Deepdive into libssh2
This number -43 is an error code that comes directly from libssh2, specifically LIBSSH2_ERROR_SOCKET_RECV. The Failed getting banner message is the dynamic error message that accompanies the error code. These two pieces of information give the location where this error is thrown, namely in the receive_banner.
Underlying problem
It was the result of the socket throwing a receive error when libssh2 tried to read from it as part of initialising your SSH session. Either the server is misconfigured and is not sending a banner or the underlying connection disconnected for some reason.
Solution
The best course of action seems to have adequate retrying in place for these kinds of errors. You are connecting to a network which is an action that can fail. As the number of servers you are connecting to increases, you are going to run into errors that are a result of the underlying network. Adequate error handling is your best course of action.
You can find how to set exception handlers from the PHP docs.

PHP mySQLi:: MySQL server has gone away

I'm trying to append a hush database directly from php.
$mysqli->query(file_get_contents('huge.sql'));
but I'm getting :
Warning: mysqli::query(): MySQL server has gone away in
I was add those 2 line at the beginning of my php code :
ini_set('mysql.connect_timeout',3000);
ini_set('default_socket_timeout',3000);
Not very helpful...
Unless you're getting an actual error and not a Warning, this doesn't necessarily mean you're doing something wrong.
With PDO for example, this warning can be issued when pooled connections are in use, such as when PHP is running as an Apache Module. There is a similar post here: How to fix the "server has gone away" warning when mysqli reconnects a persistent connection?
If what you're getting is simply a warning, and the query runs fine anyway, then you can either choose to request a persistent mysql connection, or ignore and suppress the warning.
If you're getting an error and your connection is getting terminated, then you should see https://dev.mysql.com/doc/refman/8.0/en/gone-away.html for most common causes of this issue.

GAE Random Uncaught Exception Error

I have a codeigniter app on google app engine and as I move about the application it will mostly work but I'll get these intermittent problems where I'll click on a page and it'll be completely blank with only a title in the HTML that reads:
<html><head>
<title>s~nypl-cap : uncaught application failure</title><body><pre><br></pre></body></html>
When I check the logs all I get is a 500 error that reads:
"A problem was encountered with the process that handled this request, causing it to exit. This is likely to cause a new process to be used for the next request to your application. (Error code 204)"
If I refresh the page it reloads just fine. I have no clue what could be wrong any help would be appreciated.
"App Engine cannot have more than 12 concurrent connections to a Cloud SQL instance". So, you need to close the any established connections before processing a request. If you do not do this it will cause a leak may eventually new connection to fail. And that could be one of the reason why you are getting 204 error. So in your "database.php" file in Production (App Engine Cloud SQL) DB Settings section can you change the line $db['production']['pconnect'] = TRUE; to $db['production']['pconnect'] = FALSE; and see if it works or not.

Swiftmailer crashes without php error

I have a web app that allows the user to upload a pdf and it will then email it to us via swiftmailer. With some pdfs, the process fails.
I can verify that it crashes the php script, yet returns no php error. There's a 500 error from the server, but normally if there's a 500 error, php has a log of what the error was.
I have also verified that it crashes at the
$mailer->send($message);
line
Oddly, only some pdfs crash it, and those same pdfs work fine on the development server with identical code.
What could be causing php to crash without an error message?
After running several tests, I found that error logging was happening some of the time, but not others. I didn't figure out why that was so, however, I tried renaming the php-errors.log file so php would start with a new, fresh log file, and now errors are getting logged properly. I don't know why that worked, but I'll take it.
FYI, I've run into two things that can cause a PHP crash without an error message:
Script timeouts - A timeout may prevent an error message from being returned; in my particular case the script was waiting for an SMTP response when the timeout happened, which may have been why I didn't get a timeout message. Try changing your max_execution_time value in php.ini to 300 (5 minutes) and see if you can get an actual error message.
Folder permissions - I've encountered a case where insufficient folder permissions resulted in the script just halting without providing an error.
In the case of 2, I wrapped a try/catch clause around the line that was causing the halt, and I finally got an Exception to show up explaining about the permissions problem. That may be worth trying as a general response to silent crashes.

MySQL server has gone away - ErrorException

I started getting this error when trying to query the database on a connection that had timed out.
Fatal error: Uncaught exception 'ErrorException' with message 'mysql_query(): MySQL server has gone away'
So I did a bunch of research and 99% of the users on the forum say you can use the mysql_ping command to check for a connection, so I put this in place:
if(!mysql_ping($this->sDBLink))
Connect(true);
Now I get the same error, just in reference to the mysql_ping function instead of the mysql_query function:
Fatal error: Uncaught exception 'ErrorException' with message 'mysql_ping(): MySQL server has gone away'
How do I reliably check that a connection still exists? mysql_ping throws an exception.
Why does a connection time out? Because it has been unused for a period of time longer than a particular threshold. This kind of thing is necessary for all kinds of network applications to make them resilient. What if a user abruptly switches off a client machine while it is connected to a MySQL server? The server needs to be able to drop the client connection after a while.
This kind of thing is inherent in network programming. It's a feature, not a bug.
What can you do about this?
You can switch to a more modern mysql connection management library that handles this stuff.
Especially if your client software gets used infrequently, you can reorganize your software to connect to MySQL, use the connection, and disconnect. That way you won't need a persistent connection. But that's impractical if your client server gets used a lot; there's a lot of overhead to establishing a connection.
You can change the timeout value. See here. how to change timeout for mysql persistent connections
You can use the connection regularly. mysql_ping uses the connection without actually doing any server work. So would a query that said something like SELECT 1. If you ping the connection every minute or so, then a two minute timeout won't cause your MySQL server to conclude that your client has gone away and disconnect it.
You can handle the ErrorException you're getting correctly, by trying to re-establish the connection instead of just blowing out your program with an error message. Use PHP code something like this:
try {
some operation on the mysql connection.
}
catch (ErrorException $ex) {
disconnect mysql
connect mysql
}
So I know this is ugly and makes me sick it, but it will work until we can get enough income to warrant an upgrade.
All I did was close the connection then recreate it as I know the connection will have timed out almost 95% of the time.
if($this->sDBLink){
mysql_close($this->sDBLink);
}
if($bPersistant){
$this->sDBLink = mysql_pconnect($this->sHostname, $this->sUsername, $this->sPassword);
} else {
$this->sDBLink = mysql_connect($this->sHostname, $this->sUsername, $this->sPassword);
}

Categories