Skip to content
MySQL

MySQL Error 2013

Lost connection to MySQL server during query

Applies to: MySQL client programs and libmysqlclient for MySQL 5.7, 8.0 and 8.4 (the mysql client adds "at line N" in batch mode)
Last technically reviewed 24 October 2026 on MySQL 8.0.46
The full message
ERROR 2013 (HY000): Lost connection to MySQL server during query
Other forms of this message
mysql client running a script (batch mode)
ERROR 2013 (HY000) at line 3: Lost connection to MySQL server during query
Extended form, where the client names the stage (client error 2055 template)
Lost connection to MySQL server at '%s', system error: %d
PHP mysqli and PDO (mysqlnd) in the same situations report 2006 (observed)
SQLSTATE[HY000]: General error: 2006 MySQL server has gone away
Error number
2013
SQLSTATE
HY000
Symbol
CR_SERVER_LOST
Product
MySQL Server
Versions
MySQL client programs and libmysqlclient for MySQL 5.7, 8.0 and 8.4 (the mysql client adds "at line N" in batch mode)
Last reviewed
24 October 2026

What it means

Error 2013 means the client sent a statement but the connection broke before the full answer came back, so it does not know whether the statement finished.

The client library raises 2013 (CR_SERVER_LOST) when it did not get an error while writing to the server but did not get a full answer, or any answer, to its query. The MySQL manual gives three likely causes: network trouble while a query runs or a large result is transferred (where raising net_read_timeout can help), a failure during the initial connection (a message mentioning "reading authorization packet", where connect_timeout and the Aborted_connects status counter are the clues), and a value larger than max_allowed_packet, which some clients report as 2013 rather than 1153.

In our runs, the mysql 8.0.46 client on Windows reported 2013 whenever the server had ended its connection: when the connection was killed in the middle of a query, when it was closed for being idle before the next statement (the manual notes that on Windows the client often sees the failure only when reading the answer), and when a statement exceeded max_allowed_packet. PHP mysqlnd reported all of these as 2006. Either way, the important point is that the connection is gone and the outcome of the last statement is unknown: a write may or may not have been committed.

Common causes

  • A long query or a large result set crosses a network device (load balancer, proxy, NAT gateway, VPN) that closes connections after a fixed time or when idle.
  • The server ends the connection: an administrator or a tool runs KILL, the query hits a limit enforced by a proxy, or the server restarts or crashes (check the error log and Uptime).
  • The client was idle longer than wait_timeout, so the next statement found the connection closed (reported as 2013 by the mysql client on Windows).
  • A statement or row is larger than max_allowed_packet; the server closes the connection and some clients report 2013.
  • The initial connection takes longer than connect_timeout (slow DNS, a slow or distant network), giving a message about reading the authorization or initial packet.

Diagnose it

Read-only These statements only read metadata (information_schema, performance_schema and SHOW statements) or your own data. They do not change data, privileges or configuration, so they are safe to run first.

Timeouts that end transfers and idle connections

SHOW VARIABLES LIKE 'net_read_timeout';
SHOW VARIABLES LIKE 'net_write_timeout';
SHOW VARIABLES LIKE 'connect_timeout';
SHOW VARIABLES LIKE 'wait_timeout';
SHOW VARIABLES LIKE 'max_allowed_packet';
What to look for

Defaults are 30, 60, 10 and 28800 seconds, and 64 MB. net_write_timeout is how long the server waits for a slow client to accept result data; net_read_timeout how long it waits for data from the client.

Did the server restart or abort connections?

SHOW GLOBAL STATUS LIKE 'Uptime';
SHOW GLOBAL STATUS LIKE 'Aborted_connects';
SHOW GLOBAL STATUS LIKE 'Aborted_clients';
What to look for

A small Uptime means a restart; read the error log to learn why. Aborted_connects rises for every connection attempt the server aborts (the manual's check for the initial-connection case); Aborted_clients rises for established connections that ended without being closed properly.

Long-running statements right now

SELECT id, user, host, db, time AS seconds, LEFT(info, 100) AS statement_start
FROM performance_schema.processlist
WHERE command = 'Query'
ORDER BY time DESC
LIMIT 10;
What to look for

Statements that run for a long time are the ones most likely to outlive a proxy or load balancer timeout. Compare their duration with the timeouts of the network path.

Oversized packets seen by the server

SELECT error_number, error_name, sum_error_raised, last_seen
FROM performance_schema.events_errors_summary_global_by_error
WHERE error_number = 1153;
What to look for

A non-zero count, last seen at the time of your 2013, means the statement was larger than max_allowed_packet.

How to fix it

Each fix is labelled with what it changes. Try read-only checks and the least invasive fix first, and test changes outside production.

Find out whether the server restarted or crashed

Read-only

Check Uptime and the server error log first. If one query crashes the server every time, no client timeout will help; the log shows what happened and the query to investigate.

Make long transfers shorter

Client or network side

Page through large results with keyset pagination (WHERE id > last_id ORDER BY id LIMIT 1000), export in chunks, add indexes so long queries finish sooner, and stream rather than buffer huge results in the mysql client with --quick.

SELECT id, customer_id, total
FROM orders
WHERE id > 50000
ORDER BY id
LIMIT 1000;

Raise network timeouts for the session that moves a lot of data

Client or network side

When a slow client or a large transfer is expected, raise the timeouts on that connection only.

Caution: Changing these server-wide (SET GLOBAL, SET PERSIST or my.cnf) also lets genuinely stuck clients hold their connections for longer. SET GLOBAL lasts until the next restart and affects new connections; SET PERSIST also survives restarts.

SET SESSION net_read_timeout = 120;
SET SESSION net_write_timeout = 300;  -- this connection only, in seconds

Align proxy and load balancer timeouts with your longest queries

Changes configuration

If the connection passes through a proxy, a load balancer or a cloud gateway, its idle and session timeouts must be longer than your longest query and your pool's idle time. This is configured on that device or service, not in MySQL.

Reconnect and check before retrying writes

Client or network side

After 2013 the client cannot know whether the last statement completed. Reconnect, then check whether a write took effect (for example by its key) before running it again, or make the write idempotent so a retry is harmless.

Examples

The server closes an idle connection before the next statement

Run as a script with the mysql 8.0.46 client on Windows. The client's system command (available on Windows from MySQL 8.0.19) pauses it for about 4 seconds; use system sleep 4 on Linux. PHP mysqlnd reports the same idle timeout as 2006.

SET SESSION wait_timeout = 2;
system ping -n 5 127.0.0.1 > NUL
SELECT 1;
Result
ERROR 2013 (HY000) at line 3: Lost connection to MySQL server during query

Reproduced on MySQL 8.0.46, 24 October 2026.

A script that keeps going after its connection was killed

Run with mysql --force, which continues after errors. The KILL itself reports ERROR 1317 (70100) at line 1: Query execution was interrupted; every later statement then fails with 2013, because the batch-mode client did not reconnect.

KILL CONNECTION_ID();
SELECT 1;
Result
ERROR 2013 (HY000) at line 2: Lost connection to MySQL server during query

Reproduced on MySQL 8.0.46, 24 October 2026.

The connection is killed while a query is running

Captured by running the mysql 8.0.46 client in batch mode with SELECT SLEEP(10) and ending that connection from a second connection of the lab. The lab cannot run a second session next to the client, so this one is not rerun automatically. KILL QUERY instead of KILL CONNECTION gave no error: SLEEP() returned 1 and the script continued.

-- Session A (mysql client):
SELECT SLEEP(10);

-- Session B, while A is still running:
KILL CONNECTION 1234;  -- A's connection id
Result
ERROR 2013 (HY000) at line 1: Lost connection to MySQL server during query

A statement larger than max_allowed_packet

Captured with the mysql 8.0.46 client against a server with max_allowed_packet 64 MB. The server rejected the packet (error 1153) and closed the connection, and this client reported 2013, as the manual says some clients do. The statement is too large to store in the page, so the lab does not rerun it.

-- A file holding one statement of about 70 MB, run with a large client-side limit:
-- mysql --max-allowed-packet=128M < big_insert.sql
SELECT LENGTH('xxxx...about 70 MB of text...') AS len;
Result
ERROR 2013 (HY000) at line 1: Lost connection to MySQL server during query

How to prevent it

Keep individual statements and transfers bounded: page large reads, load data in chunks, and index long-running queries. Make sure every timeout on the path (pool idle time, proxy and load balancer timeouts, wait_timeout, net_write_timeout) fits the longest legitimate query and that the shortest one is in the client, where you can handle it.

Treat a lost connection as a normal event: reconnect, check whether the last write happened, and retry idempotent work. Alert on server restarts and on rising Aborted_connects or Aborted_clients so a network or server problem is found early.

Common questions

How do I fix Lost connection to MySQL server during query?

First check whether the server restarted (Uptime, error log). If not, look at what was running: a long query or large result crossing a proxy or load balancer timeout, a statement larger than max_allowed_packet, or a KILL. Shorten the work, raise net timeouts for that session if needed, and add reconnect handling.

Did my statement run when I got error 2013?

You cannot tell from the error. The query was sent, but the answer was lost, so a write may have committed or been rolled back. Check the data (for example by the row's key) before retrying, or design the write so that running it twice is harmless.

What does "at line 3" mean in error 2013?

The mysql client adds the line number when it runs a script (batch mode). It is the line of the statement that was being sent when the connection was found broken, not necessarily the statement that caused the disconnect.

Why does PHP show 2006 where the mysql client shows 2013?

They use different client libraries. In our tests PHP mysqlnd reported idle timeouts, killed connections and oversized packets as 2006, while the mysql client (libmysqlclient) on Windows reported 2013. Handle both numbers as "the connection is lost".

Learn more on CodeWithSQL

Official documentation

Last technically reviewed 24 October 2026; examples and diagnostics run on MySQL 8.0.46. Message templates checked against the server's error catalogue (perror) for that version.

Still stuck on a MySQL error?

Browse the full MySQL error library, or get hands-on help from an experienced SQL professional.