MySQL ERROR 2006: server has gone away

This error means the connection to MySQL was lost mid-query. The usual causes are an idle timeout, a query larger than max_allowed_packet, or the server restarting.

Mark van Eijk 1 minute read Databases
  1. About the error
  2. Why do I see this error
  3. Solution
  4. Idle timeout
  5. Oversized query
  6. Server restarted

About the error

ERROR 2006 (HY000): MySQL server has gone away

It means the client sent a query but the connection to the server was already closed or dropped. The message is generic, MySQL is telling you the link is gone, not why.

Why do I see this error

Three causes account for almost every case:

  • Idle timeout: the connection sat idle longer than wait_timeout and MySQL closed it. Common with long-running queue workers and scheduled jobs.
  • Oversized query: a single statement exceeded max_allowed_packet, so the server dropped the connection. See MySQL max_allowed_packet: packet too large.
  • The server restarted or was OOM-killed while the client held a connection.

Solution

Idle timeout

If the error happens after periods of inactivity, raise the idle timeout in my.cnf:

[mysqld]
wait_timeout = 600
interactive_timeout = 600

Then restart MySQL. For a long-running PHP worker, the more robust fix is to reconnect rather than hold one connection open for hours. In Laravel, a queue worker that reconnects on each job avoids the problem; you can also have the worker restart periodically:

php artisan queue:work --max-time=3600

Oversized query

If it happens on a specific large insert or import, the packet limit is the cause, not the timeout. Raise it on both server and client:

[mysqld]
max_allowed_packet = 256M

See the dedicated max_allowed_packet article for the full walkthrough.

Server restarted

If neither applies, check whether MySQL crashed or was killed. Look at the MySQL error log and the system journal:

sudo tail -n 50 /var/log/mysql/error.log
sudo journalctl -u mysql --since "1 hour ago"

An out-of-memory kill points at server sizing, see adding swap space on Ubuntu.