Skip to content
Databases3 min read

Fix "MySQL server has gone away"

ERROR 2006 (HY000): MySQL server has gone away

Short answer

Error 2006 means your app tried to use a connection the MySQL server had already closed. It usually happens when an import or query is bigger than max_allowed_packet, or when a long-running script leaves its connection idle past wait_timeout. Shrink the statements or reconnect before use.

ArabyHost teamLast updated: ShareWhatsAppXLinkedIn
Contents
  1. Causes
  2. How to fix it
  3. Prevention
  4. On ArabyHost

The name sounds dramatic, but the server hasn’t necessarily gone anywhere. Precisely, your app sent a command over a connection the server no longer had. In PHP it appears as SQLSTATE[HY000]: General error: 2006 MySQL server has gone away, and it has a close relative, 2013 Lost connection to MySQL server during query, where the connection drops mid-query.

There are two main patterns: it appears during a big import or query, or it appears in a script that runs for a long time.

Causes

  • A statement larger than max_allowed_packet: a huge INSERT in a dump, or an image stored in a BLOB column.
  • An idle connection past wait_timeout: a worker or cron script opens the connection, waits or processes files, then comes back after the server has closed it.
  • A query that was killed for running too long.
  • A server restart or a crash from running out of memory.
  • Forked processes sharing one connection, where one closes it on the other.

How to fix it

  1. When importing, make statements smaller. In phpMyAdmin choose Export → Custom and lower Maximal length of created query. With mysqldump, write one statement per row:
mysqldump --skip-extended-insert -u cpuser_shop -p cpuser_shop > backup.sql

The file gets bigger, but every statement in it is small. If the file is very large, split it as described in phpMyAdmin import file too large.

  1. On your own server only, raise the limits in the MySQL or MariaDB config:
[mysqld]
max_allowed_packet = 256M
wait_timeout = 600

Check the current value with:

SHOW VARIABLES LIKE 'max_allowed_packet';
  1. In long-running scripts, reconnect before use:
function db(): PDO {
    static $pdo = null;
    try {
        $pdo?->query('SELECT 1');
    } catch (PDOException $e) {
        $pdo = null;
    }
    return $pdo ??= new PDO(DB_DSN, DB_USER, DB_PASS);
}

Simpler still: open the connection when you need it and close it afterwards, instead of keeping it open for the whole run.

  1. In Laravel, the framework reconnects automatically when a connection drops outside a transaction. Make workers restart themselves regularly:
php artisan queue:work --max-time=3600
  1. Don’t store files in the database. Keep images on disk and store only their path.

  2. Split heavy jobs into batches, such as updating 1000 rows at a time instead of a whole table.

Prevention

  • Keep connections short-lived and open them only when needed.
  • Check the size of dumps before moving them, and compress them.
  • If every site fails at the same moment, the cause is likely the server; report the time to support.

On ArabyHost

Resources on ArabyHost are shared with per-account limits and there’s no root access, so server settings such as max_allowed_packet aren’t yours to change. Make statements smaller and split imports instead. On AI/Developer plans you can import over SSH with mysql -u cpuser_shop -p cpuser_shop < backup.sql, and run background workers kept alive with cron, as in the cron jobs guide. The Araby AI assistant reads your error log and identifies which query or script lost its connection. Plan details are on the developers page.

FAQ

Does this mean the MySQL server crashed?

Not necessarily. Usually only your connection was closed, because it sat idle too long or the query was too large, while the server kept running for everyone else.

Why do I get it when importing a large SQL file?

Some INSERT statements in the file exceed max_allowed_packet. Export the database with smaller statements, or split the file, and import again.

How do I stop it in a Laravel queue worker?

Laravel reconnects automatically in most cases outside transactions. Run the worker with --max-time so it restarts itself regularly, and avoid long-running work inside one transaction.

Useful? Share it with someone who needs it:WhatsAppXLinkedIn

Read next

Ready to put your idea online?

Fast, isolated hosting, support that understands you, and an AI assistant that fixes problems with you.