WordPress ERROR-MYSQL Server Gone Away!!!

If you ever get this error message:

"WordPress database error MySQL server has gone away for query…"

Here is what you need to do:

Open the file wp-includes/wp-db.php using PSPAD

Find the following code in the file (line no. 338-339):

function wpdb($dbuser, $dbpassword, $dbname, $dbhost) {
return $this->__construct($dbuser, $dbpassword, $dbname, $dbhost);

After the above, add the following code:

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

So that the whole code becomes:

function wpdb($dbuser, $dbpassword, $dbname, $dbhost) {
return $this->__construct($dbuser, $dbpassword, $dbname, $dbhost);
$this->query("set session wait_timeout=600");

Save and re-upload the file to your server.

I don’t know if or how you have encountered this error message, but in my case, I got this error message while running a manual backup using the BackWPUp plugin. No fault of the plugin though, because as soon as I added the session code mentioned above, the error message vanished! I got the idea from this forum topic but felt it was not clear enough, so I posted an article of my own. Hope this helps.

There is another article here that offers different solutions to this problem, although I could not find any solution for WP 2.9.2 and above. :)


  1. Emma Lynn

    Hey Arindam,

    I think you’re reading my mind with your recent blog posts :)

    First, you posted about WP optimization which was on the day that my web host complained about resources (cleaned up the code, used your unhooking techniques in the function file and installed W3 Total Cache).

    Then, just as I’m setting up my new site at, you posted about using WP as a static site. I already knew most of it, but it was still a useful checklist.

    NOW, this morning I’ve checked the error log on Click Nutrition, and found a load of “WordPress database error MySQL server has gone away”. Am now going to follow your advice :) (again).

    So my question to you, what other WP issues will I be facing in the next few weeks :) :)


    1. Arindam


      Just keep an eye on my boring blog :P

      Actually, I write about issues as I face and resolve them; I write them not just to help others but also to help myself-in case I have similar issues again in future, the written word helps. :D

  2. Julian Bleecker

    I don’t exactly get why putting this line after the return statement is supposed to work. Return means that the function exits and returns the value to the calling method. But..maybe PHP is different from C++, ObjC and Java.


    I put this in the function __construct after the last line, $this->db_connect() and it works — it didn’t when I followed the instructions above.