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

标签 php mysql

由于未关闭的 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/

相关文章:

PHP getimagesize 空输出

php - 如何知道最近对 MySQL 创建的条目的 id(自增)?

PHP Foreach 循环使用单个数组从 XML 到 MySQL

sql - 帮助删除 mysql 文件排序

mysql - 在 MySQL 中创建一个用户定义的函数来处理 auto_increment

php - PHP 中的突出显示搜索关键字无法正确突出显示

javascript - 如何在 html 中设置文本脚本的样式?

php - 在 Sonata-Admin 全局搜索中搜索哪些列

mysql - 返回一周中每一天的数据(每行 7 条记录)- MySQL

php - 如何在 laravel 中使用模型调用参数化存储过程