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
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_timeoutwas too short on a slow network.
How to fix it
-
Look at the server error log and
SHOW PROCESSLISTto see whether the server restarted or the query is still running. -
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;
-
Raise the read timeout on the client side too (your driver's socket or read timeout, and any proxy in between).
-
Make the query faster or smaller: add indexes, page through big results, or batch large writes.
Related errors
2006MySQL server has gone away2055Lost connection to MySQL server at stage1159Got timeout reading communication packets3024Maximum statement execution time exceeded08006PostgreSQL: connection failureORA-03113Oracle: end-of-file on communication channel
More connection and client errors
2002Can't connect through socket2003Can't connect to MySQL server on host2005Unknown MySQL server host2026SSL connection error2059Authentication plugin cannot be loaded2061Authentication plugin reported error1251Client doesn't support auth protocol3159Insecure transport prohibited
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.
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.