Guides WordPress

Solving WordPress Database Error [MySQL Server has gone away] in WordPress 3.3

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

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:

  1. Using a FTP client or online file manager, browse to your WordPress’s root folder.
  2. Move further to the wp-includes  sub-directory.
  3. Open the wp-db.php in a text editor which supports line numbers (NotePad++ or cPanel’s Code Editor recommended).
  4. 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" );

    solving wordpress 3.3 mysql error

  5. 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 =”

Tags

23 Comments

Click here to post a comment

Email me when somebody replies to my comment.

  • 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.

  • 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!!

    • It will work, but you need to find the appropriate lines as the code structure would’ve changed slightly already.

    • 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.

    • 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.

Subscribe for email updates!

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 4,246 other subscribers