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

Share This Post:
Share on FacebookTweet about this on TwitterShare on Google+Pin on PinterestShare on LinkedInShare on RedditEmail this to someone

,

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

  1. patty April 1, 2012 at 4:32 am #

    Thanks for posting this. I just implemented this change, will track errors and report back!

    • Admin April 1, 2012 at 1:55 pm #

      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.

      • patty jones April 2, 2012 at 2:32 am #

        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!

        • Admin April 2, 2012 at 4:56 am #

          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.

          • patty April 13, 2012 at 1:23 pm #

            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.

            • Admin April 13, 2012 at 1:58 pm #

              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.

        • Mpumelelo July 27, 2012 at 1:50 am #

          mine is not i tried all that out what might be the problem though??

          • Admin July 27, 2012 at 2:05 pm #

            What is the exact error that appears?

  2. Mpumelelo July 27, 2012 at 1:40 am #

    Guys it never worked for me and m still trying

  3. Aditya Khurana October 7, 2012 at 6:27 pm #

    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.

  4. Matt February 28, 2013 at 9:29 pm #

    It is now line 1164 in WordPress 3.5

  5. Colin May 31, 2013 at 12:46 am #

    Does fixing this error reduce site load, or just eliminate the error?

    Thanks.

    • singhnsk May 31, 2013 at 7:36 am #

      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.

  6. daihatsu xenia January 1, 2014 at 5:39 am #

    whether this technique works for wordpress 3.7.1

    • Narender January 1, 2014 at 10:11 am #

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

  7. Nick Castelucci October 10, 2014 at 2:34 am #

    Worked like a charm. Thanks!

Leave a Reply