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.