How to Fix Error 2006: MySQL Server Has Gone Away

How to Fix Error 2006: MySQL Server Has Gone Away

Encountering the ‘Error 2006: MySQL Server Has Gone Away’ can be a disconcerting experience for many database administrators and developers. Often striking without warning, this error can disrupt database operations and bring your applications to a standstill. Understanding the underlying causes and knowing how to effectively address them is crucial. In this guide, we’ll delve deep into the root causes of this infamous MySQL error and provide actionable solutions to get your database running smoothly once again.

Fixing Error 2006: MySQL Server Has Gone Away

Here’s a step-by-step guide to fix this issue:

#1 Check Server Status

Verify that the MySQL server is running. If the server isn’t running, the client won’t be able to connect.

sudo systemctl status mysql

For the output, you’re looking for an “Active” status. If it’s “Inactive” or “Failed”, that’s a potential reason for the error.

#2 Increase ‘wait_timeout’ and ‘max_allowed_packet’ Values

These settings in MySQL configuration determine how long the server waits before closing a non-responsive connection and the maximum size of a packet that can be sent to the server, respectively.

  • Edit the MySQL configuration file.
sudo nano /etc/mysql/my.cnf
  • Add/Modify under [mysqld].
wait_timeout=28800
max_allowed_packet=128M
  • Save and exit. For restarting MySQL run this command.
sudo systemctl restart mysql

#3 Check for Crashed Tables

Corrupt or crashed tables can cause connection issues. Run this command:

mysqlcheck -u root -p --all-databases

For the output, you will see a status for each table. Look for any that say “corrupt” or “crashed”.

#4 Review MySQL Error Logs

The logs can give a more in-depth look into any underlying issues causing the server to disconnect.

sudo tail -50 /var/log/mysql/error.log

On the output, look for any recent or recurring errors that might hint at the root cause.

#5 Monitor Server Resources

Insufficient resources can cause the MySQL server to become unresponsive.

top

For the output, review the %CPU and %MEM columns, particularly for the mysqld process. High resource usage might indicate resource constraints.

#6 Verify Disk Space

If the server’s disk is near or at capacity, MySQL might not operate correctly.

df -h

Review available space on the disk, especially for the partition where MySQL data is stored (typically /var/lib/mysql).

#7 Confirm Stable Network Connectivity

For remote MySQL connections, ensure there’s no network interruption between the client and server.

ping -c 5 <MySQL_SERVER_IP>

You should see replies from the server IP with minimal or no packet loss.

#8 Adjust Open Files Limit

MySQL can sometimes exceed the allowed open files limit of the system.

  • Edit the MySQL configuration.
sudo nano /etc/mysql/my.cnf
  • Add/Modify under [mysqld].
open_files_limit=5000
  • Save, exit, and restart MySQL.

After following these steps, try your operation again. If the error persists, you may need to delve deeper, considering factors like firewall configurations, specific application queries, or even potential bugs in the MySQL version in use.

Conclusion

Searching for a managed database solution? Choose Gcore Managed Database for PostgreSQL so you can focus on your core business while we manage your database.

  • 99.9% SLA for uninterrupted service with high-availability architecture
  • Adjustable database resources for changing demands
  • Currently in free public beta

Start managing your database

Subscribe to our newsletter

Stay informed about the latest updates, news, and insights.