Databases are key components of most modern websites so errors that affect yours tend to be particularly worrisome. The “MySQL server has gone away” error, for example, may make you believe that your database was lost. That means you might have to resort to your latest backup to get your site up and running.
Despite how intimidating it sounds, however, the”MySQL server has gone away” error is pretty easy to fix. In fact, with the right instructions, your website should be back up and running in a matter of minutes.
In this article, we’re going to show you what the “MySQL server has gone away” error looks like and break down what can cause it in WordPress. Then we’ll teach you how to fix it and prevent this error from showing up again in the future.
Let’s get to work!
An Introduction to the “MySQL Server Has Gone Away” Error
First, let’s take a quick look at what the “MySQL server has gone away” error looks like:
The error itself is pretty straightforward and it almost always appears in the same way. Depending on which browser you use and your server’s configuration, however, the specific wording might change a bit.
As for the error itself, it has to do with your MySQL database, as you might imagine from the name. To be more specific, one of three things usually causes this error on most websites:
- There’s a broken table within your database. Your database became corrupted so you need to revert to a recent backup or repair it.
- Your PHP ‘timeout’ setting is too low. If a PHP script needs access to your database and it can’t fetch the information within the timeout window that’s been set, this can also trigger the aforementioned error.
- ‘Packets’ have either been dropped or are too large. The server deems this to be the case, it essentially closes the connection and throws up the error.
Fortunately, all of these issues can be easily addressed. Let’s talk about how you can start the troubleshooting process.
How to Fix the “MySQL Server Has Gone Away” Error in WordPress (3 Methods)
As we’ve seen, there are a few potential causes for this particular WordPress error.
For that reason, there are different possible solutions. In most cases, one of the fixes below should get rid of the error on your website. So if one doesn’t work, you can simply move on to the next.
1. Edit Your WordPress wp-db.php File
If your website’s PHP timeout setting is too low and your database is too large, fetching the data you need during that window can be a problem. As we mentioned before, this can trigger the “MySQL server has gone away” error.
To prevent that from happening, you’ll need to edit one of your WordPress core files, called wp-db.php. You can find this file within your WordPress root folder, by opening up the wp-includes directory:
For accessing these files, we recommend that you use an FTP client such as FileZilla and connect via SFTP. Once you’ve connected to your site, locate wp-db.php, and right-click on it to open the file using your default local text editor.
Then, search through the file for the following line:
$this->ready = true;
Add the following line right below that code:
$this->query("set session wait_timeout=300");
What this code does is set your PHP timeout value to 300 seconds, which should be a lot more than you need to prevent any errors from showing up.
Now save the changes to your wp-db.php file and make sure your website is loading as it should.
Please note that if you’re a Kinsta user, you shouldn’t need to alter your website’s PHP timeout settings. All of our plans have a base timeout value of 300 seconds out of the box and we can help you increase it depending on your needs.
2. Repair Your WordPress Database
Sometimes your WordPress database can become corrupted, which in turn can lead to errors when you’re trying to establish a connection with it. This isn’t all that common but it can happen in the course of a normal website’s growth as you add more tables to your database (as well as plugin and theme information).
To fix this issue, you can use a built-in WordPress function to repair your database. First, however, you’ll have to enable that feature. This involves navigating to your WordPress root directory and opening the wp-config.php file in order to edit it.
Once the file is open, scroll to the bottom and add the following line to it:
That simple line of code tells WordPress to enable the database repair function. Save the changes to wp-config.php, and close the file. To run the function, just visit the following URL:
WordPress will then ask if you want to simply repair your database or repair and optimize it. The first option is all you need to fix the “MySQL server has gone away” error:
The process shouldn’t take long, and when it’s done, the error in question should be gone. However, you still have some cleanup to do, as you’ll need to disable the database repair function on your website. If you don’t, anyone could trigger it by accessing the same URL.
Before you wrap up, therefore, return to your WordPress root directory and remove the line of code you added earlier. Then save your changes to the file and close it.
3. Restore Your Website Using a Backup Through Your Hosting Provider
If all else fails, you can always use a full backup of your website to restore it to a point when the database was working properly. Ideally, you’ll do this with a recent backup so that you lose as little data as possible.
The problem is that not all WordPress web hosts offer built-in backup functionality for their users. That means you’re often stuck using manual solutions such as plugins. These tools aren’t necessarily bad, but if you don’t have access to your WordPress admin area, restoring a backup becomes a tall order.
Kinsta provides automatic daily and system generated backups for all plans. Optional hourly backups are also available.
Here at Kinsta, on the other hand, you get access to full backups of your website with every plan. To restore your site (including its database) to an earlier point, you just have to access your hosting dashboard and look for the Backups tab:
Click on the backup you want to restore and you’ll see more details about when it was created. For each backup, there’s a Restore button you can use to return your website to its state at that time.
Do remember, though: using this functionality will overwrite the current version of your website. So you’ll only want to use it as a last-resort measure when you’re sure you won’t lose any critical information.
All Kinsta plans features weekly automatic MySQL database optimization to ensure better database performance. Check out our plans for more info.
The more your website grows, the more data it will need to store. All of that information goes into your WordPress database. In some cases, if it gets too big, you might run into errors such as “MySQL server has gone away”.
If you encounter this particular error, here are three ways you can get rid of it:
- Edit your WordPress wp-db.php file.
- Repair your WordPress database.
- Restore your website using a backup through your hosting provider.
Now that you know how to fix, it’s time to actually get rid of this annoying error message. If you’re still experiencing issues with MySQL, you might want to check How to Fix the MySQL 1064 Error.