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);
}
Related
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.
I'm building a web app that uses lots of requests to my database. Every thing was working perfectly smooth until a half an hour ago when the requests weren't returning... I checked the PHP file directly and it displays the following:
<br />
<b>Warning</b>: mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Too many connections in <b>/home/sanity/public_html/dev/forest/js/database.php</b> on line <b>7</b><br />
Unable to connect to MySQL
So I figured let's check phpMyAdmin, but it's not showing me ANYTHING except for a big red box that says:
SQL query: Edit Edit
SET CHARACTER SET 'utf8';
MySQL said: Documentation
#1045 - Access denied for user 'root'#'localhost' (using password: NO)
Between the last time it worked and now I haven't changed any configurations or code.. How do I begin to fix this?
Could this be caused by the fact my PHP files don't close the connection after using it? If so should I be closing the connection after every query? I figured the connection would close automatically when the user leaves the web site.
EDIT: The requests are sending through now and phpMyAdmin is back up, but how do I prepare this site for heavier traffic?
When I started my job, one of my first tasks was to continue working on what one of the directors had started coding. In his code, I saw this monstrosity:
function getTicket($id) {
mysql_connect("localhost","username","password");
mysql_select_db("database");
$sql = mysql_query("select * from tickets where id = ".intval($id));
return mysql_fetch_assoc($sql);
}
In other words, he was creating a whole new database connection every single time he wanted something from the database, and never closing any of them (instead letting them be closed at the end of the script automatically)
This was fine for basic testing, but as soon as I started writing more advanced stuff (before I'd discovered this piece of code) things majorly screwed up with the same "too many connections" error as you.
The solution was simple: restart the server to clear all pending connections, and fix the code to only connnect once per script execution.
This is what you should do. There should only ever be one call to mysql_connect (or indeed any database library's connect function) in your script (and I don't mean in a function that gets called several times!)
You should also check the database's configuration, just in case someone accidentally set the maximum connections too low, but generally this shouldn't be a problem if you manage your connections properly.
Though the mysql_* functions are deprecated (use a modern driver like PDO for instance), you should take a look at the mysql_close($con) function.
Here is the doc.
EDIT
If you are not using mysql_pconnect function, then your connection should be closed at the end of the execution of your script.
Apparently, one reason for such error is shared hostings. If you are using a shared hosting, generally speaking, the maximum connections to the server allowed by the hosting is not the greatest.
If you can change the max_connections system variable then try to change it to a greater number:
max_connections = 200
I've seen this error a couple of times, and to fix it, I just reboot my server.
Fatal error: Uncaught exception 'MongoConnectionException' with
message 'Failed to connect to: localhost:27017: Previous connection
attempts failed, server blacklisted' in
/var/www/html/include/config.php:9 Stack trace: #0
/var/www/html/include/config.php(9):
MongoClient->__construct('mongodb://local...') #1
/var/www/html/classes.php(3): include('/var/www/html/i...') #2
/var/www/html/myusers.php(8): include('/var/www/html/c...') #3 {main}
thrown in /var/www/html/include/config.php on line 9
However, I can be a while without seeing it... How can I prevent the issue from happening?
update: it happened again and after several minutes of waiting, I had to reboot to make the site work again
Since the 1.4 versions of the MongoDB driver for PHP we will "blacklist" servers for up to a minute if they can not be contacted to. This is so that we do not slam the server with connections, that might timeout. This is primarily done to make sure that in a replica set environment we can still proceed by just using another of the hosts, but of course if you only have one machine, this is a bit trickier.
If you use MongoLog then you can very easily spot what happens under the hood:
MongoLog::setModule(MongoLog::ALL);
MongoLog::setLevel(MongoLog::ALL);
MongoLog::setCallback('print_mongo_log');
function print_mongo_log($a, $b, $c) { echo $c, "\n"; }
This will display everything the driver is trying to do. It would be interesting to see the first dump of when something goes wrong, and also for one time it is "stuck" on the blacklist.
The above warning will go away after 60 seconds, or upon reboot of your web server software (or PHP-FPM is you use that). If you think this explanation is not correct, please file a bug/feature request at http://jira.mongodb.org/browse/PHP
Apparently, this is an issue caused by a bug in MongoDB's PHP driver. Check if you're using version 1.4.0, if yes, update to a newer version and the error should be fixed.
It seem mongo handle connections itself, you should not use close connection in your code if you want use persistence mongo connection, I've resolved this issue by removing close from mongo connection php script like this:
DON'T USE CLOSE..............
$client = new MongoClient("mongodb://127.0.0.1:27017");
//close mongo connection
$client->close();
Hope to be useful.
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.
While I am browsing my online app in the server I got an error like
Database Connection Failed
User coule_com#c17564 already has more than 'max_user_connections' active connections.
But this is working well in my local system. And this error occurs ANY TIME when I navigate in the server. If i refresh the browser i can able to move further. But I in need to solve this issue.
will anybody help me to solve this issue ?
connection code :
function makeConnection() {
global $config;
$this->ConLink = mysql_pconnect($config['DBHostName'],$config['DBUserName'],$config['DBPassword']) or die("Database Connection Failed". mysql_error());
mysql_select_db($config['DBName'], $this->ConLink);
return true;
}
Are you closing the connections when you're done with them?
If not, then I would assume there's lots of database connection objects lying around just waiting for GC to pick them up, and until it does, you risk running out of available connections.
you need to contact your host and get them to up the connection limit. if they won't, you need to find a better host. this is simply a fact of life in web-based applications.