What it means
Error 2006 comes from the client: it tried to send a statement and found that the server had closed the connection. The statement didn’t run. Nothing is wrong with the statement itself (unless it was too big, below); the connection was gone before it arrived.
The same event has several names, depending on the client and the moment it notices:
| You see | From |
|---|---|
2006 MySQL server has gone away | Many language drivers (PHP, Python, Ruby…) and older mysql clients |
2006 Server has gone away | The MariaDB client |
4031 The client was disconnected by the server because of inactivity | MySQL 8.0.24 and later, sent as it closes an idle connection |
2013 Lost connection to MySQL server during query | The MySQL 8.4 client, for most of the cases below |
If the connection drops while a statement runs, see Lost connection to MySQL server during query.
Common causes
- The connection was idle longer than
wait_timeout, 28,800 seconds (8 hours) by default, or than a proxy’s or load balancer’s idle timeout, which can be much shorter. A connection pool, a long-running script or a laptop that slept keeps a connection the server has since closed. - The statement was bigger than
max_allowed_packet: the server rejects it and closes the connection, and some clients only report the closed connection. See packet bigger than max_allowed_packet. - The connection was killed:
KILL, an administrator ending sessions, or a tool that kills long-idle ones. - The server restarted or crashed, or failed over to another host.
- A firewall, load balancer or NAT dropped an idle TCP connection without telling either side.
- Several processes sharing one connection, such as an application that opens a connection and then forks workers.
How to fix it
Find out which
On the server, check the timeouts and how long it has been up:
SELECT @@wait_timeout, @@interactive_timeout, @@max_allowed_packet;
SHOW GLOBAL STATUS LIKE 'Uptime';
SHOW GLOBAL STATUS LIKE 'Aborted_clients';
An Uptime shorter than the connection’s age means a restart. Aborted_clients counts connections
the server closed without the client saying goodbye, idle timeouts included. The error log can give
the reason for each one (by default on MariaDB; on MySQL with log_error_verbosity at 3):
[Warning] Aborted connection 9 to db: 'shop' user: 'root' host: 'localhost' (Got a packet bigger than 'max_allowed_packet' bytes)
Reconnect, and check connections before use
Applications should expect connections to be closed. Most pools can test a connection before
handing it out, or retire it before the server would: set the pool’s maximum idle time or lifetime
below wait_timeout (and below any proxy’s idle timeout). In a long script, reconnect on 2006 and
2013 and retry the statement if it’s safe to repeat.
Raise wait_timeout, if idle connections should live longer
For one session:
SET SESSION wait_timeout = 28800;
For everyone, an administrator sets it in the configuration (wait_timeout under [mysqld]), or
in a hosted database’s parameter settings. Longer timeouts keep more idle connections open, which
counts against max_connections.
Big statements
If the error comes right after a large INSERT or an import, raise max_allowed_packet or send
smaller statements, as described on the
max_allowed_packet page.
Reproduce it
On the shared servers, as an ordinary account, with the session’s timeout shortened:
SET SESSION wait_timeout = 2;
-- wait 4 seconds
SELECT 2;
The mariadb client, against MariaDB 11.4.13:
ERROR 2006 (HY000): Server has gone away
No connection. Trying to reconnect...
Connection id: 198223
Current database: seo_err_mysql
It then ran the statement on the new connection. The mysql client, against MySQL 8.4.11, got the
server’s own error first, then reconnected the same way:
ERROR 4031 (HY000): The client was disconnected by the server because of inactivity. See wait_timeout and interactive_timeout for configuring this behavior.
No connection. Trying to reconnect...
Run from a script (no reconnect), the statement after that got
ERROR 2013 (HY000): Lost connection to MySQL server during query. The MySQL client also reported
2013, not 2006, when an idle connection was killed with KILL or the server was restarted under it,
and against a MariaDB server’s idle timeout. The MySQL manual notes that which of the two numbers
you get depends on the operating system.
In temporary containers with max_allowed_packet at 1 MB (removed afterwards), a 2 MB INSERT from
the mariadb client gave ERROR 2006 (HY000): Server has gone away for it and the next statement,
while the mysql client showed the underlying 1153.
In Inlet
When the server closes a connection, Inlet shows the server’s message (MySQL’s inactivity error, for instance), or “The server closed the connection” when there wasn’t one, and links to this page. Inlet’s Activity monitor for MySQL and MariaDB lists the server’s sessions, which shows how many idle connections your applications keep open.