Everything Else

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 utilization 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,

So I opened it up to see what it contained,

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

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


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

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->db.) {” statement – see below in blue
  3. Paste inns thins->query("set session wait timeout=600");– see below in red

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


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!


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,

Related Articles

Back to top button