Mysql sleep has too many optimization methods

Source: Internet
Author: User
Tags pconnect

Mysql sleep has too many optimization methods
The solution is: Open include/pub_db_mysql.php and run the following code:

// Connect to the database
If ($ pconnect ==- 100) {$ this-> linkID = @ mysql_connect ($ this-> dbHost, $ this

-> DbUser, $ this-> dbPwd );}
Else {$ this-> linkID = @ mysql_pconnect ($ this-> dbHost, $ this-> dbUser, $ this-

> DbPwd );}

Replace it with the following code:

// Connect to the database
// If ($ pconnect ==- 100) {$ this-> linkID = @ mysql_connect ($ this-

> DbHost, $ this-> dbUser, $ this-> dbPwd );}
// Else {$ this-> linkID = @ mysql_pconnect ($ this-> dbHost, $ this-> dbUser, $ this-

> DbPwd );}

// Directly use connect
$ This-> linkID = @ mysql_connect ($ this-> dbHost, $ this-> dbUser, $ this-> dbPwd );

Save and check the mysql process again. OK?
It is not necessarily the best choice to use the pconnect () link instead of connect.

Mysql is in a false dead state, and there are too many sleep processes. As a result, the system was completed for dozens of users.
In fact, this is the problem of mysql configuration, the default linux, mysql configuration is my-large.cnf

Configuration, which is suitable for large servers. High memory, such as 2G and 4G memory, is suitable for use, but generally 512 M

Memory will not work. It will occupy MB of memory to save the mysql processes of the system. These processes have long-term heap quality,

Does not release, resulting in slow system. So, change the configuration to a my-small.cnf, the small configuration can be
#! /Bin/sh
While:
 
Do
N = '/usr/bin/mysqladmin processlist | grep-I sleep | wc-l'
Date = 'date + % Y % m % d [% H: % M: % S]'
Echo $ n
 
If ["$ n"-gt 10]
Then
For I in '/usr/bin/mysqladmin processlist | grep-I sleep | awk' {print

$2 }''
Do
/Usr/bin/mysqladmin kill $ I
Done
Echo "sleep is too I killed it" & gt;/tmp/sleep. log
Echo "$ date: $ n" & gt;/tmp/sleep. log
Fi
Sleep 5
Done

Method 2

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.

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.