Skip to content
FR
  1. LuzineDebug
  2. Databases (SQL)
  3. ERROR 2006 (HY000): MySQL server has gone away
Databases (SQL) Intermediate ≈ 15 min to fix

Fix "ERROR 2006 (HY000): MySQL server has gone away"

Error message
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')
In short

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.

Be the first to confirm this fix

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

  1. Connection idle for too long: a queue worker, daemon or cron script keeps the same connection for hours between two jobs.
  2. Packet too large: inserting a big BLOB, importing a dump or a multi-row INSERT larger than max_allowed_packet (64 MB by default in MySQL 8, 16 MB in MariaDB).
  3. Server restarted or crashed: killed by the OOM killer, Docker container restarted, deployment or maintenance of a managed database.
  4. Network cut on idle: ProxySQL, cloud load balancer, NAT or firewall that closes idle TCP connections earlier than MySQL does.
  5. Session killed: an administrator or a tool like pt-kill ran KILL on the connection.

Step-by-step fix

  1. Read the server settings and uptime

    If the error comes after a long pause, think wait_timeout; during a big INSERT, max_allowed_packet. An Uptime of a few minutes at the time of the error means the server just restarted.

    sql
    SHOW VARIABLES LIKE 'wait_timeout';
    SHOW VARIABLES LIKE 'max_allowed_packet';
    SHOW GLOBAL STATUS LIKE 'Uptime';
  2. Check whether the server crashed

    The MySQL error log shows a shutdown or restart; the kernel log, a mysqld process killed for lack of memory. In Docker, read the database container logs.

    bash
    sudo tail -n 100 /var/log/mysql/error.log
    docker logs --tail 100 mysql
    sudo dmesg -T | grep -i 'killed process'
  3. 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.

    ini
    [mysqld]
    max_allowed_packet = 256M
    
    [mysql]
    max_allowed_packet = 256M
    
    [mysqldump]
    max_allowed_packet = 256M
  4. 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.

    bash
    php artisan queue:work --max-time=3600 --max-jobs=500
  5. Python and Node: check and recycle pool connections

    With SQLAlchemy, pool_pre_ping tests the connection before use and pool_recycle replaces it before wait_timeout. With mysql2 in Node, create a pool with enableKeepAlive: true and an idleTimeout lower than the server and proxy timeouts.

    python
    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_timeout and 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 mysqld that 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

All Databases (SQL) errors →