我有这种情况:
[ 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 使用相同的用户/密码/数据库,为什么会发生这种情况?
我怎么解决这个问题?谢谢