Incident Response

MySQL Error 2013 "Lost Connection to MySQL Server During Query": How to Fix It

Error 2013 means the connection died while the client was waiting for an answer. The cause is almost never the query text: it is a server restart, a timeout on a slow transfer, a proxy that cut an idle socket, or a packet that was too big. Here is how to tell which one in a few minutes.

· 13 min read · AG Data Team

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;
  1. Did the server restart? A small Uptime means 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.
  2. 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 raising connect_timeout if it was set to only a few seconds (Lost connection to MySQL server). The default is 10 seconds.
  3. 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

-- 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:

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:

Key takeaways

MySQL Errors Timeouts Networking
Work with us

Lost connections breaking jobs or dumps?

We trace MySQL connection drops through the server, proxies and network, fix timeouts and crashes, and make long jobs and backups finish reliably.

Get in touch More from the blog