MySQL Error 2006 (HY000) occurs when the connection between your client and MySQL server is lost. Common causes include connection timeouts, large packets, firewall issues, server crashes, and killed connections. Fixing requires adjusting wait_timeout, increasing max_allowed_packet, checking network connectivity, and verifying server health.
MySQL Error 2006 "server has gone away" indicates that the connection between your application and the MySQL server has been disconnected or lost. This error can occur at different points: when your client attempts to connect to the server, during an active query, or even when trying to reconnect after a brief lapse. The error code "HY000" is a generic SQLSTATE code representing a "general error" in MySQL. The underlying cause can be one of several things: the server actively closed your connection due to idle timeout, the connection was forcibly terminated (via KILL command or process termination), a network issue severed the TCP/IP link, the server crashed or restarted, or your packet was too large for the server to process. Unlike syntax errors that immediately indicate a problem with your SQL, Error 2006 is contextual—it depends on what the server was doing when it lost the connection. This makes diagnosis require multiple checks: network connectivity, server logs, timeout settings, and query characteristics.
First, confirm the server is actually up and can accept connections.
# Check if MySQL process is running:
ps aux | grep mysqld
# Try a simple connection:
mysql -u root -p -h localhost -e "SELECT 1;"
# Or check the status:
sudo systemctl status mysql
# Check for recent restarts in the MySQL error log:
tail -50 /var/log/mysql/error.log | grep -i "started\|restarted\|ready for connections"If the server recently restarted, that explains the disconnection. Check system logs for crashes:
dmesg | tail -20 # kernel OOM killer, segfaults, hardware errors
journalctl -u mysql -n 50 # systemd logsBy default, MySQL closes idle connections after wait_timeout seconds (default 28800 = 8 hours). Long-running queries or idle pooled connections will hit this limit.
# Check the current timeout:
mysql -u root -p -e "SHOW VARIABLES LIKE 'wait_timeout';"
# Temporarily increase it (until restart):
mysql -u root -p -e "SET GLOBAL wait_timeout = 86400;"
# Make it permanent by editing /etc/mysql/mysql.conf.d/mysqld.cnf (or /etc/my.cnf):
[mysqld]
wait_timeout = 86400
interactive_timeout = 86400
# Restart MySQL:
sudo systemctl restart mysqlFor PHP applications, also set default_socket_timeout in php.ini to match or exceed wait_timeout:
# In /etc/php/8.0/apache2/php.ini or /etc/php/8.0/cli/php.ini:
default_socket_timeout = 86400If your application performs large INSERT statements, bulk imports, or works with BLOB columns, the default max_allowed_packet (64 MB) may be exceeded.
# Check the current setting:
mysql -u root -p -e "SHOW VARIABLES LIKE 'max_allowed_packet';"
# Temporarily increase it:
mysql -u root -p -e "SET GLOBAL max_allowed_packet = 256M;"
# Make it permanent in /etc/mysql/mysql.conf.d/mysqld.cnf:
[mysqld]
max_allowed_packet = 256M
# Restart MySQL:
sudo systemctl restart mysqlNote: The value must be the same on both server and client. If using a MySQL client library, you may also need to set max_allowed_packet in that client's configuration.
Verify the network path between your client and MySQL server is clear.
# From the client, test connectivity to the server:
telnet <mysql-host> 3306
nc -zv <mysql-host> 3306
# Check firewall status on the MySQL server:
sudo iptables -L | grep 3306 # Linux iptables
sudo ufw status | grep 3306 # UFW firewall
# Temporarily allow MySQL port (if using UFW):
sudo ufw allow 3306
# Check for remote connection limits in MySQL:
mysql -u root -p -e "SELECT user, host FROM mysql.user WHERE user = 'your-app-user';"If the host is restricted to localhost, your remote client cannot connect. Update the bind address in my.cnf:
[mysqld]
bind-address = 0.0.0.0 # Listen on all interfaces
# Or specify a specific IP:
bind-address = 192.168.1.100For applications with long-running processes or idle connections, use connection pooling with auto-reconnect.
Node.js with mysql2:
const pool = mysql.createPool({
connectionLimit: 10,
host: 'localhost',
user: 'root',
password: 'password',
database: 'mydb',
waitForConnections: true,
enableKeepAlive: true,
keepAliveInitialDelayMs: 0
});PHP with PDO:
$pdo = new PDO('mysql:host=localhost;dbname=mydb', 'root', 'password', [
PDO::ATTR_PERSISTENT => false,
PDO::ATTR_TIMEOUT => 5,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4"
]);Java with HikariCP:
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:mysql://localhost:3306/mydb");
config.setMaximumPoolSize(10);
config.setConnectionTimeout(30000);
config.setIdleTimeout(600000);
HikariDataSource ds = new HikariDataSource(config);Pool connections periodically and implement exponential backoff retry logic.
If connections are being killed explicitly or implicitly, investigate.
# Check if any queries are being killed (monitor in another terminal):
mysql -u root -p -e "SHOW FULL PROCESSLIST;"
# Look for your connections and their status
# If Status shows "Killed", the connection was terminated
# Check server resource limits:
FILE_DESCRIPTORS=`ulimit -n`
echo "Max open files: $FILE_DESCRIPTORS"
# Check for memory pressure:
free -h
htop # Check for OOM killer
# Review the error log for OOM killer activity:
grep -i "killed\|oom" /var/log/syslog # Ubuntu/Debian
grep -i "killed\|oom" /var/log/messages # CentOS/RHELIf the OOM killer terminated MySQL, increase available memory or optimize MySQL buffer pool:
mysql -u root -p -e "SET GLOBAL innodb_buffer_pool_size = 512M;" # Adjust based on available RAMQueries that run longer than wait_timeout will trigger Error 2006.
# Enable slow query logging:
mysql -u root -p -e "SET GLOBAL slow_query_log = 1; SET GLOBAL long_query_time = 10;"
# Make it permanent in /etc/mysql/mysql.conf.d/mysqld.cnf:
[mysqld]
slow_query_log = 1
long_query_time = 10
log_queries_not_using_indexes = 1
log-slow-admin-statements = 1
# Check the slow query log:
tail -100 /var/log/mysql/slow.log
# Analyze a slow query:
mysql -u root -p
EXPLAIN SELECT ... <your slow query>\G
# Look for "Using temporary" or "Using filesort" which indicate inefficiency
# Add indexes to improve performanceOptimizations include: adding indexes, breaking large transactions into smaller batches, using LIMIT to reduce result sets, and using stored procedures instead of application-level loops.
For forked child processes (common in PHP-FPM, Apache with mod_php, and worker-based architectures): each child process must create its own connection to MySQL. Sharing a connection handle between parent and child causes unpredictable behavior. Ensure each worker opens a fresh connection in its initialization code.
In containerized environments, connection timeouts often stem from orchestrator health checks interfering with long-lived connections. Docker health checks and Kubernetes liveness probes may force connection termination. Set grace periods appropriately and ensure probes do not interrupt active queries.
For MySQL Replication, Error 2006 on the replica slave thread indicates the slave lost connection to the master. This is often a network issue. Check "SHOW SLAVE STATUS\G" and look at Last_IO_Errno and Last_IO_Error. If the network is unstable, increase slave net_read_timeout: "SET GLOBAL slave_net_read_timeout = 120;" (default 30 seconds).
When using SSH tunnels or ProxySQL, connection pooling at the proxy layer can mask underlying timeouts. Monitor the proxy logs and ensure its timeout settings exceed the application's.
For AWS RDS MySQL, note that RDS can forcibly close connections every 15 minutes for maintenance. Use RDS Proxy to handle reconnection logic transparently. Similarly, Google Cloud SQL and Azure MySQL have similar connection management behaviors documented in their platform guides.
EE_WRITE (3): Error writing file
How to fix "EE_WRITE (3): Error writing file" in MySQL
CR_PARAMS_NOT_BOUND (2031): No data supplied for parameters
How to fix "CR_PARAMS_NOT_BOUND (2031): No data supplied for parameters" in MySQL
CR_DNS_SRV_LOOKUP_FAILED (2070): DNS SRV lookup failed
How to fix "CR_DNS_SRV_LOOKUP_FAILED (2070): DNS SRV lookup failed" in MySQL
ERROR 1146: Table 'database.table' doesn't exist
How to fix "ERROR 1146: Table doesn't exist" in MySQL
ERROR 1040: Too many connections
How to fix "ERROR 1040: Too many connections" in MySQL