MySQL sleep for too many optimization methods
The solution is: Open include/pub_db_mysql.php about line 73rd, and put the following section of code:
Connecting to a 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 with the following code:
Connecting to a 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); }
Direct Use Connect
$this->linkid = @mysql_connect ($this->dbhost, $this->dbuser, $this->dbpwd);
Save, and then look at the MySQL process. Is this OK?
The official waiver of Connect () and the use of Pconnect () link is not necessarily the best choice.
MySQL suspended animation state, the sleep process too much and so on are like. Led to dozens of users, the system is done.
In fact, this is a MySQL configuration problem, the default Linux, MySQL configuration is my-large.cnf
Configuration, which is suitable for large servers. There is high memory, such as 2g,4g memory, suitable for use, while the general 512M
The memory is not. It consumes 512M of memory to save the process of the system MySQL, which has a long heap of quality,
Does not release, causing the system to slow down. So, change the configuration to become my-small.cnf, small configuration is OK
#!/bin/sh
While:
Todo
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
$} '
Todo
/usr/bin/mysqladmin Kill $i
Done
echo "Sleep is too many I killed it" >> /tmp/sleep.log
echo "$date: $n" >> /tmp/sleep.log
Fi
Sleep 5
Done
Method Two
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. And then
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 a dead connection in the database every 2 minutes.