mysql_close和mysql_pconnect无法正常工作

时间:2016-07-27 11:38:45

标签: php mysql mysql-connect mysql-pconnect

我有这种情况:

[ file: config.php ]
$conn=mysql_connect(...
mysql_select_db

[ file: function.php ]
include('db.php');
function a()
{
mysql_query("INSERT INTO...");
}

[ file: index.php ]
include('function.php');
a();
mysql_close($conn);

在文件config.php中我是mysql_connect和mysql_select_db函数,在文件function.php中我有一个很长的函数,在" index.php"文件我调用此函数(执行INSERT查询),然后关闭mysql连接。

似乎一切都很好,但如果我用ApacheBenchmark测试该脚本的性能,我会看到很多睡眠连接和mysql达到max_user_connection的限制:

| 20592 | admin_tracker        | localhost       | admin_tracker | Sleep   | 0    |       |                  | 0.000    |
| 20593 | admin_tracker        | localhost       | admin_tracker | Sleep   | 0    |       |                  | 0.000    |
| 20594 | admin_tracker        | localhost       | admin_tracker | Sleep   | 0    |       |                  | 0.000    |
| 20595 | admin_tracker        | localhost       | admin_tracker | Sleep   | 0    |       |                  | 0.000    |
| 20596 | admin_tracker        | localhost       | admin_tracker | Sleep   | 0    |       |                  | 0.000    |
| 20597 | admin_tracker        | localhost       | admin_tracker | Sleep   | 0    |       |                  | 0.000    |

所以我使用了mysql_pconnect函数:

**首先,在连接时,该功能将首先尝试找到已使用相同主机,用户名和密码打开的(持久)链接。如果找到一个,将返回一个标识符,而不是打开一个新连接。

其次,当脚本的执行结束时,不会关闭与SQL服务器的连接。相反,链接将保持打开以供将来使用(mysql_close()不会关闭mysql_pconnect()建立的链接。)**

但问题仍然存在:

[Wed Jul 27 07:12:46.153812 2016] [:error] [pid 29741] [client XX.XX.XX.XX:43853] PHP Warning:  mysql_pconnect(): Too many connections in /home/admin/web/XXXXXXXXXXX/public_html/config.php on line 2
[Wed Jul 27 07:12:46.177555 2016] [:error] [pid 29744] [client XX.XX.XX.XX:43854] PHP Warning:  mysql_pconnect(): Too many connections in /home/admin/web/XXXXXXXXXXX/public_html/config.php on line 2
[Wed Jul 27 07:12:46.202182 2016] [:error] [pid 29738] [client XX.XX.XX.XX:43851] PHP Warning:  mysql_pconnect(): Too many connections in /home/admin/web/XXXXXXXXXXX/public_html/config.php on line 2
[Wed Jul 27 07:12:46.446825 2016] [:error] [pid 29729] [client XX.XX.XX.XX:43852] PHP Warning:  mysql_pconnect(): Too many connections in /home/admin/web/XXXXXXXXXXX/public_html/config.php on line 2

如果文件相同且mysql_pconnect使用相同的用户/密码/数据库,为什么会发生这种情况?

我该如何解决这个问题? 感谢

0 个答案:

没有答案