"MySQL server has gone away "is a data-related error. This occurs when one of the tables that are present in your database is broken. Due to this, your database gets corrupted. In some cases, you can also lose your whole website's data. To be tension-free and be ready for such errors, you need to have a backup copy of your database and website. This will ensure that your website will never be deleted from the web.
But do not worry, if this error is occurring on your site there are some ways to fix these issues. Let's look into them one by one:
1. Editing your WordPress wp-dp.php file
The error can occur if your PHP timeout settings are extremely low and your database is large. If so, then the error can be fixed by editing one of your WordPress core files named wp-db.php.
It is the WordPress database file. This file is located in the wp-includes folder of the WordPress root folder.
To access these files use FTP clients such as FileZilla, or you can use SFTP. After finding out the file, open it with a text editor, such as Wordpad.
Then search for the line that says:
$this->ready = true;
Now to solve the issue, add the below code after the above line
$this->query("set session wait_timeout=300");
Now the PHP timeout value 300 seconds error will be solved. Save the file and the issue of "MySQL server has gone away " will be solved.
2. Repair your WordPress database
Sometimes your tables in WordPress get corrupted. This happens when you add loads of database tables, plugins, and themes. This can create errors. To fix this you can use a built-in WordPress function.
To solve this go to your WordPress root directory and open the wp-config.php file. Open the file and scroll down to the bottom. Add the code given below in the end.
define('WP_ALLOW_REPAIR', true);
Save all the changes made and close the file. To run the function you have to browse the URL given below:
https://yourwebsite.com/wp-admin/maint/repair.php
Change the 'yourwebsite.com' to your domain name. Then you will get options to Repair Database or to Repair and Optimize Database.
To fix the "MySQL server has gone away" error select the first option. After some minutes you will notice that the error
message will not appear again.
But at the end, you need to delete the line that you have added earlier. Otherwise, anyone can trigger it by accessing the same URL.
3. Restore your website using backup through your hosting provider:
To perform this step you need to have a backup copy of your website. Otherwise, you can not perform this step.
Not all hosting providers give you backup service. But you can also manually back up. You can restore your lost data from your backups and your website will be up and running again.
That's it. This is how you can solve the "MySQL server has gone away" error.