MySQL sleep for too many optimization methods

Source: Internet
Author: User
Tags pconnect sleep

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.

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.