Since databases are the major components of most modern websites and therefore if there are errors that affect your website, then you need to take them seriously. For example, the “MySQL server has gone away” error might force you to believe that your database was lost. It means you might have to restore your latest backup for getting your site up and running.
Though it sounds a bit daunting, but, this error is really very easy to fix. You simply need to follow the instructions properly and your website will get back to normal in just few minutes.
This article will help you resolve the “MySQL server has gone away” error and also the causes of it.
Understanding the “MySQL Server Has Gone Away” Error
At first, let’s see how the “MySQL server has gone away” error looks like:
You can see that the error itself is straightforward and it almost always is displayed in the same way. Based on the browser you use and server configuration, the words might change to some extent.
From the error itself, you can identify that it is related to your MySQL database. For being more specific, the reasons for this error to occur on most websites are as below:
A broken table is present within your database. Your database gets corrupted and so you need to get back to a recent backup or repair it.
The PHP ‘timeout’ setting is lower than what it should be. In case a PHP script requires accessing your database and it isn’t able to fetch the information within the timeout window that is set, this can also give rise to the above-mentioned error.
The packets are dropped or may be are too large. The server might believe the same and closes the connection, displaying the error.
Thankfully, you can easily address these issues. Let’s see the troubleshooting process for them.
3 Ways to Fix the “MySQL Server Has Gone Away” Error in WordPress
As mentioned above, there are a few potential reasons for this particular WordPress error to occur.
You have different possible solutions for this error. In several cases, one of the below method will help you to get rid of the error on your website. So, if one doesn’t help you, you can try another one.
1. Editing Your WordPress wp-db.php File
In case your website’s PHP timeout setting is extensively low and your database is very large, you won’t be able to fetch the data required. As mentioned earlier, this can display the “MySQL server has gone away” error.
In order to avoid this, it is required to edit one of your WordPress core files, called wp-db.php. This file is located within your WordPress root folder, by opening up the wp-includes directory:
In order to access these files, it is recommended to use an FTP client such as FileZilla and connect via SFTP. After you get connected to your site, find wp-db.php, and right-click on it to open the file using your default local text editor.
Then, search for the following line in the file:
$this->ready = true;
Now add the below line right below that code:
$this->query("set session wait_timeout=300");
This code will set your PHP timeout value to 300 seconds, which is a lot more than you need to avoid any errors from showing up.
Then save the changes to your wp-db.php file and ensure your website is loading normally.
2. Repair Your WordPress Database
Sometimes your WordPress database can get corrupted and this can trigger error when you try to establish a connection with it. Though this isn’t common, it can happen even with a normal website’s growth when you add more tables to your database ( plus plugin and theme information).
For fixing this issue, a built-in WordPress function can be used to repair your database. At first, you will need to enable that feature. This can be done by accessgin your WordPress root directory and opening the wp-config.php file for editing it.
After file is open, scroll to the bottom and add the below line to it:
define('WP_ALLOW_REPAIR', true);
This code asks WordPress to enable the database repair function. Next save the changes to wp-config.php, and close the file. To run the function, just browse the below URL:
https://yourwebsite.com/wp-admin/maint/repair.php
WordPress will then prompt that you want to simply repair your database or repair and optimize it. Select the first option to fix the “MySQL server has gone away” error:
It will need just few minutes for the process and when it’s done, the error in question will get disappeared. But there is still some cleanup left to do, because you will need to disable the database repair function on your website. If you avoid this, anyone can trigger it by accessing the same URL.
So, go to your WordPress root directory and remove the line of code which you had added earlier. After this, save changes to the file and close it.
3. Restore Your Website Using a Backup Through Your Hosting Provider
If none of the above works for you, just use the full backup of your website to restore it to a point till the database was working properly. Of course this needs to be done with a recent backup so that you lose as little data as possible.
All WordPress web hosting providers don’t offer built-in backup functionality for their users. Therefore, you often get stuck on using plugins for backups. These tools aren’t bad but if don’t have an access to your WordPress admin area, restoring backup becomes a big struggle.
MilesWeb offers you website backup feature which is paid but you shouldn’t avoid buying it.
Conclusion
When your website is in growing, it will need to store more data. All the information gets stored into your WordPress database. In some cases, if the data is too big, you might get errors such as “MySQL server has gone away”.
Now that you know the troubleshoot process, you can get rid of this annoying error message.