Skip to content

MySQL "server has gone away" (error 2006/2013) on a cPanel host — the four causes worth checking

A query dies mid-flight with "MySQL server has gone away". The connection was dropped, not refused — and which setting dropped it tells you exactly what to fix.

MySQL server has gone away (client error 2006, or 2013 “Lost connection during query”) means the client had a working connection and it disappeared underneath an in-flight query. This is not a refused connection — the handshake succeeded — it is a dropped one, which narrows the suspects considerably. The connection died for one of a handful of concrete reasons: it sat idle too long and the server reaped it, the query sent more data than max_allowed_packet permits, the query ran longer than a timeout allowed, or the MySQL server itself restarted or was killed. Each cause leaves a different fingerprint, and the fix follows directly from which one you find.

What’s happening

When MySQL closes a connection that a client still believes is open, the next operation on that connection fails with 2006. The most common trigger on a shared cPanel host is the idle timeout: wait_timeout defines how many seconds a connection may sit doing nothing before the server hangs up. A long-lived PHP worker, a cron job that opens a handle and then sleeps, or an import paused for input will exceed it and find the connection gone when it resumes.

The second trigger is packet size. MySQL refuses to receive a single packet larger than max_allowed_packet; a large INSERT, a LONGBLOB write, or restoring a dump with big multi-row inserts can exceed it, and the server drops the connection rather than buffer it. The third is a query that runs past a server-side limit (net_read_timeout, net_write_timeout, or a Governor/proxy cap) and gets cut off mid-result. The fourth, and the one to rule out first, is that mysqld itself went away — an OOM kill, a crash-and-restart, or a deliberate systemctl restart mysql — in which case every connection drops at once, not just one.

How to verify

Establish first whether the whole server bounced or just one connection died:

# Did mysqld restart? Uptime in seconds — a small number means it just came back
mysqladmin status
mysql -e "SHOW STATUS LIKE 'Uptime';"

# Crash or OOM kill leaves a trail
tail -50 /var/lib/mysql/$(hostname).err
grep -i 'mysqld\|oom' /var/log/messages | tail -20
dmesg | grep -i 'killed process' | tail

If uptime is large and there is no crash in the log, the server is fine and a single connection was dropped. Now check the two settings that drop healthy connections:

# Idle timeout and the packet ceiling
mysql -e "SHOW VARIABLES LIKE 'wait_timeout'; SHOW VARIABLES LIKE 'interactive_timeout';"
mysql -e "SHOW VARIABLES LIKE 'max_allowed_packet';"

# Have packets actually been rejected for being too big?
mysql -e "SHOW STATUS LIKE 'Aborted_clients'; SHOW STATUS LIKE 'Aborted_connects';"

For a “gone away” during a restore or large write, the packet size is almost always the cause — compare the largest row or blob in the dump against max_allowed_packet. For a “gone away” in a long-running app or cron, it is the idle timeout. Reproduce the import path if that is where it fails:

# A restore that dies partway is the classic packet-size symptom
mysql dbname < big_dump.sql
# ... ERROR 2006 (HY000) at line N: MySQL server has gone away

The fix

  1. Rule out a server restart first — if Uptime is small or the error log shows a crash or OOM kill, the connection drop is a symptom, not the disease. Fix why mysqld restarted (out-of-memory tuning, a crashing query, swap thrashing) before touching client timeouts. Chasing wait_timeout when the real problem is a nightly OOM kill wastes the afternoon.
  2. Raise max_allowed_packet for large writes and restores — this is the fix when 2006 appears during an import or a big INSERT. Set it on both server and client: in /etc/my.cnf add max_allowed_packet=256M under [mysqld], restart MySQL, and pass --max_allowed_packet=256M to the mysql client when restoring. The dump’s largest single packet must fit; bump it until the restore completes.
  3. Lengthen the idle timeout for long-lived connections — if a cron job or worker holds an idle handle longer than wait_timeout, either raise wait_timeout (and interactive_timeout) in /etc/my.cnf, or, better, have the application not hold an idle connection across long sleeps. A pattern that connects, works, and disconnects per task never trips this.
  4. Reconnect instead of assuming the handle is alive — the durable application fix is to ping the connection before reusing it after any pause. WordPress does this through $wpdb, but custom code that caches a connection object should call mysqli_ping() (or catch the error and reconnect) rather than firing a query into a handle that may have been reaped minutes ago.
  5. Account for server-side query timeouts — if the connection dies specifically on a long SELECT or a slow result transfer, check net_read_timeout / net_write_timeout and, on CloudLinux, whether MySQL Governor or a per-user limit is killing the query at the database tier. Raising the relevant timeout helps, but a query slow enough to hit it usually wants an index, not a longer leash.
  6. Restore through a method that paces itself — for very large databases, a dump full of giant multi-row inserts is fragile. Re-dump with mysqldump --max-allowed-packet=256M --net-buffer-length=16384 and consider --extended-insert=FALSE for problem tables, or restore over the local socket rather than TCP to avoid any network-layer timeout entirely.
  7. Confirm the fix end to end — after adjusting, re-run the exact operation that failed (the import, the cron, the report) and watch it complete. For idle-timeout fixes, leave the connection idle past the old limit and confirm the next query succeeds. A 2006 that does not recur under the same load is genuinely fixed; one that “seems better” is not.

The whole diagnosis turns on one early decision: did the server go away, or did one connection get dropped. The first sends you to memory and crash tuning; the second sends you to packet size and timeouts. Stack Harbor untangles MySQL connection drops under managed cPanel operations — start a conversation when queries keep dying mid-flight.