Fix "ERROR 2006 (HY000): MySQL server has gone away"
ERROR 2006 (HY000): MySQL server has gone away
- SQLSTATE[HY000]: General error: 2006 MySQL server has gone away
- ERROR 2013 (HY000): Lost connection to MySQL server during query
- ERROR 4031 (HY000): The client was disconnected by the server because of inactivity. See wait_timeout and interactive_timeout for configuring this behavior.
- Warning: PDOStatement::execute(): MySQL server has gone away
- (2006, 'MySQL server has gone away')
The connection existed, but the server closed it before or during the query: inactivity beyond wait_timeout, a packet bigger than max_allowed_packet, or a server restart. Find the cause in the MySQL log, raise max_allowed_packet for large imports, and make long-running processes (workers, daemons) reconnect instead of holding a connection open for hours.
What this error means
The client had an open connection and, when sending a query, finds that the server closed it (2006) or loses it in the middle of the query (2013). This differs from "Connection refused" (2002), where the connection was never established. The server closes a connection when it stays idle longer than wait_timeout seconds (8 hours by default), when it receives a packet larger than max_allowed_packet, when it restarts or crashes (often killed for lack of memory), or when a session is killed with KILL. A proxy, load balancer or NAT that drops idle connections produces the same symptom. Since MySQL 8.0.24, inactivity is reported explicitly with error 4031.
Common causes
- Connection idle for too long: a queue worker, daemon or cron script keeps the same connection for hours between two jobs.
- Packet too large: inserting a big BLOB, importing a dump or a multi-row
INSERTlarger thanmax_allowed_packet(64 MB by default in MySQL 8, 16 MB in MariaDB). - Server restarted or crashed: killed by the OOM killer, Docker container restarted, deployment or maintenance of a managed database.
- Network cut on idle: ProxySQL, cloud load balancer, NAT or firewall that closes idle TCP connections earlier than MySQL does.
- Session killed: an administrator or a tool like
pt-killranKILLon the connection.
Step-by-step fix
-
Read the server settings and uptime
If the error comes after a long pause, think
wait_timeout; during a bigINSERT,max_allowed_packet. AnUptimeof a few minutes at the time of the error means the server just restarted.SHOW VARIABLES LIKE 'wait_timeout'; SHOW VARIABLES LIKE 'max_allowed_packet'; SHOW GLOBAL STATUS LIKE 'Uptime'; -
Check whether the server crashed
The MySQL error log shows a shutdown or restart; the kernel log, a
mysqldprocess killed for lack of memory. In Docker, read the database container logs.sudo tail -n 100 /var/log/mysql/error.log docker logs --tail 100 mysql sudo dmesg -T | grep -i 'killed process' -
Raise max_allowed_packet for large packets
Set the value on the server and client side (
mysql,mysqldump), then restart the server. In MySQL 8,SET PERSIST max_allowed_packet = 268435456;applies it without a restart for new connections. For application imports, splitting into batches remains the best fix.[mysqld] max_allowed_packet = 256M [mysql] max_allowed_packet = 256M [mysqldump] max_allowed_packet = 256M -
Laravel: restart long-running workers
Laravel reconnects on its own when it detects a lost connection outside a transaction, but not in the middle of one. Have workers restart regularly and, in a custom long-running script, call
DB::reconnect()before each job.php artisan queue:work --max-time=3600 --max-jobs=500 -
Python and Node: check and recycle pool connections
With SQLAlchemy,
pool_pre_pingtests the connection before use andpool_recyclereplaces it beforewait_timeout. Withmysql2in Node, create a pool withenableKeepAlive: trueand anidleTimeoutlower than the server and proxy timeouts.import os from sqlalchemy import create_engine engine = create_engine( os.environ["DATABASE_URL"], pool_pre_ping=True, pool_recycle=3600, )
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Keep pool connection recycling shorter than
wait_timeoutand than the idle timeout of proxies and load balancers. - Split imports and bulk inserts into batches (for example 1,000 rows) instead of one giant query.
- Monitor the MySQL server memory and restarts: a
mysqldthat keeps getting killed causes this error everywhere in the application.
Frequently asked questions
What is the difference between error 2006 and error 2013?
2006: the connection was already closed when the client sent the query. 2013: it was lost during the query, often a very long query, a big packet or a server crashing midway.
Should I raise wait_timeout?
Not first. A huge value lets idle connections pile up (risk of "Too many connections"). Make the client reconnect or recycle connections instead.
Why does the error happen while importing an SQL dump?
The dump contains INSERT statements bigger than max_allowed_packet. Raise the value on the server and pass --max_allowed_packet=256M to the mysql client importing the file.
Useful tools for this error
More Databases (SQL) errors
- Fix "ERROR 1045 (28000): Access denied for user 'root'@'localhost'" (MySQL, MariaDB)
- Fix "ERROR 1062 (23000): Duplicate entry for key" (MySQL, MariaDB, PostgreSQL)
- Fix "Deadlock found when trying to get lock; try restarting transaction" (MySQL, PostgreSQL)
- Fix FATAL: password authentication failed for user "postgres" (PostgreSQL)
- Fix "ERROR 1040: Too many connections" (MySQL) and "too many clients already" (PostgreSQL)
- Fix "ERROR 1054 (42S22): Unknown column in 'field list'" (MySQL)
- Fix "ERROR: relation "users" does not exist" (PostgreSQL)