gpt4 book ai didi

php - 如何关闭未关闭的mysql连接?

转载 作者:可可西里 更新时间:2023-11-01 06:37:03 26 4
gpt4 key购买 nike

由于未关闭的 mysql 连接太多,我的客户托管提供商阻止了该站点。该站点以前由某人创建,目前由我维护。

我在页面末尾添加了 mysql_close 函数。它关闭连接很好,但我仍然有一些连接未关闭。我可能会离开一些地方..

我需要的是使用 cron 文件或其他东西关闭服务器中未关闭的 mysql 连接..

我需要做什么?

是否可以一次关闭所有连接?如果是这样,怎么做?

最佳答案

您是否使用持久连接?如果不是,那么您真的不必太担心关闭连接。来自manual

Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution. See also freeing resources.

不是有太多未关闭的连接,难道不是(当然本质上是一样的)你有太多打开的连接吗?例如,您的网站上同时有太多用户?

如果你确实有持久连接,不要忘记这一点:

mysql_close() will not close persistent links created by mysql_pconnect().

如评论中所述,mysql_connect() 资源极不可能在脚本末尾未被释放。来自另一个manual page

Freeing resources

Thanks to the reference-counting system introduced with PHP 4's Zend Engine, a resource with no more references to it is detected automatically, and it is freed by the garbage collector. For this reason, it is rarely necessary to free the memory manually.

Note: Persistent database links are an exception to this rule. They are not destroyed by the garbage collector. See the persistent connections section for more information.

然而,从关于 mysql_close page 的评论中可能会有一个旁注

At least with PHP5.3.2 and Windows connecting by tcp, you should always use this mysql_close() function to close and free up the tcp socket being used by PHP. Garbage collection after script execution does not close the tcp socket on its own. The socket would otherwise remain in 'wait' state for approximately 30 seconds, and any additional page loads/connection attempts would only add to the total number of open tcp connections. This wait time does not appear to be configurable via PHP settings.

关于php - 如何关闭未关闭的mysql连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6320771/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com