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

If you’re regularly receiving the ‘MySQL server has gone away’ database error in your WordPress  based blog,

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:

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" );


All Done! Save the file and clear the cache, if you’re using a caching plugin.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s