Fix WordPress Database Error "MySQL server has gone away.."

Read {count} times since 2020

If you run a WordPress blog and see the error "MySQL server has gone away for query….." on your error logs a lot, you are certainly in trouble. When this happens, your blog will be down. It happened to this blog and I started searching for a solution. Finally I got a solution and it works. It’s a simple solution. All you have to do is add 2 lines after a specific code. Let’s get right on it.

This trick will work for all WordPress versions. Open your Wordpress directory. The directory will contain the folders :

  1. wp-admin
  2. wp-content
  3. wp-includes

Open wp-includes. Search for the wp-db.php file. If you found the file, open it using a text editor. Using the text editor’s search tool, search for :

$this->ready = true;

Here is a screenshot of the found string in gEdit text editor :


Once you found the line, add the following lines just after the found line :

//WP Query Gone Away Error Fix

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

Save the file. Now, when you check the error log of your site, you won’t see the error anymore which means that the site won’t go down as before.

This is just a temporary fix. You can’t be 100% sure that the error won’t come anymore. By doing this "fix", MySQL will wait for some time to finish a query. Here are some tips to fix this error permanently :

  1. Try Disabling / Removing Very High Memory Needed Plugins
  2. Disable plugins one by one and find the plugins that cause this error
  3. Use plugins that you only need. More plugins means more C.P.U memory and MySQL server will be overloaded
  4. Use LightWeight Themes that only uses less memory ( )
Show Comments