mysqldude
Error codes › 2013

MySQL & MariaDB error 2013: Lost connection to MySQL server during query

MySQLLost connection to MySQL server during query
MariaDBLost connection to server during query
Example from our test runs: 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.

MySQLMariaDB
SymbolCR_SERVER_LOSTCR_SERVER_LOST
SQLSTATEHY000HY000
TypeClient library errorClient library error
Versionsevery release since 5.0 activesince 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_timeout and 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_timeout expired while sending a large result or a slow LOAD 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_timeout for that client: SET SESSION wait_timeout = 28800.
  • For big imports or exports raise net_read_timeout, net_write_timeout and max_allowed_packet.
  • For long queries, check max_execution_time and 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.

  1. Connect with mysql -u root -p and create a scratch database:

    CREATE DATABASE demo;
    USE demo;
  2. Session A: start a long-running query.

    SELECT SLEEP(60);
  3. 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 above
  4. Session A reports the lost connection.

You get: ERROR 2013 (HY000) at line 1: Lost connection to server during query(11.8.9)

Verified fixes

  1. 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

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.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
✓✓✓✓✓✓✓✓✓
A query runs longer than the server lets it (here the session is killed mid-query) repro
MySQL 5.6✓ 20135.7✓ 20138.0✓ 20138.4✓ 20139✓ 2013
MariaDB 10.6✓ 201310.11✓ 201311.4✓ 201311.8✓ 2013
The connection sits idle longer than wait_timeout, then sends a query version check
MySQL 5.6✓ 20065.7✓ 20068.0✓ 40318.4✓ 40319✓ 4031
MariaDB 10.6✓ 200610.11✓ 200611.4✓ 200611.8✓ 2006
Raise the timeout for long-running or long-idle sessions fix
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-25 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9

Related errors

  • 2006 CR_SERVER_GONE_ERRORexplained
  • 4031 ER_CLIENT_INTERACTION_TIMEOUTexplained
  • 1153 ER_NET_PACKET_TOO_LARGEexplained
  • 1205 ER_LOCK_WAIT_TIMEOUTexplained