MySQL & MariaDB error 2013: Lost connection to MySQL server during query
Lost connection to MySQL server during query (5.6.51) · Lost connection to server during query (10.6.28) The connection dropped while the client was waiting for a reply. The server closed it, a timeout fired, the query was killed, or the server crashed.
| MySQL | MariaDB | |
|---|---|---|
| Symbol | CR_SERVER_LOST | CR_SERVER_LOST |
| 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 client sent a statement and the connection closed before the result arrived. 2013 means “it broke during a query”; 2006 means the connection was already gone when the next query was sent. The causes overlap, and the server’s error log usually tells you which one it was.
Why it happens
- The query was killed:
KILL,max_execution_time(MySQL),max_statement_time(MariaDB), or a proxy or load balancer idle timeout. - The connection sat idle longer than
wait_timeoutand the server closed it; the next query fails. In our tests that case returns 4031 on MySQL 8.0+ and 2006 on MySQL 5.x and MariaDB. net_read_timeout/net_write_timeoutexpired while sending a large result or a slowLOAD DATA.- A statement larger than
max_allowed_packet: see 1153. - The server crashed or was restarted (out of memory, an assertion); check the error log for
mysqld got signal.
How to fix it
- Check the server error log first: it records crashes, killed connections and packet-size aborts.
- For long-idle connections (pools, cron scripts), use a pool with connection validation, or raise
wait_timeout/interactive_timeoutfor that client:SET SESSION wait_timeout = 28800. - For big imports or exports raise
net_read_timeout,net_write_timeoutandmax_allowed_packet. - For long queries, check
max_execution_timeand any proxy timeouts, or make the query faster.
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:CREATE DATABASE demo; USE demo;Session A: start a long-running query.
SELECT SLEEP(60);Session B: find it and kill it (a proxy timeout or max_execution_time does the same).
SELECT id FROM information_schema.PROCESSLIST WHERE info = 'SELECT SLEEP(60)'; KILL 123; -- the id from the query aboveSession A reports the lost connection.
You get: ERROR 2013 (HY000) at line 1: Lost connection to server during query(11.8.9)
Verified fixes
Raise the timeout for long-running or long-idle sessions
Give the session (or the pool's connections) a longer idle timeout, or reconnect/validate before use.
SET SESSION wait_timeout = 28800; -- 8 hours
Tested on
| Step | MySQL | MariaDB | |||||||
|---|---|---|---|---|---|---|---|---|---|
| 5.6 | 5.7 | 8.0 | 8.4 | 9 | 10.6 | 10.11 | 11.4 | 11.8 | |
| A query runs longer than the server lets it (here the session is killed mid-query) repro | ✓ 2013 | ✓ 2013 | ✓ 2013 | ✓ 2013 | ✓ 2013 | ✓ 2013 | ✓ 2013 | ✓ 2013 | ✓ 2013 |
| The connection sits idle longer than wait_timeout, then sends a query version check | ✓ 2006 | ✓ 2006 | ✓ 4031 | ✓ 4031 | ✓ 4031 | ✓ 2006 | ✓ 2006 | ✓ 2006 | ✓ 2006 |
| Raise the timeout for long-running or long-idle sessions fix | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |