How to Fix MySQL Server Has Gone Away in WordPress
The “MySQL server has gone away” error is a common database issue in WordPress.
It usually appears when WordPress tries to execute a query but loses connection with the MySQL server.
You may see errors like:
MySQL server has gone away
Error establishing database connection
This error can make your website slow or completely inaccessible.
What Causes This Error?
1. Large Database Queries
Heavy queries may exceed server limits.
2. Low MySQL Timeout
If the server takes too long, MySQL closes the connection.
3. Small max_allowed_packet Size
Large data packets may be rejected.
4. Server Resource Limits
Low memory or CPU can crash MySQL.
5. Corrupted Database Tables
Damaged tables may break connections.
Step 1: Increase max_allowed_packet
Edit MySQL config:
/etc/mysql/my.cnf
Add:
max_allowed_packet=64M
Restart MySQL:
sudo systemctl restart mysql
Step 2: Increase wait_timeout
Add:
wait_timeout=600
This prevents connection timeout.
Step 3: Repair Database
Use phpMyAdmin or run:
mysqlcheck -u root -p --auto-repair --optimize --all-databases
Step 4: Optimize Database
Clean unnecessary data:
- post revisions
- spam comments
- transient data
Step 5: Increase PHP Limits
Update:
max_execution_time = 300
memory_limit = 256M
Step 6: Restart MySQL Server
sudo systemctl restart mysql
Step 7: Check Server Load
Use:
top
or
htop
Upgrade server if needed.
Best Hosting for Database Stability
Reliable hosting is critical for database performance.
👉 Cloudways Managed Hosting

👉 Bluehost Hosting
They offer optimized MySQL servers and better performance.
Preventing MySQL Errors
Optimize Database Regularly
Use Caching (Redis/Memcached)
Monitor Queries
Upgrade Hosting
Conclusion
The MySQL server has gone away error occurs due to:
- large queries
- timeout settings
- server limits
By adjusting MySQL configuration and optimizing your database, you can resolve this issue effectively.
