Use the PHP applet to clear Mysql dead connection _ MySQL

Source: Internet
Author: User
Using the PHP applet to clear Mysql dead connections in recent days, many dead connections always occur in the school's MySQL database, mainly because there are too many Sleep connections and the Time is very long, all available connections are occupied, so that other users cannot connect to the database. I started to consider adjusting MySQL database parameters, but many parameters have not solved this problem. So I thought of a tough way to write a php script and execute it every two minutes. I found that the connection was killed (more than 120 seconds, in this way, some programs will no longer be killed on the database server. The following is a Kill connection applet:

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 $ password in it to your actual database password, and the dead connection time can also be modified. Then join the scheduled task. For example, use the crontab-e command to add:

*/2 * php/usr/local/sbin/kill-mysql-sleep-proc.php

You can check and clear the dead connections in the database every 2 minutes.

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.