This article mainly introduces the implementation of PHP to clear the dead connection method, through the timing of the implementation of PHP script to the MySQL dead link check and cleanup function, the need for friends can refer to. We hope to help you.
Connection, the main performance is that there is too much sleep connection, and time is very long, the full number of available connections, so that other users can no longer connect to the database. I started to think about tuning MySQL database parameters, but many of the parameters still didn't solve the problem. So think of a more ruthless approach, write a PHP script, every 2 minutes to execute, found dead connection (more than 120 seconds) to kill, so that some programs will never kill the database server, the following is the Kill connection applet:
kill-mysql-sleep-proc.php:
Define (' Max_sleep_time ', +); $hostname = "localhost"; $username = "root"; $password = "password"; $connect =mysql_ Connect ($hostname, $username, $password) $result =mysql_query ("Showprocesslist", $connect); while ($proc =mysql_fetch _ASSOC ($result)) {if ($proc ["Command"]== "Sleep" && $proc ["Time"]>max_sleep_time) {@mysql_query ("KILL". $ proc["Id"], $connect);}} Mysql_close ($connect);? >
Change the $password in it to your actual database password, and the time of the dead connection can be changed. Then you can join the scheduled task. For example, use the CRONTAB-E command to join:
*/2****php/usr/local/sbin/kill-mysql-sleep-proc.php
You can check and clear the dead connections in the database every 2 minutes.
Related recommendations:
How PHP generates MySQL data dictionary
PHP How to solve the MySQL storage data Chinese garbled
About MySQL super-long automatic truncation instance detailed