Solved: MySQL 'Lost Connection to Server During Query' Error in Production
Quick Fix Summary
TL;DRIncrease `wait_timeout` and `interactive_timeout` in your MySQL configuration and restart the service.
MySQL ERROR 2013 occurs when a client connection is terminated by the server before a query completes. This is typically due to timeout settings, network issues, or server resource constraints.
Diagnosis & Causes
Recovery Steps
Step 1: Diagnose Current Timeout and Packet Settings
First, connect to your MySQL server and check the current configuration values that most commonly cause this error.
SHOW GLOBAL VARIABLES LIKE 'wait_timeout';
SHOW GLOBAL VARIABLES LIKE 'interactive_timeout';
SHOW GLOBAL VARIABLES LIKE 'max_allowed_packet';
SHOW GLOBAL STATUS LIKE 'Aborted_connects'; Step 2: Adjust Server Configuration (my.cnf / my.ini)
Permanently increase timeout and packet size limits in the MySQL configuration file. Adjust values based on your application's long-running query needs.
[mysqld]
wait_timeout = 28800
interactive_timeout = 28800
max_allowed_packet = 256M
net_read_timeout = 120
net_write_timeout = 120 Step 3: Apply Changes and Restart MySQL Service
After editing the config file, apply the changes with a graceful restart. Use systemd on modern Linux distributions.
# For systemd systems (Ubuntu 16.04+, CentOS/RHEL 7+)
sudo systemctl restart mysql
# OR for older SysVinit systems
sudo service mysql restart Step 4: Configure Client Connection Pool Keep-Alive
Prevent timeouts from the application side by configuring your connection pool (e.g., HikariCP, C3P0) to test connections before use.
# Example HikariCP properties for Spring Boot (application.yml)
spring:
datasource:
hikari:
connection-test-query: SELECT 1
keepalive-time: 30000 # ms
max-lifetime: 240000 # ms (keep below wait_timeout) Step 5: Monitor for Network and Resource Issues
Rule out infrastructure problems by checking system resources and network stability between your application and database servers.
# Check for packet loss and latency
ping -c 10 your-mysql-host
# Monitor MySQL server memory and CPU (example with top)
top -c -p $(pgrep mysqld)
# Check MySQL error log for OOM or crash clues
sudo tail -100 /var/log/mysql/error.log Step 6: Optimize Long-Running Queries
If the error occurs on specific complex queries, analyze and optimize them to reduce execution time below the timeout threshold.
-- Identify slow queries from the slow log or performance_schema
SELECT * FROM performance_schema.events_statements_summary_by_digest
ORDER BY SUM_TIMER_WAIT DESC LIMIT 5;
-- Use EXPLAIN to analyze a problematic query
EXPLAIN ANALYZE SELECT * FROM large_table WHERE complex_condition; Architect's Pro Tip
"In Kubernetes, this error often surfaces after a pod's liveness probe fails, triggering a restart and severing all active DB connections. Set your probe `periodSeconds` and `timeoutSeconds` significantly lower than `wait_timeout`."
Frequently Asked Questions
What's the difference between `wait_timeout` and `interactive_timeout`?
`wait_timeout` applies to non-interactive client connections (e.g., from a web app pool). `interactive_timeout` applies to interactive clients (e.g., the MySQL CLI). Always set them to the same value to avoid confusion.
Will increasing `max_allowed_packet` impact server performance?
Yes, it increases the memory buffer size per connection. Monitor your `Max_used_connections` and ensure `(max_allowed_packet * max_connections)` does not exceed available RAM, to prevent swapping or OOM kills.
My application uses an ORM (like Hibernate). How do I fix it there?
Configure the connection pool properties within your ORM/data source settings. The key is enabling a validation query (`validationQuery: SELECT 1`) and setting a sensible `maxLifetime` (e.g., 300000ms).