Use PHP program to clear MySQL dead connection

Source: Internet
Author: User
Tags join php script sleep mysql database

In recent days, the school's MySQL database has always been a lot of dead connections, mainly because 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:

Download:

kill-mysql-sleep-proc.php

Define (' Max_sleep_time ', 120);

$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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.