Назад к списку

How Do I Fix the Error “MySQL Server Has Gone Away”?

When using databases, various issues in functionality can arise. For instance, you might encounter a common error – "MySQL server has gone away." It's quite unpleasant to discover this error during your work, but it's a manageable problem. If you've encountered this error, you must understand what it means and how to resolve it. Sometimes, addressing such malfunctions requires changing your system's configuration or the app that initiates the connection to the database server. 

 In the article, DB Serv experts will thoroughly acquaint you with the issue of the "MySQL has gone away" error in databases. We'll discuss the most common reasons for its occurrence and provide straightforward examples of solving this error.

What Is the MySQL 1045 Error?

The MySQL server has gone away error notifies you that the response timeout has expired and the database server has closed the connection. It can occur for various reasons, which we will discuss further. Now, let's delve into the algorithm of its occurrence. 

You send a query to connect to the MySQL server, and by default, you're given eight hours (28,800 seconds) to receive a response. When the timeout expires, the system initiates a disconnection. Note that sometimes, your web hosting provider or database administrator might have set a shorter timeout in the settings. So, the first step is to check this value. If everything is fine there, you need to search for other causes of the MySQL server has gone away problem. 

Keep in mind that this malfunction might be recorded differently in the MySQL database server's error logs. For example: 

   ⚫️ General error: 2006 MySQL server has gone away. 

   ⚫️ Error Code: 2013. Lost connection to MySQL server during query. 

   ⚫️ Warning: Error while sending QUERY packet. 

The registration of an error indicating connectivity issues happens in various ways. Sometimes, it can be a sign of more profound problems in the system. Therefore, the "server has gone away" notification in the database might indicate a failure in a connected third-party application or service. In this case, you'll need to examine all error logs with a focus on the time of its occurrence to determine whether it's a failure in another application or, specifically, on your database server.

What Are the Causes of the MySQL has Gone Away Error? 

When a failure occurs, the first thing you need to do is determine the cause. Based on that, you can select the best solution and ensure the persistence of your MySQL database going forward.

Your connection can be interrupted in several cases. The most common ones include: 

   ⚫️ Timeout Expired: This is the most prevalent cause. It's possible that your MySQL settings for `wait_timeout` contain a low value. The maximum time the server will wait for activity is 28,800 seconds. However, your value might be lower. Importantly, if you have a low number of seconds but a stable connection, it signifies the high efficiency of your MySQL database.

   ⚫️ File Size Exceeded: You might see the error message "error 2006 hy000 MySQL server has gone away". This kind of issue indicates that the SQL import file is large or contains a lot of information, leading to exceeding the `max_allowed_packet` parameter. In such cases, the system assumes that there are problems on the client side, causing an interrupted connection.

   ⚫️ Insufficient Server Space: A straightforward reason behind the "2006 MySQL server has gone away" error. In this situation, it's better to seek help from qualified database administrators who can promptly fix the issue and scale your server.

   ⚫️ Exceeded MySQL max_connections Settings. This value determines the acceptable number of simultaneous client connections. Doubling the connections might resolve the error. For example, if the maximum client connections throughout uninterrupted work were 100, you can increase the `max_connections` setting to 200. However, setting this value too high could lead to memory exhaustion, so be cautious.

   ⚫️ Remote Connections Exist: In some cases, the "MySQL has gone away" error might point to a deeper problem. When your database uses remote connections to third-party services, it can cause additional issues. For instance, failures might occur due to damaged session tables in an external plugin for payment processing or a similar resource. 

We've listed the main causes affecting MySQL database persistence, resulting in the "server has gone away" message. 

How To Fix 2006 MySQL Server Has Gone Away Error

Troubleshooting issues related to the "server has gone away" error in MySQL databases can be approached in several ways. We've gathered the most effective and straightforward options to help you reconnect to the server.

Increase Response Timeout 

To recover the connection to the server, find the `wait_timeout` variable in the my.cnf configuration file. For better results, check the following values: `net_read_timeout`, `net_write_timeout`, and `Interactive_timeout`. Modify them if needed and add lines that suit your requirements. For example, `wait_timeout=120`, `net_read_timeout=100`, `net_write_timeout=150`, `Interactive_timeout=300`, `connect_timeout=120`.

Increase Data Transfer Volume 

When the cause of the "MySQL has gone away" error is transferring an excessively large or incorrect file, you need to increase the packet size. Go to the my.cnf file to change existing settings and set the desired amount of data in bytes. For example: `max_allowed_packet = 128M`. After that, restart your MySQL server using the command `sudo /etc/init.d/mysql restart`.

In some cases, you might also need to increase the value of the MySQL `innodb_log_file_size` variable in the my.cnf configuration. Remember that for proper operation, this size should be at least 20% of the `innodb_buffer_pool_size`, ideally 25%. Generally, the larger this variable, the more time you'll need to recover the database in case of failure.

Important: Before making changes to `innodb_log_file_size` and `innodb_buffer_pool_size`, you must stop MySQL. Otherwise, you risk losing all data or triggering a critical database failure. 


Issues in MySQL, such as the "server has gone away" error, are quite common and have simple solutions. The first widespread cause is the expiration of the response timeout. When the server doesn't receive activity, it triggers a disconnection. To resolve the error, it's sufficient to increase the timeout.

The second reason for encountering the "2006 MySQL server has gone away" error is a large import file size into the database. You might also see the "server has gone away" message when the server itself exceeds its memory limit. In these situations, my.cnf files come to your aid, allowing you to adjust the necessary settings independently. 

Regular technical checks of all components are essential to ensure the highly efficient and persistent functioning of your database. This will help you promptly detect issues and make necessary adjustments to packet files, thus preventing the emergence of errors. 

The team of professional database administrators is always ready to assist you and provide comprehensive guidance in resolving errors in the MySQL database.

DBserv  >  Blog  >  How Do I Fix the Error “MySQL Server Has Gone Away”?
Quick Links: Blog Contacts Prices
© 2008-2024. Sitemap