Apache PHP MySQL PHP implementation method to clear the dead connection of MySQL

Source: Internet
Author: User
Tags apache php php mysql php programming
The example in this paper describes how PHP is implemented to clear the MySQL dead connection. Share to everyone for your reference, as follows:

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"; $c $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.

More interested in PHP related content readers can view this site topic: "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", " PHP Primer for object-oriented programming, PHP string Usage Summary, Introduction to PHP+MYSQL database operations, and PHP common database operations Tips Summary

I hope this article is helpful to you in PHP programming.

The above describes the Apache PHP MySQL PHP implementation of the removal of the MySQL connection method, including the Apache PHP MySQL content, I hope to be interested in PHP tutorial friends helpful.

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