How to Fix “MySQL server has gone away” (Error 2006) – Causes & Solutions
The MySQL server has gone away error (code 2006) is one of the most common headaches when importing large SQL dumps, running WordPress migrations, or executing long-running PHP/Python scripts. It means your client sent a query after MySQL or MariaDB had already closed the connection.
Quick answer: 90% of the time it comes down to two server settings – max_allowed_packet being too small (large query or SQL dump import) or wait_timeout expiring on an idle connection.
Step 1. Check Current Server Variables
Run these queries in the MySQL/MariaDB CLI or phpMyAdmin’s SQL tab:
SHOW VARIABLES LIKE 'max_allowed_packet'; SHOW VARIABLES LIKE 'wait_timeout'; SHOW VARIABLES LIKE 'interactive_timeout';
Default max_allowed_packet is often just 16 MB (or 1–4 MB on older hosts). If a query or bulk insert exceeds that limit, MySQL treats the packet as malformed and drops the connection immediately.
Step 2. Increase Limits in my.cnf / mysqld.cnf
Open your database config file (on Ubuntu/Debian usually /etc/mysql/mysql.conf.d/mysqld.cnf or /etc/mysql/mariadb.conf.d/50-server.cnf; on CentOS/Rocky /etc/my.cnf) and raise these values under [mysqld]:
[mysqld] max_allowed_packet = 64M wait_timeout = 600 interactive_timeout = 600
Restart the database service:
sudo systemctl restart mysql # or for MariaDB: sudo systemctl restart mariadb
Step 3. Importing Large SQL Dumps via CLI
When restoring a big backup from the command line, pass an increased packet limit to the mysql client as well:
mysql --max_allowed_packet=256M -u root -p dbname < backup.sql
2 Other Frequent Causes of Error 2006
- Out-of-Memory (OOM Killer). On low-RAM VPS plans, Linux can kill
mysqldunder load spikes, leaving clients withMySQL server has gone away. Checkdmesg | grep -i oom, add swap space, or move to a roomier VPS (such as Fornex with 7% off or Vultr – affiliate links). - Application logic. A worker opens a DB connection, spends 30 minutes downloading files or scraping pages without querying SQL, and then reuses a timed-out handle (or queries a handle it already closed). Add a ping/reconnect check in long-running workers.
Related database guide: MariaDB vs MySQL – which to choose for your server.
Start by checking SHOW VARIABLES and dmesg for OOM events – bumping max_allowed_packet and wait_timeout fixes the vast majority of cases in two minutes.
