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

solving-wordpress-3.3-mysql-error-300x261

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