Keywords: MySQL | idle connections | timeout configuration
Abstract: This article provides an in-depth exploration of managing long-idle MySQL connections in legacy PHP systems. It presents two core solutions: manual cleanup using SHOW PROCESSLIST and KILL commands, and automatic timeout configuration through wait_timeout and interactive_timeout parameters. The paper analyzes implementation steps, considerations, and potential impacts of both approaches, emphasizing the importance of addressing connection leakage at its source.
Background and Challenges of MySQL Idle Connections
When maintaining legacy PHP systems, MySQL database connections often remain idle for extended periods due to application logic flaws, improper connection pool configuration, or network anomalies. By default, MySQL's wait_timeout and interactive_timeout parameters are set to 28800 seconds (8 hours), meaning idle connections may persist for hours before automatic closure.
Manual Cleanup of Idle Connections
The most direct solution involves manually terminating idle connections through MySQL's process management. First, use the SHOW FULL PROCESSLIST command to view current connection states:
mysql> SHOW FULL PROCESSLIST;
+---------+------------+-------------------+------+---------+-------+-------+-----------------------+
| Id | User | Host | db | Command | Time | State | Info |
+---------+------------+-------------------+------+---------+-------+-------+-----------------------+
| 1193777 | TestUser12 | 192.168.1.11:3775 | www | Sleep | 25946 | | NULL |
+---------+------------+-------------------+------+---------+-------+-------+-----------------------+
Rows with "Sleep" in the Command column and large Time values indicate idle connections. Terminate specific connections using the KILL [connection_id] command:
mysql> KILL 1193777;
Note that manual termination may cause PHP applications to report errors, particularly when queries are interrupted. Therefore, this method is best used during maintenance windows or when connection limits are approached.
Automatic Timeout Configuration Optimization
A more systematic approach involves adjusting MySQL's timeout parameters. First, check current settings:
mysql> SHOW VARIABLES LIKE "%timeout%";
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| connect_timeout | 5 |
| delayed_insert_timeout | 300 |
| innodb_lock_wait_timeout | 50 |
| interactive_timeout | 28800 |
| net_read_timeout | 30 |
| net_write_timeout | 60 |
| slave_net_timeout | 3600 |
| table_lock_wait_timeout | 50 |
| wait_timeout | 28800 |
+--------------------------+-------+
The critical parameters wait_timeout and interactive_timeout control idle timeout for non-interactive and interactive connections. Modify dynamically with:
mysql> SET GLOBAL wait_timeout = 60;
mysql> SET GLOBAL interactive_timeout = 60;
To ensure persistence, add corresponding settings to the my.cnf configuration file:
[mysqld]
wait_timeout = 60
interactive_timeout = 60
Reducing timeout from the default 8 hours to 1 minute (60 seconds) significantly decreases idle connection duration, but adjustments should align with application requirements to avoid interrupting normal operations.
Root Cause Analysis and Best Practices
While adjusting timeout parameters alleviates idle connection issues, this addresses symptoms rather than causes. The true solution lies in identifying why connections remain idle. Common issues in PHP applications include:
- Failure to properly call
mysqli_close()ormysql_close()functions - Missing connection closure in exception handling logic
- Web server connection pools not properly managed
- Connections not released during long script execution
Best practice recommendations:
- Use try-catch-finally structures in PHP code to ensure connections are always closed
- Regularly review application logs for connection-related errors
- Configure appropriate maximum idle time and connection limits when using connection pools
- Monitor MySQL's
Threads_connectedandThreads_runningstatus variables
Conclusion
Managing MySQL idle connections requires integrating manual cleanup and automatic timeout configuration strategies. For emergencies, manual KILL commands provide quick solutions; for long-term maintenance, properly configuring wait_timeout and interactive_timeout parameters is more effective. However, the most crucial aspect is addressing connection leakage at the application level through optimized code logic and configuration management, ensuring efficient database connection utilization and system stability.