MySQL error 2013, "Lost connection to MySQL server during query", is a client error: the connection broke while the client was waiting for a result. The server did not send an error back, the socket just went away. That is why the message is so unhelpful, and why the fix depends entirely on finding who closed the connection: the server, something in between, or the client itself.
ERROR 2013 (HY000): Lost connection to MySQL server during query
-- variants of the same client error family
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading authorization packet', system error: 0
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0
In the client error reference, 2013 is CR_SERVER_LOST; the related 2055 (CR_SERVER_LOST_EXTENDED) adds the stage and the system error, and 2006 is "MySQL server has gone away" (client error reference). In practice 2006 usually shows up when a client sends a query on a connection that was already closed, and 2013 when the connection dies while a query is running. We covered 2006 in MySQL server has gone away; many causes overlap.
First five minutes: who closed the connection?
-- did the server restart? (Uptime in seconds)
SHOW GLOBAL STATUS LIKE 'Uptime';
-- connections that died mid-session vs. failed handshakes
SHOW GLOBAL STATUS LIKE 'Aborted_c%';
-- the timeouts that most often end a connection
SELECT @@global.net_read_timeout, @@global.net_write_timeout,
@@global.wait_timeout, @@global.connect_timeout,
@@global.max_execution_time, @@global.max_allowed_packet;
- Did the server restart? A small
Uptimemeans mysqld restarted, and every running query got 2013. Look at the error log and the kernel log for a crash or an OOM kill. This is the first thing to rule out, because no timeout tuning will help. - Is it a handshake problem? Errors that mention "reading authorization packet" or "reading initial communication packet" happen during connect. The manual suggests checking
Aborted_connects, which increases for each connection attempt the server aborts, and raisingconnect_timeoutif it was set to only a few seconds (Lost connection to MySQL server). The default is 10 seconds. - Is it "during query"? The manual says this usually points to network trouble, or to very large result sets being transferred (MySQL manual). Then it is a timeout question: which side stopped waiting first.
# server side: crash, OOM kill or "Aborted connection" lines
grep -Ei 'aborted connection|ready for connections|signal' /var/log/mysql/error.log | tail -50
journalctl -k | grep -i -E 'out of memory|killed process' | tail
When a connected client disconnects improperly or is terminated, the server increments Aborted_clients and writes an "Aborted connection" message to the error log (communication errors). The message includes the connection id, user and host, so you can match it to the failing job. In our experience it is the most useful single line in the investigation.
The timeouts that end connections
net_read_timeout, default 30 seconds: how long the server waits for more data from a connection before aborting the read. The manual recommends raising it to 60 seconds or more when millions of rows are transferred (MySQL manual).net_write_timeout, default 60 seconds: how long the server waits for a block to be written to the connection (server system variables). If the client reads a big result slowly, the server gives up writing.wait_timeout: how long an idle non-interactive connection may sit before the server closes it. In our experience it causes 2006 on the next query more often than 2013.max_execution_time, default 0 (disabled): a limit for read-onlySELECTstatements, in milliseconds. When it fires, the client gets server error 3024ER_QUERY_TIMEOUT, "maximum statement execution time exceeded" (server error reference), not 2013. If you see 2013, it was not this limit.max_allowed_packet: the manual listsBLOBvalues larger than this limit as a cause of 2013 with some clients. AnER_NET_PACKET_TOO_LARGEerror confirms it.
-- long transfers of big result sets, only for this session or job
SET SESSION net_read_timeout = 600;
SET SESSION net_write_timeout = 600;
Raise these per session for the job that needs them, not globally. A global net_write_timeout of an hour also means a dead client keeps its thread and locks for an hour.
The query is usually innocent. If the same statement sometimes works and sometimes fails with 2013, look at what changes between runs: result size, client speed, network path, server memory. Rewriting the SQL rarely helps unless it makes the result smaller.
Proxies, load balancers and firewalls
Anything stateful between the application and MySQL can cut a connection that is quiet for too long. While a long query runs and has not started returning rows, the connection carries no data at all. Examples:
- An AWS Network Load Balancer stops tracking a TCP flow after its idle timeout, 350 seconds by default (configurable from 60 to 6000 seconds for TCP listeners), and answers later packets with a TCP RST. TCP keepalive packets reset the idle timer (AWS NLB documentation).
- Firewalls, NAT gateways and connection-tracking tables behave similarly with their own limits.
- ProxySQL, MySQL Router and HAProxy have their own client and server timeouts. In our experience a proxy timeout lower than the longest report query is one of the most common causes of "random" 2013 errors that only certain jobs see.
The tell-tale sign is a constant duration: if failing queries always die at almost exactly the same elapsed time, something in the path has a timer. Compare that number with every timeout you can find.
2013 during mysqldump
mysqldump enables --opt by default, which includes --quick: rows are retrieved one at a time instead of being buffered in memory first (mysqldump). That is what you want for large tables, but it means the server is writing to the dump for as long as the dump runs. If the output goes through a slow pipe, for example compression on a busy host or a slow network share, the server can wait longer than net_write_timeout and drop the connection. In our experience this is the most common source of 2013 on big dumps, and raising the session timeout for the dump user, or writing to a fast local disk first, fixes it. For very large databases physical backups avoid the problem altogether, see mysqldump vs XtraBackup.
What changes on large databases
On a small test database these queries finish in milliseconds and the error never appears. On production:
- Big results take minutes to transfer. A report that exports tens of millions of rows spends most of its time streaming, so
net_write_timeoutand client read speed matter more than the query plan. Paginate by primary key ranges instead of one huge result. - Long silent phases. Large sorts,
GROUP BYon big tables andALTER TABLEcan run for many minutes before sending anything. That is exactly when idle timeouts in proxies and load balancers fire. Enable TCP keepalive on the client or raise the proxy timeouts for these jobs. - Memory pressure. Many concurrent heavy queries can push mysqld into the kernel OOM killer. The symptom is 2013 for everybody at the same moment and a reset
Uptime. Check the buffer pool size and per-connection buffers against RAM rather than raising timeouts. - Lost work is expensive. When a 40-minute batch dies at minute 39, retrying from scratch doubles the load. Make long jobs resumable in chunks so a broken connection costs one chunk.
- Replicas and backups. A dump or export running against a replica competes with replication. If the replica is also restarting or lagging, 2013 on the backup job can be a symptom of a replica that is in trouble, not of the backup.
Key takeaways
- 2013 is a broken connection, not a SQL error. Find out who closed it before changing anything.
- Check
Uptime, the error log and the kernel log first: a crash or OOM kill explains everything at once. - "reading authorization packet" points to
connect_timeoutand the network; "during query" points to big transfers and timeouts. - Defaults:
net_read_timeout30 s,net_write_timeout60 s,connect_timeout10 s. Raise them per session for the jobs that need it. - Failures at a constant elapsed time mean a proxy, load balancer or firewall timer. On AWS NLB the default idle timeout is 350 s.