Solution for “WordPress database error MySQL server has gone away for query…”




If you have a WordPress blog and it is hosted on a shared server then you may want to continue reading..

For the past six months or so, my blog has been doing weird things.. like timing out, taking ages to load pages, sometimes the login page wouldn’t appear for me, etc..

Just random events occurring now and then.. or so I thought.

Also my CPU utilisation was going through the roof, resulting in warnings from my web host to reduce it, or else risk my site going offline.

So I decided to dig a bit deeper to try to find out the root cause of these issues.

Error Log

After logging into my website one day I noticed an error_log in the /public/html directory,

error log Solution for WordPress database error MySQL server has gone away for query...

So I opened it up to see what it contained,

view edit Solution for WordPress database error MySQL server has gone away for query...

I was quite shocked when I discovered the following error,

WordPress database error MySQL server has gone away for query…” was appearing multiple times a day, every single day..

WordPress database error MySQL server has gone away for Solution for WordPress database error MySQL server has gone away for query...




Why This Error Appears

After digging around using Google, I eventually found this brilliant article at Rob’s Notebook called “Workaround for WordPress database error: [MySQL server has gone away] for different versions of WordPress” that provides the workaround for this error.

Rob goes on to explain why this error appears,

“When someone tries to view one of your WordPress web pages, the PHP code first opens a connection to the MySQL server. Subsequent queries to the MySQL server rely on the connection remaining open, and do not re-open the connection. If the connection to the server happened to have shut down before a query is made, then WordPress will display this error.”

Solution

Rob’s solution is to change the “wait_timeout” variable to 600 in the wp-db.php file which is located in the \wp-includes directory,

wp db.php  Solution for WordPress database error MySQL server has gone away for query...

Open up this file using a text editor.. and do a search for the following text (this will bring you to the general area that needs to be edited),

  • !$this->dbh

Now copy the following,

  • $this->query("set session wait_timeout=600");

The place to insert this line is,

  1. Inside the “__construct(…)” function – see below in green
  2. And after the closing brace ‘}’ of the “if (!$this->dbh) {” statement – see below in blue
  3. Paste in $this->query("set session wait_timeout=600");see below in red

code Solution for WordPress database error MySQL server has gone away for query...

Now <Save> this newly modified wp-db.php file.

Done!

Since adding this, the “WordPress database error MySQL server has gone away for query…” no longer appears in my error_log..

My blog has now become more stable, more efficient and more responsive.

Future WordPress Upgrades

You will need to do this each time you upgrade WordPress.. a real pain.. but necessary to keep this error from appearing.

Full credit goes to Rob for discovering this solution!




Finally..

If you liked this post please register to receive The Social Media Guide’s free newsletter that’s packed with tips, guides, downloads and more!

Most of these posts are available to download for free as PDFs for registered members.

Let your Facebook friends know that you Like The Social Media Guide by clicking on,

I like The Social Media Guide41 Solution for WordPress database error MySQL server has gone away for query...And following me on Twitter,

follow @socialguide on twitter32 Solution for WordPress database error MySQL server has gone away for query...Enjoy!

Related posts:

  1. Sync your WordPress blog posts with your LinkedIn profile
  2. How to Upgrade your WordPress Theme to a Newer Version
  3. How to publish from Google Docs to your WordPress blog
  4. The WordPress Security Guide
  5. How to add a WordPress Blog to YouTube
  6. How to add Google Friend Connect to WordPress
  7. Yamidoo Magazine – Premium WordPress Theme Review

TAGS: , , , , ,

16 Comments

  1. Diane says:

    I have a dedicated server. I was getting this error message.

    I tried what you suggested but for some reason it didn't work

    so my hosting company started erroring out the error logs now

    the cpu usage has gone out of the roof and they state there so

    many errors within the mysql database the server cannot function

    Do you know of a way to correct this problem

    Thanks,

    Diane

    • There may be a conflict with one or more of your plugins.

      I would firstly delete all non-essential plugins.

      Then, disable the rest of them.. and re-enable each one at a time.. that way you can better determine if there is a rogue plugin causing your high CPU usage.

  2. Hemant Arora says:

    Hi, I have the following line in my wp-db.php, right where you have illustrated:

    $this->query("set session wait_timeout=3600");

    Still I get the 'database has gone away' error in my err_log, and my app doesn't work. In fact, it is a custom plug-in that performs XMLRPC on multiple blogs, at regular intervals. This might cause a timeout, because of XMLRPC response delay.

    Please help.

    Thanks.

    • Maybe try changing the code on that line to the code I have got?

      Instead of <code>$this->query("set session wait_timeout=3600");</code>

      Try <code>$this->query("set session wait_timeout=600");</code>

      Make sure you backup this file first.

  3. Ian says:

    Is there a reason why you shouldn't just set this in the mysql config file instead?

  4. Diane says:

    Does this work when you are getting the error

    on a plugin. I am getting this in an error log on one of my plugins

    Thanks,

    Diane

  5. gede says:

    just said thanks!! very hopely and now my blog running well again, thanks :)

  6. Jose says:

    I tried this with wordpress 3.0.1 and it didn't work. Do you know any trick for 3.0 version?

  7. Thanks for that, VERY good to know.

    I do have one question though:

    Does it make a difference with it being a shared server?

    I will be setting up a new WP installation soon that should have a couple of thousand users logging on to watch videos. I am trying to get a handle on whether I can stick with shared server; VPS; or other hosting option…

    Any suggestions?

    Thanks,

    Chris

    • Really depends on cost and how much you are willing to pay for your hosting.

      Shared is the cheapest, then VPS, then dedicated..

      • Diane says:

        I am on a dedicated server. I have an error log on a plugin stating this. I have tried this to fix it but it doesn't work

        Is there something else you can use when the error is

        on a plugin

        Thanks,

        Diane

        • If this solution doesn't work for errors appearing for a particular plugin.. then the only things I could suggest would be, update the plugin to the latest version.. or disable the plugin.. or contact the plugin's developer and report the issue.