Are you experiencing the MySQL Server Has Gone Away error in WordPress? No need to panic. As scary as it sounds, your site’s database isn’t actually gone.
In this article, we invite you to get to know what the MySQL Server Has Gone Away error is and what causes it. Not only that, we will also explain three ways to overcome them, namely by:
– Editing the wp-db.php file;
– Improve website databases;
– And use the hosting provider’s backup feature.
Have a good study!
What is the MySQL Server Has Gone Away Error?
MySQL Server Has Gone Away is an error that causes the website to be inaccessible. When this error occurs, your browser displays one of the following error logs:
– General error: 2006 MySQL server has gone away
– Error Code: 2013. Lost connection to MySQL server during query
– Warning: Error while sending QUERY packet
– PDOException: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away
So, the MySQL Server Has Gone Away error can be caused by one of the following four things:
– Troubled tables in the database
Your database is corrupted and you need to use a previous backup or repair the database.
– PHP timeout is too short
A PHP script that requires something from the database can be a culprit if the max duration is too short.
– Failed to send data fragments (packets).
The process of loading website pages includes transferring data from the database. When there are data fragments that fail to be sent, errors can occur.
– The data size is too large
This is related to the PHP timeout earlier. If the data size used by the website page is large, the PHP timeout needs to be extended so that the data transfer does not fail.
The good news, the four triggers above can be overcome easily. Let’s look at the methods in the next section!
Overcoming MySQL Server Has Gone Away with Restore Backup Database
To deal with problematic databases, the easy way is to restore the backup. However, there is something you need to know before putting this solution into practice. How to restore database backups depends on the control panel provided by your hosting provider. So, in this article we use the Niagahoster cPanel as an example.
1. Login to cPanel
First, access your cPanel dashboard. To do this, you can add /cpanel to the end of your website URL. For example websiteku.com/cpanel.
If so, log in with the username and password provided by the hosting provider when you bought hosting.
2. Access Menu Database Backups
After logging into cPanel, click the Database Backups menu. You can search for it using the cPanel search bar to make it easier.
3. Restore Database Backups
The menu will display a list of your database backups. Well, to the right of each backup you will find a Restore button. Click the button to restore database.
This method is guaranteed to be effective in eliminating database errors. However, keep in mind that doing this will remove the last update on your site and replace it with the last backup performed. For this reason, it is important for you to back up your database regularly.
Overcoming MySQL Server Has Gone Away by Editing the wp-db.php File
As mentioned, large data sizes and too short PHP timeouts can cause errors. Therefore, you should increase the duration. You can do this by editing the wp-db.php file in the website root folder. Following are the steps:
1. Access cPanel File Manager
Access your cPanel and go to File Manager.
2. Find the wp-db.php file
After entering the File Manager, access the public_html folder > wp-includes. Then, find the wp-db.php file.
3. Add PHP Timeout
Now you have to increase the PHP timeout by adding code to the wp-db.php file. To do this, you can use a separate text editor or the one in cPanel. In this guide we use the cPanel text editor as an example.
Right click on the wp-db.php file and click Edit to open the file. If the text editor appears, look for the line of code $this->ready = true.
To make it easier, you can also type the code in the cPanel text editor search bar.
Add $this->query(“set session wait_timeout=300”); below that line, then click Save Changes.
The code ensures that your PHP timeout is 300 seconds. With that long duration, the MySQL Server Has Gone Away error can be prevented.
Overcoming MySQL Server Has Gone Away by Repairing the Website Database
With the increasing volume of website data, sometimes the database can have problems. The MySQL Server Has Gone Away error can appear when this happens.
Fortunately, WordPress provides a built-in feature that allows you to fix the above problems. However, you must first enable the setting in the wp-config.php file. How to? Follow these steps.
1. Find the file wp-config.php
Open File Manager and access the public_html folder. then open the wp-config.php Folder
2. Repair Database
Like when dealing with the wp-db.php file earlier, right-click on the wp-config.php file and click Edit.
In your text editor, add define(‘WP_ALLOW_REPAIR’, true); at the very bottom of the wp-config.php file. This will enable the database repair feature.
Next, access https://yourwebsite.com/wp-admin/maint/repair.php in your browser. Two options will appear, namely:
– Database repairs
– Repair database and optimize
Choose the first option because you only need to repair the database. If so, don’t forget to delete the code to enable database repair that you added in the wp-config.php file. If not, other people can access your website database repair via the URL above.
Ready to Overcome MySQL Server Has Gone Away?
You don’t need to be afraid when dealing with the MySQL server has gone away error. Even if it makes the website inaccessible, you can overcome this error in the ways discussed in this article, namely:
– Edited the wp-db.php file
– Improve website database
– Restore database backups
Unfortunately, MySQL server has gone away is not the only WordPress error you can encounter. Often, these errors come unexpectedly.
[hurrytimer id=”247″]
