// MySQL error reference

MySQL error 2013: Lost connection to MySQL server during query

The connection dropped while a query was running or results were being read: a server timeout, a killed query, a crash, or a network or proxy timeout.

Error number
2013
Name
CR_SERVER_LOST
Category
Connection and client
Error message
Lost connection to MySQL server during query

What it means

2013 is raised by the client when the connection breaks in the middle of a query. The server may have finished the statement, partly run it, or never started it, so you can't assume either way; for writes, check the data before retrying.

Variants of the message add at 'reading initial communication packet' (the server closed the connection right after accepting it) or at 'reading authorization packet', which point at connection setup rather than the query.

Common causes

  • The query ran longer than a client, proxy or load-balancer read timeout.
  • Sending a result took longer than net_write_timeout, for example when the client reads a big result slowly.
  • The query was killed (KILL, max_execution_time) or the server crashed or restarted; check the server error log.
  • During connection setup: the server rejected the host, or connect_timeout was too short on a slow network.

How to fix it

  1. Look at the server error log and SHOW PROCESSLIST to see whether the server restarted or the query is still running.

  2. Raise the timeouts that apply to long queries and slow clients.

    SHOW VARIABLES LIKE 'net_%_timeout';
    SET GLOBAL net_read_timeout = 120;
    SET GLOBAL net_write_timeout = 120;
  3. Raise the read timeout on the client side too (your driver's socket or read timeout, and any proxy in between).

  4. Make the query faster or smaller: add indexes, page through big results, or batch large writes.

Related errors

More connection and client errors

The code and message text are from the MySQL 8.4 Error Message Reference. Browse the full MySQL error list or search every database error.

// fewer errors, faster fixes

Work on MySQL in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for MySQL and the other databases you use.