【问题标题】:Mysql creates new thread connection on each refresh pageMysql 在每次刷新页面创建新的线程连接
【发布时间】:2017-07-22 15:40:49
【问题描述】:

当我刷新 php 页面时,mysql 创建了新的线程连接,由于 max_user_connection 的限制,这会导致共享主机上的问题。限制为 30,我刷新页面 30 次,mysql 停止并显示User already has more than 'max_user_connections' active connections。 我认为我设置正确但找不到解决方案。

db.php

$host="localhost";
$user="root";
$pass="";
$db="terind";
mysql_connect($host,$user,$pass);
mysql_select_db($db);
mysql_query("set names 'utf8'");

head.php

require("db.php");

...codes...

end.php

mysql_close();

index.php

require("head.php");
$check=mysql_fetch_assoc(...);
echo $check['index'];
require("end.php");

我该如何解决这个问题?

【问题讨论】:

  • 停止使用mysql_*函数。它们已被弃用,并且不支持准备好的语句(您应该使用它们)。此外,当脚本终止时,连接也应该被删除,除非您的共享主机由于某种原因使脚本保持活动状态。尝试在end.php 末尾添加die() 看看是否有帮助。
  • die(mysql_error()) 没有显示任何错误

标签: php mysql multithreading connection database-connection


【解决方案1】:

您可以通过不直接访问数据库,而是使用某种连接池来修复它。这在共享主机环境中可能不太容易做到。

您可以检查是否可以运行,例如工作进程数量有限的 PHP-FPM 模式。您可以要求托管服务提供商将网络服务器工作人员的数量限制为允许的连接数,或者将连接数调整为工作人员的数量。

【讨论】:

    猜你喜欢
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    • 2017-08-04
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多