Keywords: MySQL | Connection Timeout | Error 2013 | Performance Optimization | Database Configuration
Abstract: This paper provides a comprehensive analysis of MySQL Error Code 2013 'Lost connection to MySQL server during query', offering complete solutions from three dimensions: client configuration, server parameter optimization, and query performance. Through detailed configuration steps and code examples, it helps users effectively resolve connection interruptions caused by long-running queries, improving database operation stability and efficiency.
Problem Background and Error Analysis
MySQL Error Code 2013 is a common connection interruption error that typically occurs during the execution of long-running queries or large data operations. This error indicates that the connection between the client and MySQL server was unexpectedly terminated during query execution, preventing the operation from completing.
Client Configuration Optimization
In MySQL Workbench, connection timeout can be extended by adjusting SQL Editor configuration. The specific path is: Edit → Preferences → SQL Editor → DBMS connection read timeout (in seconds). It is recommended to increase this value from the default 600 seconds to 6000 seconds to accommodate long-running queries.
Server Parameter Adjustment
In addition to client configuration, server-side parameter settings are equally crucial. The following SQL commands can be used to adjust key timeout parameters:
SET GLOBAL max_allowed_packet=1073741824;
SET GLOBAL wait_timeout = 600;
SET GLOBAL net_read_timeout = 600;
SET GLOBAL connect_timeout = 600;Among these, the max_allowed_packet parameter controls the maximum packet size, wait_timeout controls the timeout for non-interactive connections, net_read_timeout controls the timeout for reading data from the client, and connect_timeout controls the connection establishment timeout.
Configuration File Modification
For scenarios requiring persistent configuration, MySQL's configuration file my.cnf can be directly modified:
[mysqld]
connect_timeout = 31536000
net_read_timeout = 31536000
wait_timeout = 28800
interactive_timeout = 28800This configuration method ensures that parameter settings remain effective after server restart.
Query Performance Optimization
To fundamentally resolve connection timeout issues, query performance optimization is necessary. Improvements can be made through the following approaches:
-- Add appropriate indexes
CREATE INDEX idx_table_column ON table_name(column_name);
-- Use pagination queries to avoid processing large amounts of data at once
SELECT * FROM large_table LIMIT 1000 OFFSET 0;
-- Optimize complex queries, reduce unnecessary join operations
SELECT t1.id, t1.name FROM table1 t1
WHERE t1.id IN (SELECT t2.table1_id FROM table2 t2 WHERE t2.status = 'active');Monitoring and Diagnostics
When encountering connection issues, monitoring and diagnostics can be performed through the following methods:
-- Check current connection status
SHOW PROCESSLIST;
-- Examine server status variables
SHOW GLOBAL STATUS LIKE '%timeout%';
-- Monitor query execution progress
EXPLAIN ANALYZE SELECT * FROM large_table WHERE conditions;Through these monitoring means, potential performance bottlenecks and connection issues can be promptly identified.
Comprehensive Solution Approach
By combining client configuration, server parameter optimization, and query performance improvements, a complete solution system can be constructed. It is recommended to implement the following steps: first optimize query statements and add necessary indexes, then adjust client timeout settings, and finally modify server parameters when necessary. This layered solution approach can effectively prevent and resolve Error 2013 issues.