The example of this article describes the method of PHP implementation to clear MySQL dead connection. Share to everyone for your reference, specific as follows:
Connection, the main performance is that there are too many sleep connections, and time is very long, full of all the 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 changing many of the parameters still didn't solve the problem. So think of a more ruthless way, write a PHP script, every 2 minutes, found dead connection (more than 120 seconds) to kill, so that no longer let some programs to kill the database server, the following is a kill the connection of the small program:
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 modified. Then you can join the planning 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 a dead connection in the database every 2 minutes.
More about PHP Interested readers can view the site topics: "PHP+MYSQLI Database Programming Skills Summary", "PHP based on PDO Operation Database Skills summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", " Introduction to PHP object-oriented programming, "PHP string (String) Usage Summary", "Php+mysql Database Operations Introduction Tutorial" and "PHP Common database Operation tips Summary"
I hope this article will help you with the PHP program design.