MySQL & MariaDB error 2006: MySQL server has gone away
MySQL server has gone away (5.6.51) · Server has gone away (10.6.28) The client tried to use a connection the server had already closed. Usually an idle timeout, a packet bigger than max_allowed_packet, or a server restart.
| MySQL | MariaDB | |
|---|---|---|
| Symbol | CR_SERVER_GONE_ERROR | CR_SERVER_GONE_ERROR |
| SQLSTATE | HY000 | HY000 |
| Type | Client library error | Client library error |
| Versions | every release since 5.0 active | since 10.2.44 active |
What it means
The connection was closed before the client sent its statement (or while sending it). The client only finds out when it tries to write, hence “has gone away”. 2013 is the same family, raised when the drop happens while waiting for a result.
Why it happens
- Idle timeout: the connection was unused for longer than
wait_timeout(default 28,800 seconds, often lowered by hosting providers to 60–300). MySQL 8.0.24+ reports this case as 4031 when it can. - Packet too large: a single statement or row bigger than
max_allowed_packet. MySQL 8.0+ clients now report this case clearly as 1153; MySQL 5.x and MariaDB still show 2006, as our tests confirm (4 MB by default on MySQL 5.6/5.7, 64 MB on 8.0, 16 MB on MariaDB). The server drops the connection instead of reading it: typical with big BLOBs, multi-row INSERTs and mysqldump restores. See 1153. - The server was restarted or crashed in between.
- A firewall or NAT gateway silently dropped an idle TCP connection.
How to fix it
- Large statements: raise
max_allowed_packeton the server (SET GLOBAL max_allowed_packet = 67108864;and inmy.cnf) and in the client (mysql --max-allowed-packet=64M, or the driver setting). For dumps, split them withmysqldump --net-buffer-length/--skip-extended-insert. - Idle connections: reconnect or validate before use (connection pools have a test-on-borrow setting), or raise
wait_timeoutfor that client. - Check the server error log for restarts or crashes.
Reproduce it yourself
These steps are the exact test our harness runs. Use a scratch server, never production.
Connect with
mysql -u root -pand create a scratch database and the test data:CREATE DATABASE demo; USE demo; CREATE TABLE big (b LONGBLOB);As an admin, lower the limit to 1 MB (new connections pick it up).
SET GLOBAL max_allowed_packet = 1048576;Reconnect and send a statement of about 3 MB, for example from a shell:
{ printf "INSERT INTO big VALUES ('"; head -c 3000000 /dev/zero | tr '\0' a; printf "');\n"; } | mysql -u root -p demoPut the limit back afterwards (64 MB is the MySQL 8.0 default).
SET GLOBAL max_allowed_packet = 67108864;
You get: ERROR 2006 (HY000) at line 1: Server has gone away(11.8.9)
Verified fixes
Raise max_allowed_packet above the largest row or statement you send
Raise the server limit (and put it in my.cnf under [mysqld] so it survives a restart).
SET GLOBAL max_allowed_packet = 67108864; -- 64 MBReconnect with a matching client limit and send the statement again.
mysql -u root -p --max-allowed-packet=64M demo < big-insert.sql
Tested on
| Step | MySQL | MariaDB | |||||||
|---|---|---|---|---|---|---|---|---|---|
| 5.6 | 5.7 | 8.0 | 8.4 | 9 | 10.6 | 10.11 | 11.4 | 11.8 | |
| Send a statement bigger than max_allowed_packet (the server drops the connection) repro | ✓ 2006 | ✓ 2006 | ✓ 1153 | ✓ 1153 | ✓ 1153 | ✓ 2006 | ✓ 2006 | ✓ 2006 | ✓ 2006 |
| Raise max_allowed_packet above the largest row or statement you send fix | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
Related errors
How the message changed between versions
| MySQL from | Symbol | Message |
|---|---|---|
| 5.0.96 | CR_SERVER_GONE_ERROR | MySQL Server nicht vorhanden |
| 5.5.63 | CR_SERVER_GONE_ERROR | MySQL server has gone away |