Seeing the “MySQL server has gone away” error appear in WordPress essentially means that the connection between WordPress and the MySQL database has been lost.
In this guide, we’ll walk through some common causes of this error and the steps you can take to resolve the issue in WordPress. With the right troubleshooting, you can get your site back up and running smoothly again.
What Causes the “MySQL Server Has Gone Away” Error in WordPress
There are a few potential reasons that the MySQL connection could be dropped:
- Server timed out – By default, MySQL closes idle connections after 8 hours. This can occur if your site has low traffic.
- Exceeded resources – Using up the maximum number of connections or memory available to MySQL could sever the link.
- User privileges changed – Altered user permissions in the database can interrupt the connection.
- Server restarted – Any server reboots can temporarily disrupt the MySQL connection.
- Database failure – An outage or crash of the MySQL database itself will break the connection.
- Plugin conflicts – Certain plugins like caching tools may incorrectly close database connections.
Understanding the source of the disconnection helps resolve it. Next, let’s go through some troubleshooting steps.
Troubleshooting and Resolving the “MySQL Server Has Gone Away” Error
Increase the MySQL Timeout Duration
One of the most common reasons for the “gone away” error is that the MySQL timeout window is too short. The default is 8 hours, which may be too restrictive for low-traffic WordPress sites.
Increasing the timeout duration gives idle connections more time before being closed. Here’s how to extend the timeout in WordPress:
- Open your wp-config.php file in the WordPress root directory.
- Add the following code above the “/* That’s all, stop editing!” line:
define(‘MYSQL_TIMEOUT’, 28800);
- The timeout is defined in seconds. The value of 28800 sets it at 8 hours.
- Save changes and the MySQL timeout will be extended.
This prevents idle connections from reaching the timeout limit as frequently.
Check User Privileges and Credentials
If the MySQL user associated with WordPress has been revoked access or had permissions changed, that could explain the sudden failed connection.
Double-check that the database user entered in wp-config.php has full privileges. Also verify the password is still accurate and has not changed.
Reset the credentials if needed to restore the authorized connection. Just be sure to use ESCAPE_STRING to sanitize entered passwords.
Increase Database Resources
If your WordPress site has grown significantly in size, the MySQL server may be exhausting its allocated resources. Too many open connections or memory usage can cause stability issues.
Consider upgrading to a larger MySQL server with more connections available. Or optimize queries and indexes to reduce load.
Managed WordPress hosts can easily allocate more resources if MySQL limitations are causing the lost connection.
Refresh Database Connection
Sometimes simply refreshing the connection between WordPress and MySQL will resolve any temporary errors.
- Try manually reconnecting by adding this code to your wp-config.php file below the database credentials:
$mysql = mysqli_init();
$mysql->real_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME );
- Also, use a plugin like WP DBManager to reset the connection via the dashboard.
Refreshing the link between WordPress and MySQL often clears up any transitory issues.
Troubleshoot Caching and Optimization Plugins
Advanced WordPress plugins related to caching, image optimization, migrations, etc can sometimes incorrectly close database connections.
- If issues appear after installing an optimization plugin, try temporarily disabling it to see if that fixes the problem. Narrow down which plugin may be responsible.
- You can then tweak that plugin’s settings or switch to an alternative to avoid database connection disruptions.
Switch MySQL API
WordPress can use a couple different APIs to connect to MySQL:
- mysqli – The improved MySQL extension API.
- mysql – The original MySQL API for PHP.
By default, WordPress uses mysqli, but trying a switch to the mysql API could potentially resolve stubborn “gone away” errors.
To change the API, edit wp-includes/wp-db.php and replace every instance of mysqli with mysql. This updates database calls to utilize the legacy mysql API.
Update WordPress and Plugins
Outdated WordPress core files, themes, and plugins can sometimes contribute to connection issues with MySQL.
- Make sure you have updated to the latest versions across your entire WordPress installation. Old code containing optimization gaps or bugs could be a culprit.
- Use the built-in update features in the WordPress dashboard to refresh your installations. Updates often include crucial database fixes.
Refresh Permalinks
An easy step you can try – go to Settings > Permalinks and simply click “Save Changes” without altering anything.
This re-registers your permalink rules and can help realign paths between WordPress and the database if there are any issues.
Switch to In-Memory Caching
Some WordPress caching plugins like W3 Total Cache offer an in-memory caching mode. Instead of caching to disk, pages are served from memory.
In-memory caching reduces load on the database which may help avoid overwhelmed resources and dropped connections.
This likely requires bumping up the PHP memory limit as well. Check with your host on available resources for in-memory caching.
Contact Your Web Host
If you’ve tried all other troubleshooting tips with no success, reach out to your web hosting provider.
They have the most visibility into potential issues with the MySQL server software and hardware resources powering your database.
The hosting provider may spot and resolve configuration problems or identify needs for upgraded infrastructure.
With their help tracking down the root cause, the MySQL connection can hopefully be restored.
Conclusion
The “MySQL server has gone away” error can appear confusing at first. But armed with these troubleshooting techniques, you can methodically track down the source of the dropped database connection in WordPress.
Check user credentials, increase timeouts, refresh the link, disable problematic plugins, update core files, and don’t hesitate to lean on your web host for database support.
With persistence, the connection can be repaired and your WordPress site will be back online and running smoothly once again. Let us know if any other fixes worked for you!