How to Fix MySQL Server Has Gone Away Error in WordPress

One reason why WordPress works so effectively is that it is powered by a MySQL database technology in the background. MySQL database works as a storage engine for your site settings, posts, pages, comments, and other content. Should something go wrong with that database, your server will throw up an error message. Are you currently seeing the following error? “MySQL Server Has Gone Away”.

If you are, it means something went wrong with your database.

To a beginner, and even someone with advanced knowledge, this MySQL error log can be indeed scary, and that’s quite understandable. At first, it might seem like your MySQL server has really gone away – i.e. it just disappeared. Or perhaps something went awfully wrong and the server died.

In reality, none of the aforementioned assumptions are correct. When you see the error MySQL server has gone away, it indicates that:

  • Database connections have been closed
  • Server exceeding timeout setting
  • A file you are trying to upload to your database is too much for the server to handle

Other factors can also be responsible for this error, as you will get to find out later.

If you are currently experiencing this error, don’t fret. You are about to find out how to fix it quite easily.

MySQL Server Has Gone Away Error: What Causes It?

Server has gone error is one of those errors developers often encounter when working with MySQL.

This error, usually, stems from MySQL performance settings. For instance, MySQL server has a connection window of 8 hours. That is to say, if the server is left idle for 8 hours, remote MySQL connections will be lost. And this lost connection leads to an error.

But there are quite a number of other factors that can contribute to this error. They are as follows:

  • Your MySQL server wasn’t set to reconnect automatically. So when a timeout occurs on the server, client connections will be lost.
  • Something corrupts your database, thus prompting the need to fix it or recover the corrupted database from a backup.
  • The server ran into problems while executing a query fed to it.
  • This error can also be a result of the query being too large for the server to process. When this happens, the MySQL server closes the connection with the client-side and then pops up an error.
  • It’s equally possible that the problem could be coming from your hosting provider. For instance, if your database server goes down in your host, you may see this error.

With all that said, let’s now see some of the ways to fix this error.

How To Fix MySQL Server Error

Thankfully, this error, sometimes seen as“general error: 2006 MySQL server has gone away”, can be fixed with some work. In this section, we will be showing you some of the methods to restore your database. As a matter of fact, some of the methods are so simple that you can have your website up and running within minutes.

Method 1: Fix Any Corrupted Database

Like earlier mentioned, a corrupted database can cause your server to throw up errors. Usually, the more you add content and plugins to your WordPress site, the more tables will be added to your database.

With time, your database starts to expand, thus increasing the chances of a table getting corrupted.

Thankfully, a corrupted database can be fixed, and the process is fairly simple. First, you will have to access your site via FTP. When connected, open the folder containing your WordPress files and locate the wp-config.php file (your main configuration file).

Once you have access to this SQL file, open it and paste the following line of code to it.

define(‘WP_ALLOW_REPAIR’, true);

Ensure you save and upload the file after pasting the above code.

What this code does is to instruct WordPress to initiate the database repair process. To complete the process, copy the following lines of code and paste into your browser.

https://mywebsite.com/wp-admin/maint/repair.php

Of course, you will have to replace mywebsite.com with your actual URL.

Once you do this, you will see the screen below. Click Repair Database.

Repair database to fix MySQL server has gone away error

Upon click, WordPress will automatically fix broken database tables, and after that restart MySQL.

The MySQL server error code should be gone now. But if it hasn’t, move on to the next step.

NoteIt’s important you disable the database repair function. If you don’t someone might manipulate your database just by typing the repair function URL.

To do this, navigate back to your wp-config.php file, and delete the code you had earlier pasted there.

Method 2: Increase Timeout Setting

Like we had earlier mentioned, a low timeout window can cause MySQL to run into problems. As such, one of the ways to fix MySQL database connections problem is by increasing your site’s timeout value.

To do this, you will have to make edits to your site’s wp-db.php file. As the name suggests, this is where your site’s MySQL settings are saved.

This file sits in the wp-includes folder, and to access it you will first have to establish a FTP connection.

Again, open the folder containing your WP file, and locate the ‘wp-includes’ folder. Once inside this folder, look for wp-db.php file. When you find it, right-click on it and select View/Edit

Increase timeout to fix MySQL server has gone away error

With the file opened, copy and paste the following lines of code

$this->query(“set session wait_timeout=400”);

For best results, ensure that you paste the code just below the line that reads

$this->ready = true;

Basically, what you are telling WordPress is to increase the timeout value to 400 seconds.

Save the file and exit. With the timeout value increased, your site should be restored now. If this method still doesn’t work, proceed to the next method.

Method 3: Do a Full Backup Restoration

If up to this point nothing has worked, the best thing to do is a full db (and possibly site) restoration from a backup. Of course, for this to work, you should have a full backup of your website.

Depending on your hosting provider, your site might have been backed up automatically. So check with your provider to know if they have a backup copy of your website.

Conclusion – MySQL Server Has Gone Away Error

MySQL server has gone” is an error nobody ever wishes to encounter. However, this error might just happen to you, especially if your site is large or rapidly growing.

In this article, we solved MySQL server error and showed you the steps we took. If you followed through, your site should be up and running by now.

If not, you may decide to request professional help and have it fixed quickly.

Posted in Hosting Article.