Australian cloud for business software — built and run in Sydney since 2002

NetLeverage
NetLeverage

MySQL server has gone away (error 2006): wait_timeout, max_allowed_packet or a crash

Technical article
Troubleshooting

About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.

The message MySQL server has gone away is client error 2006 (CR_SERVER_GONE_ERROR). It means the client could not send its query to the server. The MySQL manual says the most common reason is that the server timed out and closed an idle connection. Other documented reasons include a packet larger than max_allowed_packet, a KILL, a firewall or DNS fault, and a server that died and restarted. The fix depends on which one it is, so measure first.

What the error means

The MySQL 8.4 Error Reference lists client error 2006 with the symbol CR_SERVER_GONE_ERROR and the text MySQL server has gone away. The manual describes two related client codes. CR_SERVER_GONE_ERROR means the client could not send a question to the server. CR_SERVER_LOST (error 2013) means the client wrote to the server without an error but did not get a full answer, or any answer. The manual covers Lost connection to server during query on the same page, so the two are diagnosed together.

By default the server closes a connection after eight hours of inactivity. That is wait_timeout, whose default is 28800 seconds (28,800 ÷ 3,600 = 8 hours). A connection made by an interactive client uses interactive_timeout, which has the same default. The session value of wait_timeout is set from the global value, or from interactive_timeout, when the connection starts.

The manual says a packet bigger than max_allowed_packet makes the server (or client) issue ER_NET_PACKET_TOO_LARGE (error 1153, Got a packet bigger than 'max_allowed_packet' bytes) and close the connection. It adds that with some clients you may also get Lost connection to MySQL server during query. That message is client error 2013, not 2006. The "gone away" page lists max_allowed_packet as a possible cause too: it says that if you need big queries, you can raise the limit by setting the server's max_allowed_packet variable. So an oversized statement can show up as error 1153, as error 2013 or as "gone away", depending on the client.

Likely causes

Cause What you see How to confirm
Idle timeout (wait_timeout) The first query after a quiet period fails, later queries work after a reconnect; pooled connections fail in the morning The failure happens after an idle gap close to the wait_timeout value; Aborted_clients rises
Packet larger than max_allowed_packet One big statement, a large BLOB or a huge multi-row INSERT fails every time The statement size is above the server and client limits; error 1153 may appear in the server log
Connection killed Sudden loss mid-session A KILL or mysqladmin kill in an admin history or script
Server crashed and restarted Many clients fail at the same moment, then work mysqladmin version shows a short uptime; the error log shows a restart
Network, firewall or DNS Failures that do not match a timeout or a size Port 3306 blocked, failing name lookups, or skip_networking enabled
Client-side timeout or disabled reconnect A client library read or write timeout fires, or automatic reconnect is off Client settings such as MYSQL_OPT_READ_TIMEOUT and the reconnect flag
Child processes sharing one connection Random failures in a forking application The application forks after connecting

Prove the cause before changing anything

These checks only read, with one optional exception: raising the log verbosity at the end of step 4 changes a setting, so it is marked.

1. Did the server restart? Run:

mysqladmin version

The manual tells you to look at the uptime in this output. A few minutes of uptime when the server has run for weeks means a crash or restart. In that case read the error log and treat the restart as the real fault; MySQL documents a separate procedure for a server that keeps crashing.

2. Read the two limits that matter. In a mysql session on the server:

SHOW GLOBAL VARIABLES LIKE 'wait_timeout';
SHOW GLOBAL VARIABLES LIKE 'interactive_timeout';
SHOW GLOBAL VARIABLES LIKE 'max_allowed_packet';

SHOW VARIABLES needs no privilege beyond the ability to connect. Defaults in MySQL 8.4 are 28800, 28800 and 67108864 bytes. The last is 64 × 1,048,576 = 67,108,864 bytes, which is 64 MB.

3. Compare the idle gap with wait_timeout. If the application logs show the failing request came after a pause of roughly the wait_timeout value, an idle timeout is the likely cause.

4. Look for aborted connections.

SHOW GLOBAL STATUS LIKE 'Aborted%';

SHOW STATUS also needs no privilege beyond the ability to connect. The manual explains that Aborted_clients counts clients that connected and later disconnected improperly, including clients that slept longer than wait_timeout or interactive_timeout. Aborted_connects counts clients that never completed the connection (wrong password, no privileges, a bad connection packet, or a slow connect). The manual also says that with log_error_verbosity set to 3 the error log shows lines such as [Note] Aborted connection 854 to db: 'employees' user: 'josh'. To see those lines you must raise log_error_verbosity. Print the current value first with SHOW GLOBAL VARIABLES LIKE 'log_error_verbosity'; (the default is 2).

Risk: a verbosity of 3 adds information messages to the error log, so the log grows faster. Back-out: set the variable back to the value you printed with SET GLOBAL log_error_verbosity = 2; (the variable is dynamic, and 2 is the default; use your printed value if it differs).

SET GLOBAL log_error_verbosity = 3;

5. Measure the offending statement. If you suspect size, find the largest statement or row the application sends. The manual says the largest possible packet is 1 GB. The mysql client's default max_allowed_packet is 16 MB, and mysqldump defaults to 24 MB, so a client can fail below the server's 64 MB.

Fix

Idle timeout. The manual says that for a script you can issue the query again and let the client reconnect automatically, if automatic reconnection is enabled (the default for the mysql command-line client). The cleaner fix is in the application: validate or refresh pooled connections before use, or reconnect and retry once. For Windows clients the manual gives two options: call mysql_ping() on the connection after a long idle period, or set wait_timeout so high that it never expires in practice.

If you must raise the server limit, save the current value first:

SHOW GLOBAL VARIABLES LIKE 'wait_timeout';

Note the value (28800 unless changed).

Risk: raising wait_timeout changes server behaviour for every new connection, and idle connections stay open for longer, which uses more of max_connections. Back-out: run SET GLOBAL wait_timeout = 28800; using the value you noted. Existing sessions keep the value they started with.

Then apply it at runtime:

SET GLOBAL wait_timeout = 57600;

That example value is 57,600 seconds, or 16 × 3,600 = 16 hours.

To make the change survive a restart, MySQL 8.4 offers SET PERSIST, which also writes mysqld-auto.cnf in the data directory:

SET PERSIST wait_timeout = 57600;

The back-out is RESET PERSIST wait_timeout; followed by the SET GLOBAL wait_timeout = 28800; line above (using the value you noted).

Oversized packet. First try to send less per statement. The manual says an INSERT or REPLACE that inserts a great many rows is sent as a single request, so reducing the number of rows per statement often avoids the error. If you need bigger packets, the manual says to raise max_allowed_packet on both the client and the server, because each has its own limit. Save first:

SHOW GLOBAL VARIABLES LIKE 'max_allowed_packet';

Risk: raising max_allowed_packet is, the manual says, safe because the extra memory is allocated only when needed, but a bigger limit lets one client send a larger statement, which can use more memory on a busy server. Back-out: SET GLOBAL max_allowed_packet = 67108864; using the value you noted, and RESET PERSIST max_allowed_packet; if you persisted it.

Note the value, then apply a new one (128 MB is 128 × 1,048,576 = 134,217,728 bytes):

SET GLOBAL max_allowed_packet = 134217728;

The session value of this variable is read-only, so only connections made after the change see the new limit; reconnect the application. For the client, the manual's example is mysql --max_allowed_packet=32M. If you prefer an option file, the manual shows max_allowed_packet=128M under [mysqld]. Before editing an option file such as /etc/my.cnf or /etc/mysql/my.cnf (both are in the manual's list of files read), copy it:

cp -p /etc/mysql/my.cnf /etc/mysql/my.cnf.2026-11-20.bak

Disruptive step: the restart that applies the option-file setting closes every connection on mysqld. Do it in a quiet period. Back-out: copy /etc/mysql/my.cnf.2026-11-20.bak back over /etc/mysql/my.cnf, run the mysqld --validate-config check below, then restart.

Add max_allowed_packet=128M under [mysqld], then check the configuration and only then restart:

mysqld --validate-config

An exit code of 0 means no errors were found; 1 means an error was found and the server prints a diagnostic message.

Killed or crashed server. Stop changing limits. Read the error log for the restart cause and follow the manual's section on a server that keeps crashing, which starts by checking whether issuing the same query again kills the server again. Disk, memory and InnoDB recovery are covered by MySQL will not start after a crash: InnoDB errors and innodb_force_recovery used safely.

Network, firewall or DNS. The manual lists a blocked MySQL port (default 3306), skip_networking enabled, and failing host name lookups as causes. Fix those at their source and retest from the same client host.

Child processes. Give each forked child its own connection.

Example (illustrative): a nightly import on web01 writes to db01 (192.0.2.20). It fails at the same row every night with MySQL server has gone away. wait_timeout is 28800 and the job runs for minutes, so an idle timeout is ruled out. The server uptime is 41 days, so there was no crash. The server's max_allowed_packet is 67108864 bytes. The importer builds one INSERT of about 80 MB, which is 80 × 1,048,576 = 83,886,080 bytes, above the limit. The team first changes the importer to send 1,000 rows per statement, which fixes it. They leave the server limit alone.

Confirm the fix

Rerun the failing action and watch for the original message. For the idle case, leave a test connection idle for longer than the new wait_timeout in a controlled test and confirm the application reconnects. For the size case, rerun the exact statement. Check SHOW GLOBAL STATUS LIKE 'Aborted%'; afterwards: Aborted_clients should stop climbing for that workload. If you raised log_error_verbosity to 3 to look at the log, set it back to the value you printed.

What to watch afterwards

  • Server uptime, so an unplanned restart is noticed.
  • Aborted_clients and Aborted_connects over time.
  • Idle connection counts against max_connections if you raised wait_timeout.
  • Statement sizes after application releases, especially bulk loads and BLOB columns.

Risks and limits

The values and wording here come from the MySQL 8.4 manual. MariaDB and earlier MySQL releases may have different defaults. Raising wait_timeout hides an application that does not handle reconnects; raising max_allowed_packet hides statements that should be batched. These commands have not been run or lab-tested.

Sources