What it means
Error 2013 comes from the client: it sent something, was waiting for the reply, and the connection closed instead. The server didn’t send an error first, so the client can’t tell you why. Whether the statement ran is unknown: a write may have been committed moments before the connection dropped.
The text after “Lost connection to MySQL server” says when it happened:
during query: while a statement ran, or on the first statement after the server had dropped the connection. The MySQL 8.4 client reports most dropped connections this way, including the ones older clients called “MySQL server has gone away”.at 'reading initial communication packet'orat 'waiting for initial communication packet': before sign-in. Something accepted the TCP connection but didn’t answer as a MySQL server would.system error: 110means it timed out waiting.
The MariaDB client leaves out “MySQL”: Lost connection to server during query.
Common causes
- The query was killed: an administrator, a monitoring tool or a script ran
KILLon it. - A timeout between you and the server: a load balancer, proxy, VPN or firewall that cuts connections after a fixed time, or the client driver’s own read timeout, shorter than the query.
- The server restarted or crashed during the query, for example when the operating system ran out of memory and stopped it.
- A statement or row larger than
max_allowed_packet: the server (or the client) gives up on the connection. - The connection had already been closed for idleness, and this was the next statement.
- For the handshake form: the wrong port, a service that isn’t MySQL, or an SSH tunnel whose far end can’t reach the database, so the tunnel accepts and then closes.
How to fix it
See whether the server restarted or killed it
SHOW GLOBAL STATUS LIKE 'Uptime';
SHOW GLOBAL STATUS LIKE 'Aborted_clients';
A short Uptime means a restart; look at the server’s error log, and on Linux at the system log for
an out-of-memory kill. Ask whoever runs the server whether something kills long queries.
Make the query shorter, or let it run longer
A query that’s cut off at the same duration every time is meeting a timeout. Make the query faster
(EXPLAIN it, add the index it needs, work in batches), or raise the timeout that stops it: the
driver’s read timeout, the proxy’s or load balancer’s idle timeout. The server’s own
net_read_timeout (30 s) and net_write_timeout (60 s) apply while it’s waiting to receive or send
data, such as a large import or a client reading rows slowly.
Big rows and statements
If it happens on a statement with a large value, or when reading one, raise max_allowed_packet on
both ends; see packet bigger than max_allowed_packet.
Retry carefully
Reconnecting is safe; repeating the statement may not be. An INSERT or a payment that was
committed moments before the connection dropped would run twice. Check, or make the statement safe to
repeat (an upsert with a unique key), before retrying.
At reading initial communication packet
Check that the host and port are a MySQL or MariaDB server: nc -vz <host> <port> only proves
something listens. Common culprits are a Docker port mapped to the wrong container port, another
service on that port, and an SSH tunnel whose remote end points at the wrong host or port. (MySQL’s
X Protocol port, 33060, gives a different error with the classic client:
ERROR 2007 (HY000): Protocol mismatch; server version = 11, client version = 10.)
A server that’s very slow to accept connections shows this too. The MySQL manual suggests checking
SHOW GLOBAL STATUS LIKE 'Aborted_connects', which counts connection attempts the server gave up
on, and making sure connect_timeout (10 seconds by default) isn’t set lower.
Reproduce it
On MySQL 8.4.11, a query running in one session and KILL <id> from another:
SELECT SLEEP(10);
ERROR 2013 (HY000): Lost connection to MySQL server during query
The MariaDB 11.4.13 client said ERROR 2013 (HY000): Lost connection to server during query for
the same thing on MariaDB.
The mysql client connecting to ports that aren’t MySQL: a Redis server, which waits for the client
to speak first (with --connect-timeout=3), and an SSH server, which sends its own greeting:
ERROR 2013 (HY000): Lost connection to MySQL server at 'waiting for initial communication packet', system error: 110
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0
The mariadb client to the same ports:
ERROR 2013 (HY000): Lost connection to server at 'handshake: reading initial communication packet', system error: 110
ERROR 2013 (HY000): Lost connection to server at 'handshake: reading initial communication packet', system error: 11
The MySQL 8.4 client also gave during query on the first statement after an idle connection was
killed, after the session’s wait_timeout passed (following MySQL’s 4031 error), after the server
restarted, and after a 2 MB statement to a server with a 1 MB max_allowed_packet.
In Inlet
When a connection drops, Inlet reports it in its own words (“The connection to the server was
lost”, with the system’s reason) and links to this page; a port that doesn’t answer as MySQL gets
“This doesn’t look like a MySQL or MariaDB server. Check the port.” A long query can be stopped
with Cancel in the query editor, and Inlet’s Activity monitor lists the server’s sessions. If the
database is only reachable through a bastion host, Inlet’s SSH tunnels (Pro) use the system ssh
and your ~/.ssh/config.