If you’re regularly receiving the ‘MySQL server has gone away’ database error in your WordPress 3.3 based blog, then this tutorial is for you.
The error usually occurs at high CPU usage because of MySQL server closing the connection before the full request was made. The error can be rectified by increasing the timeout of the MySQL connection. Here’s how to do it:
Update: It is working fine with WordPress 3.4.1
Contents
Editing the wp-db.php file
In order to get rid of the error, we need to place a query in the wp-db.php file to keep the connecting open for a longer duration. Follow these steps to do that:
- Using a FTP client or online file manager, browse to your WordPress’s root folder.
- Move further to the wp-includes sub-directory.
- Open the wp-db.php in a text editor which supports line numbers (NotePad++ or cPanel’s Code Editor recommended).
- Move to line number 1057 (approx., See Screenshot below) and copy > paste the following code there before the $this->dbh query.
$this->query("set session wait_timeout=600" );
- All Done! Save the file and clear the cache, if you’re using a caching plugin.
Alternatively, if you weren’t able to edit the wp-dp.php file correctly, you can download this already edited file (link here), extract the zipped file and upload it to the wp-includes directory.
Why it happens?
Whenever a code makes a call to the MySQL, the connection stays open for sometime so that other queries can be made at the same time without the need to re-open the connection. The connection is auto-closed if it stays idle for more than the duration in ‘wait_timeout‘. The above workaround keeps the connection open for a longer duration and thus prevents such an error.
Checking the default value ‘wait_timeout’
The default value of wait_timeout is usually 10 seconds, but it might be changed by your web host. You can find out the value of wait_timeout set by your web host under Variables tab in phpMyAdmin.
Error Sample
The database errors could be of many forms. Error can look like:
“WordPress database error MySQL server has gone away for query SELECT comment_ID FROM wp_comments WHERE comment_post_ID =”
I have installed wordpress on live server but if I continue refresh page of wordpress , it stop the mysql server and give database connnection error. Any suggestion please.
Now it´s between 1499 and 1520 😉
[…] Sometimes, depending on MySql settings of the hosting provider, during the restoring process in step 10 you may encounter this error: “Error Message : MySQL server has gone away“. This happened to me, but I found a workaround in this article. […]
Line in latest WP is 1445
Does anyone have any idea where the line has moved to in wordpress 4.1? I think they have changed the coding as no timeout line anywhere….looked up and down and obviously missing it!!
Agreed, it would be great to get an answer to this for WordPress 4.1!
Thank you for this guide, saved me tons of freaking time!
Worked like a charm. Thanks!
whether this technique works for wordpress 3.7.1
It will work, but you need to find the appropriate lines as the code structure would’ve changed slightly already.
Does fixing this error reduce site load, or just eliminate the error?
Thanks.
Nothing to do with the load and it will remain the same. All this will do is to keep the connection open for longer which will also eliminate the error. It is only important if portions of your blog aren’t properly loading or if visitors are receiving errors when they fill out forms (like writing a new comment). Sometimes those comments won’t even reach to your dashboard.
It is now line 1164 in WordPress 3.5
Just going to try this on my website after deleting the error_log which had bloated to 60MB in size. Thanks a lot for posting. Will try and report the follow up soon.
Guys it never worked for me and m still trying
Thanks for posting this. I just implemented this change, will track errors and report back!
I'm sure you won't see that error again. It worked perfectly for me. Now, as you've said, please do update us if it worked for you or not.
It has worked perfectly for me, I have not seen that error a single time since I added this line of code. I will have to bookmark this page and add this to my list of things to do when wordpress upgrades.
I will continue to monitor this. We were down most of the day yesterday due to a DDOS attack…
Thanks again for posting this!
Good luck with your site during the DDOS attack. I wish you won't experience that again.
BTW, thanks for finding the time to report back.
Checking back… The DDOS attack knocked us down for most of 4 days during that week. Not fun.
In any case, I checked my error logs this evening and am still finding "WordPress database error MySQL server has gone away for…" It is not happening nearly as often but there are still many, more than 40 for yesterday and a dozen times today, but that surely beats the hundreds that would occur before.
That might be caused by some plugin using different settings for itself. Usually, all connections should not take any time longer than that specified in the wp-db.php file.
Still, you can try increasing the timeout even further to a more higher value, if that helps.
mine is not i tried all that out what might be the problem though??
What is the exact error that appears?