makenoiz
makenoiz

Reputation: 351

Reducing number of MySQL processes

I need to try to understand how MySQL processes/connections work. I have googled and dont see anything in laymans terms so I'm asking here. Here is the situation.

Our host is giving us grief over "too many MySQL processes". We are on a shared server. We are allowed .2 of the server mySQL processes - which they claim is 50 connections - and they say we are using .56.

From the technical support representative:

"Number of MySQL procs (average) - 0.59 meant that you were using 0.59% of the total MySQL connections available on the shared server. The acceptable value is 0.20 which is 50 connections. "

Here is what we are running:

Zen Cart:           1.5.1  35K products. Auto updating of 1-20
                           products every 10 hours via cron.  
PHP version         5.3.16  
MySQL version       5.1.62-cll  
Architecture        i686  
Operating system    linux 

We generally have about 5000 hits per day on the site and Google bot loves to visit even though I have the crawl rate set to minimum in Google webmaster tools.

I'm hoping someone can explain MySQL processes to me in terms of what this host is talking about. Every time I ask them I get an obfuscated answer that is vague and unclear. Is a new MySQL process created every time a visitor visits the site? That does not seem right.

According to the tech we were using 150 connections at that particular time.

EDIT: here is the connection function in zencart

  function connect($zf_host, $zf_user, $zf_password, $zf_database, $zf_pconnect = 'false', $zp_real = false) {
$this->database = $zf_database;
$this->user = $zf_user;
$this->host = $zf_host;
$this->password = $zf_password;
$this->pConnect = $zf_pconnect;
$this->real = $zp_real;
if (!function_exists('mysql_connect')) die ('Call to undefined function: mysql_connect().  Please install the MySQL Connector for PHP');
$connectionRetry = 10;
while (!isset($this->link) || ($this->link == FALSE && $connectionRetry !=0) )
{
  $this->link = @mysql_connect($zf_host, $zf_user, $zf_password, true);
  $connectionRetry--;
}
if ($this->link) {
  if (@mysql_select_db($zf_database, $this->link)) {
    if (defined('DB_CHARSET') && version_compare(@mysql_get_server_info(), '4.1.0', '>=')) {
      @mysql_query("SET NAMES '" . DB_CHARSET . "'", $this->link);
      if (function_exists('mysql_set_charset')) {
        @mysql_set_charset(DB_CHARSET, $this->link);
      } else {
        @mysql_query("SET CHARACTER SET '" . DB_CHARSET . "'", $this->link);
      }
    }
    $this->db_connected = true;
    if (getenv('TZ') && !defined('DISABLE_MYSQL_TZ_SET')) @mysql_query("SET time_zone = '" . substr_replace(date("O"),":",-2,0) . "'", $this->link);
    return true;
  } else {
    $this->set_error(mysql_errno(),mysql_error(), $zp_real);
    return false;
  }
} else {
  $this->set_error(mysql_errno(),mysql_error(), $zp_real);
  return false;
}

Upvotes: 1

Views: 1186

Answers (1)

halfer
halfer

Reputation: 20467

I wonder if it is a problem with connection pooling. Try changing this line:

$this->link = @mysql_connect($zf_host, $zf_user, $zf_password, true);

to this:

$this->link = @mysql_connect($zf_host, $zf_user, $zf_password);

The manual is useful here - the forth parameter is false by default, but your code is forcing it to be true, which creates a new connection even if an existing one is already open (this is called connection pooling and saves creating new connections unnecessarily i.e. saves both time and memory).

I would offer a caveat though: modifying core code in a third-party system always needs to be done carefully. There may be a reason for the behaviour they've chosen, though there's not much in the way of comments to be able to tell. It may be worth asking a question via their support channels to see why it works this way, and whether they might consider changing it.

Upvotes: 1

Related Questions