Methods of payment Abuse

Ошибка 2006: MySQL Server has gone away

09.03.2021, 22:01

Error 2006 called MySQL Sever has gone away means that the server has refused to connect even though it is running. There are only three known reasons why the error appears. The first reason is that the server is overloaded. The waiting time has expired. The second reason - the client sent a packet that is too painful. The third reason is that the server has not been fully initialized. Further we will consider in detail the reasons why the error appears and how to deal with it.

How to fix the error

Usually the error appears when trying to connect to the database using PHP, console client, or in the case of using PhpMyAdmin:

PhpMyAdmin

Let's further consider each situation separately.

Expired timeout

As mentioned at the beginning of this article, one of the possible causes is expired waiting time. It may be that the server was overloaded and cannot handle the load - processing all connections. To understand how long server requests take, you can use any console client and connect to the server. If you are able to do so, execute any request. If queries take too much time to process, you can optimize MySQL using a special script MySQLTuner. Usually increase the pool size of the InnoDB engine by setting the innodb_buffer_pool_size parameter. The optimal value is determined using the above script.

If it is 800 megabytes (it can be any other size), set it:

$ sudo vi /etc/mysql/my.cnf
innodb_buffer_pool_size=800M

https://pq.hosting/help/myfiles/mysqlhasgoneavay-991x576.png

There is another way to solve the problem. For this purpose, the response time from the server is increased. To accomplish this task, you need to change the wait_timeout parameter. This is the time in seconds during which you should wait for a response from the server.

For example:

wait_timeout=500

параметр wait_timeout

When making changes, don't forget to restart the server next:

$ sudo systemctl restart mysql

Or:

$ sudo systemctl restart mariadb

Too large a packet

When a user's client creates too many packets, the server will generate this error. The available packet size (maximum value) can be increased using the max_allowed_packet parameter.

For example:

$ sudo vi /etc/mysql/my.cnf
max_allowed_packet=128M

Pay special attention to the client, because if it sends a lot of requests, you are obviously doing something wrong. At least do not generate queries to MySQL using for loops.

The server is incorrectly initialized

If you decide to deploy MySQL or MariaDB in Docker, be prepared to encounter a similar error. The initial initialization of the container requires a little more free time. If you don't let the container complete initialization by stopping and starting it first, the database will always return this error. The solution is to completely remove the container data from the database.

This is done like this:

$ docker-compose down

or:

$ docker rm mysql-container

Next, you need to delete the storage (volume) with the incorrectly initialized database. But at the beginning, look through the list of all storages:

$ docker volume ls

СЕРВЕР НЕВЕРНО ПРОИНИЦИАЛИЗИРОВАН

After deleting:

$ docker volume rm volume_name

Now you can start initializing the application, just wait until the database server tells you that it is ready and you can connect to it.