Skip to content
MySQL

MySQL Error 2006

MySQL server has gone away

Applies to: MySQL client libraries and connectors for MySQL 5.7, 8.0 and 8.4 (PHP mysqlnd reports it for most lost connections)
Last technically reviewed 24 October 2026 on MySQL 8.0.46
The full message
ERROR 2006 (HY000): MySQL server has gone away
Other forms of this message
PHP PDO
SQLSTATE[HY000]: General error: 2006 MySQL server has gone away
mysql command-line client in the same situations (error 2013, observed)
ERROR 2013 (HY000) at line 3: Lost connection to MySQL server during query
Server error 4031 for an idle disconnect (catalogue text; not seen in our runs)
ERROR 4031 (HY000): The client was disconnected by the server because of inactivity. See wait_timeout and interactive_timeout for configuring this behavior.
Error number
2006
SQLSTATE
HY000
Symbol
CR_SERVER_GONE_ERROR
Product
MySQL Server
Versions
MySQL client libraries and connectors for MySQL 5.7, 8.0 and 8.4 (PHP mysqlnd reports it for most lost connections)
Last reviewed
24 October 2026

What it means

Error 2006 means the connection to the server was already closed when the client tried to use it, so the statement was never run (or its result never arrived).

The client library raises 2006 (CR_SERVER_GONE_ERROR) when it cannot send a statement over the connection or the connection turns out to be closed. The server did not reject the statement; the connection was gone. The most common reason, in the MySQL manual's words, is that the server timed out and closed an idle connection: a connection that sends nothing for wait_timeout seconds (28800, eight hours, by default) is closed by the server, and the client only notices when it next sends a query. Other reasons are a KILL of the connection, a server restart or crash, a statement larger than max_allowed_packet (1153), and client-side read or write timeouts.

Which number you see depends on the client library and platform. With PHP mysqli and PDO (mysqlnd) we got 2006 for an idle timeout, for a connection killed in the middle of a query, for a read timeout and for the statement after an oversized packet. The mysql 8.0.46 command-line client on Windows reported 2013 (Lost connection during query) for the same idle timeout, which the manual explains: on Windows the client often gets the error when reading the answer rather than when writing the query. Treat 2006 and 2013 the same way in application code: the connection is unusable and must be reopened.

Common causes

  • The connection stayed idle longer than wait_timeout: a long-lived script, a worker that sleeps between jobs, or a pool that hands out a connection the server has already closed.
  • Something ended the connection: an administrator or monitoring tool used KILL, the server restarted or crashed, or a failover moved the database.
  • The statement was bigger than the server's max_allowed_packet, for example a large BLOB or a huge multi-row INSERT; the server closes the connection after error 1153.
  • A client-side timeout fired, such as MYSQLI_OPT_READ_TIMEOUT or a driver's socket timeout shorter than the query.
  • A firewall, NAT gateway or load balancer between client and server dropped an idle TCP connection without either side knowing.
  • Several processes share one connection (a connection opened before fork()), or the application uses a connection after closing it.

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 and the packet limit

SHOW VARIABLES LIKE 'wait_timeout';
SHOW VARIABLES LIKE 'interactive_timeout';
SHOW VARIABLES LIKE 'max_allowed_packet';
SHOW VARIABLES LIKE 'net_%_timeout';
What to look for

wait_timeout is the idle limit for non-interactive clients such as applications (interactive_timeout applies to clients that connect as interactive, such as the mysql client typed at by a person). Compare it with the longest idle time of your connections. max_allowed_packet is the largest statement the server accepts (64 MB by default in 8.0).

Did the server restart?

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

Seconds since the server started. If the errors began when the uptime starts, every existing connection was lost in a restart; check the error log for a crash or a planned restart.

Which connections are idle, and for how long

SELECT id, user, host, db, command, time AS seconds_in_state
FROM performance_schema.processlist
WHERE command = 'Sleep'
ORDER BY time DESC
LIMIT 20;
What to look for

Idle connections and how many seconds they have been idle. Connections that regularly approach wait_timeout belong to clients that will meet 2006 the next time they send a query.

Signs of oversized packets and dropped clients

SELECT error_number, error_name, sum_error_raised, last_seen
FROM performance_schema.events_errors_summary_global_by_error
WHERE error_number IN (1153, 4031);

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

A rising count for 1153 means statements are larger than max_allowed_packet. Aborted_clients counts connections that ended without being closed properly, which includes idle timeouts and clients that went away.

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.

Reconnect, and retry only what is safe

Client or network side

Catch 2006 and 2013, open a new connection and retry. Retry a read freely; for writes, retry the whole transaction from the start, because anything uncommitted on the old connection was rolled back. Do not rely on automatic reconnection in client libraries: the MySQL manual notes it is deprecated and that a reconnect silently resets session state (open transactions are rolled back, temporary tables, user variables and session settings are lost).

Do not keep connections idle longer than the server allows

Client or network side

Open the connection when the work starts and close it when it ends. For long-running workers and pools, set the pool's maximum idle time (or connection lifetime) below wait_timeout, and validate a connection before handing it out after a long idle period (for example with a ping or a cheap SELECT 1).

Raise wait_timeout only for the sessions that need it

Client or network side

A batch job that legitimately waits between statements can set a longer timeout on its own connection right after connecting.

Caution: Do not raise wait_timeout server-wide (SET GLOBAL, SET PERSIST or my.cnf) to hide connection leaks: idle connections then occupy slots for longer and push the server towards error 1040 Too many connections. SET GLOBAL lasts until the next restart and affects new connections only; SET PERSIST also writes mysqld-auto.cnf so it survives restarts.

SET SESSION wait_timeout = 3600;  -- this connection only, in seconds

Keep statements under max_allowed_packet

Client or network side

If the error follows a large INSERT or BLOB write, send smaller batches or chunks, as described for error 1153. Raise max_allowed_packet only deliberately, on both the server and the client, when a single value must be larger.

Examples

An idle connection closed by wait_timeout

Run with PHP mysqli (mysqlnd); PDO gave SQLSTATE[HY000]: General error: 2006 MySQL server has gone away. The mysql 8.0.46 client on Windows reported ERROR 2013 for the same idle timeout.

SET SESSION wait_timeout = 2;  -- this connection only (the default is 28800)
-- the application now sends nothing for 4 seconds
SELECT 1;
Result
ERROR 2006 (HY000): MySQL server has gone away

Reproduced on MySQL 8.0.46, 24 October 2026.

The connection is killed while a query runs

Session A ran through PHP mysqli and received 2006 as soon as its connection was killed. The mysql command-line client reports this case as 2013 Lost connection to MySQL server during query.

-- Session A: a long query
SELECT SLEEP(10);

-- Session B (for example an administrator), while A is waiting:
KILL CONNECTION 1234;  -- A's connection id from SHOW PROCESSLIST
Result
ERROR 2006 (HY000): MySQL server has gone away

Reproduced on MySQL 8.0.46, 24 October 2026.

A client read timeout shorter than the query

Captured with PHP 8.2 mysqli (mysqlnd) against MySQL 8.0.46: the query failed after 2 seconds with errno 2006, shown here in the client's ERROR format. The lab cannot set client options, so this one is not rerun automatically.

<?php
$db = mysqli_init();
$db->options(MYSQLI_OPT_READ_TIMEOUT, 2);  // seconds
$db->real_connect('127.0.0.1', 'app', $password, 'shop');
$db->query('SELECT SLEEP(6)');
Result
ERROR 2006 (HY000): MySQL server has gone away

How to prevent it

Design for connections that disappear: keep them short-lived or pooled with an idle limit below wait_timeout, check them after long idle periods, and handle 2006 and 2013 with a reconnect and a deliberate retry of the whole transaction. Never share one connection between forked processes.

Keep statements well under max_allowed_packet, send large data in chunks, and watch the server: alert on restarts (Uptime), on a rising Aborted_clients count and on error 1153 in performance_schema, so the cause is found rather than hidden by a longer timeout.

Common questions

How do I fix MySQL server has gone away?

Find out why the connection closed: an idle connection past wait_timeout, a server restart, a KILL, or a statement larger than max_allowed_packet. Then fix that cause (shorter-lived or validated connections, smaller statements) and add reconnect-and-retry handling for the cases you cannot prevent.

What is the difference between error 2006 and 2013?

In the MySQL manual, 2006 means the client could not send the query and 2013 means it sent the query but did not get a full answer. In practice the number also depends on the library and platform: PHP mysqlnd reported 2006 in all our tests, while the mysql client on Windows reported 2013 for the same situations.

Should I set wait_timeout very high to avoid error 2006?

No. A very high wait_timeout keeps abandoned connections open and can exhaust max_connections. Raise it for one session that needs it, and fix pools and scripts so they do not reuse connections the server has closed.

Is my data lost when I get error 2006?

Uncommitted work on that connection is lost: the server rolls back an open transaction when the connection ends. Work committed before the disconnect is safe. If you are not sure whether the last statement ran, check the data before retrying a write.

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.