mysqli使用p:connect选项打开多个新进程

mysqli使用p:connect选项打开多个新进程

问题描述:

This is my first time asking a question on here. I've scoured Google, stackoverflow, etc. looking for help with the issue I'm having. We're currently using PHP 5.3.10 & MySQL 5.0.95 with Apache 2.2.21 (CentOS).

We're in the process of starting to cut over from the old mysql library to mysqli in new code, and I'm leading the charge. I've tried

  • making sure I explicitly close the connection to the database when I'm done with it
  • freeing result sets when I'm done with them
  • upping the connection limit to 250 from 150

There are included files (having to do with session checking, etc.) that use the old style mysql_pconnect() to validate certain things. These are included in nearly all of our code.

something like the code:

$mysqli =  new mysqli('p:'.DBHOST, DBUSER, DBPASS, $_SESSION['dbname']);
if ($mysqli->connect_error) {
    throw new Exception($mysqli->connect_error,  $mysqli->connect_errno);
    exit;
}
// do my stuff here, a bunch of SQL queries like:
$sql = 'SELECT * FROM MyTable';
$result = $mysqli->query($sql);
if (!$result) {
    throw new SQLException($sql, $mysqli);
    exit;  
    // SQLException is an extension to mysqli_sql_exception that adds the 
    // query into the messaging internally
}
while ($result && $row = $result->fetch_assoc()) {
// do stuff here, like show it on screen, etc., all works normally
}
$result->free(); // free up the result
$mysqli->close(); // close the connection to the database

freeing the results and closing the connection were things I did after getting a "Too many connections" error. Before doing that, I would get 3-4 new database connections each time I ran my program. (viewed in the back end with SHOW PROCESSLIST)

The problem is lessened somewhat (it adds 0 to 3 new connections, rather than 3 new connections each time).

Some of my reading suggests that this could have something to do with Apache threading + the new persistent connections added if there are no existing idle ones in the current thread. Is it this? Are persistent connections not supported well with mysqli? (should I give up on persistence?)

Thanks for any suggestions you might have.

这是我第一次在这里提问。 我已经搜索过Google,stackoverflow等等,正在寻找我正在解决的问题的帮助。 我们目前正在使用PHP 5.3.10& MySQL 5.0.95与Apache 2.2.21(CentOS)。 p>

我们正在开始从旧的mysql库切换到新代码中的mysqli,我就是 领导这项指控。 我已经尝试了 p>

  • 确保在完成后明确关闭与数据库的连接 li>
  • 释放结果集时 我已经完成了它们 li>
  • 将连接限制从150提升到250 li> ul>

    包含文件(与会话有关) 检查,使用旧样式mysql_pconnect()来验证某些事情。 这几乎包含在我们所有代码中。 p>

    类似代码: p>

      $ mysqli = new mysqli('p:'  .DBHOST,DBUSER,DBPASS,$ _SESSION ['dbname']); 
    if($ mysqli-> connect_error){
     throw new Exception($ mysqli-> connect_error,$ mysqli-> connect_errno); 
     退出; 
    } 
     //在这里做我的东西,一堆SQL查询,如:
     $ sql ='SELECT * FROM MyTable'; 
     $ result = $ mysqli-> query($ sql); \  nif(!$ result){
     throw new SQLException($ sql,$ mysqli); 
     exit;  
     // SQLException是mysqli_sql_exception的扩展,它将
     //查询添加到内部消息中
    } 
    而($ result&& $ row = $ result-> fetch_assoc()){
     /  /在这里做东西,比如在屏幕上显示等,一切正常
    } 
     $ result-> free();  //释放结果
     $ mysqli-> close();  //关闭与数据库的连接
      code>  pre> 
     
     

    释放结果并关闭连接是我收到“Too many connections”错误后所做的事情。 在此之前,每次运行程序时,我都会获得3-4个新的数据库连接。 (在SHOW PROCESSLIST的后端查看) p>

    问题有所减轻(它增加了0到3个新连接,而不是每次增加3个新连接)。 p>

    我的一些阅读建议,如果当前线程中没有现有的空闲连接,这可能与Apache线程+添加新的持久连接有关。 是这个吗? mysqli是否支持持久连接? (我应该放弃持久性吗?) p>

    感谢你提出任何建议。 p> div>

I have no experience with mysqli persistent connections but some of your questions and expectations looks strange to me.

mysqli opens multiple new processes with p: connect option

Yes, that's what permanent connections are for

making sure I explicitly close the connection to the database when I'm done with it

You cannot make sure you closed it explicitly as you just can't do that. Again because the only point of permanent connection is to lasts open

I would get 3-4 new database connections each time I ran my program.

So, you have to make sure you're opening only one.

  • You have at least two connects from your script - one old style and some from mysqli?
  • How many mysqli objects being instantiated?
  • How many php scripts being run to serve one HTTP request? Are you sure?

After all, if it bothers you so much, why you're using persistent connections? Are you any real (not imaginary) benefits from it? After all, if your version is 5.3, why bother with rewriting from mysql at all?