You may encounter the Error 2006 (HY000): MySQL server has gone away' when executing the queries. The error usually occurs when the server has timed out or closed the connection. Sometimes, the queries may fail to execute due to corruption in database, causing such an error. In this article, we’ll explore the causes of this MySQL error and how to fix error MySQL server has gone away effectively.
Causes of MySQL Server has Gone Away Error
The Error 2006 (HY000): MySQL server has gone away can occur due to several reasons. Some of them are:
- MySQL database is corrupted or damaged.
- The queries you are trying to execute are corrupted.
- You might be trying to execute large-sized or unorganized queries.
- Running thread has been killed with the mysqladmin kill command.
- Connection between the server and client systems is lost due to network issues.
- Server has closed the connection as it has been idle for too long.
- All existing connections are lost due to incorrect system shutdown.
- Packet size issues.
Solutions to Fix MySQL Server has Gone Away Error
First, you need to verify whether MySQL Server is running or not. For this, you can use the mysqladmin command as given below:
$> bin/mysqladmin version
$> bin/mysqladmin variables
If you fail to connect to the server, run the below command to specify the –u root to connect as root:
$> bin/mysqladmin -u root -p version
Enter password: (enter root password here)
If this doesn’t work, then follow the below solutions to resolve the error.
Solution 1: Check the Wait-Timeout Settings
The MySQL server has gone away error can occur when the query you are executing fails due to connection failure. The connection usually fails if the server has not been active for the duration set in the wait-timeout settings. In such a case, you can check and increase the wait_timeout value by following these steps:
- Locate your system's configuration file (my.cnf).
- Search for the [mysqld] section and check the wait_timeout value. By default, the value is set to 28800, i.e., 8 hours. MySQL Server closes the connection after this duration, if the system is idle.
- Increase the timeout value and save the changes.
- Now, restart the MySQL Server to apply the changes.
Solution 2: Increase max_allowed_packet Value
The "MySQL Server has gone away" error can also appear when the command you are executing is large, which requires complex and multiple operations. In such a case, you can increase the max_allowed_packet value. To do this, you can follow the below steps:
- Go to MySQL configuration file on your system.
- Find the max_allowed_packet settings under the [mysqld] section.
- Check and increase the max_allowed_packet value to a larger size. You can change it up to 1 GB.
- Save the applied changes.
Solution 3: Check the Network Issues
You can also encounter the "MySQL server has gone away" error when the MySQL Server drops the connection due to network problems. Make sure your network is reliable. You can check the network logs to find out the cause. The logs are located in the MySQL data directory. Here's the path:
C:\ProgramData\MySQL\MySQL Server [version number]\Data\
You can run the mysql_ping() command to check whether the server connection is working or not.
int
mysql_ping(MYSQL *mysql)
Solution 4: Repair MySQL Database
MySQL database corruption is also one of the reasons due to which queries fail to respond in MySQL, leading to such errors. If you have a dump file, you can use the mysqldump utility to restore the MySQL database. Here is the command:
mysql -u root -p db_name < dump.sql
If you do not have dump file, then you can repair the MySQL database using the REPAIR TABLE command. Here is the command:
REPAIR TABLE tablename [options]
This command supports MyISAM tables.
If you’re using InnoDB storage engine, you can perform force InnoDB recovery to repair corrupt InnoDB tables.
Solution 5: Use a Professional MySQL Repair Tool
If the above repair methods fail to repair the MySQL database, then you can use a professional MySQL database repair tool. Stellar Repair for MySQL is one such tool that can repair corrupt InnoDB and MyISAM tables with complete precision. It recovers all the data, including deleted items, from the MySQL database and saves the data in a new database file. It can also save the repaired data in various other formats, such as MariaDB, SQL Script, CSV, XLS, and HTML. This MySQL repair tool supports repairing of databases on both Windows and Linux systems.
To Conclude
The "MySQL server has gone away" error can occur due to a variety of reasons, like server timeout, network connection issues, corruption in the database, etc. If the error is associated with corruption in the MySQL database, then you can repair it using a professional MySQL repair tool, like Stellar Repair for MySQL. MVPs recommend the tool to recover InnoDB and MyISAM tables effectively and quickly with complete integrity. The tool supports all the versions of MySQL Server, including the latest MySQL version 8.0.36.