php 如何关闭未关闭的mysql连接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6320771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to close unclosed mysql connections?
提问by Jagadeesan
My clients hosting provider blocks the site because of too much unclosed mysql connections. the site was previously created by someone and currently I'm maintaining it.
由于未关闭的 mysql 连接太多,我的客户托管提供商阻止了该站点。该网站以前是由某人创建的,目前我正在维护它。
I have addded the mysql_close
function at the end of the pages. It is closing the connections good but still I'm getting some connections left unclosed. I may left some where..
我mysql_close
在页面末尾添加了该功能。它很好地关闭了连接,但我仍然有一些连接未关闭。我可能会留下一些地方..
What I need is to close the unclosed mysql connections in the server using a cron file or some thing..
我需要的是使用 cron 文件或其他东西关闭服务器中未关闭的 mysql 连接。
What do I have to do?
我需要做什么?
Is it possible to close all the connections at once? if so, how?
是否可以一次关闭所有连接?如果是这样,如何?
采纳答案by Nanne
Are you using persistent connections? IF not, then you really shouldn't worry too much about closing your connections. From the 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()
通常是不必要的,因为非持久打开链接在脚本执行结束时自动关闭。另请参阅释放资源。
Instead of too_much unclosed connections, couldn't it be (which is essentially the same ofcourse) that you have too many open connections? For instance, too many users on your site at once?
不是太多未关闭的连接,难道不是(当然本质上是相同的)您有太多打开的连接吗?例如,您网站上的用户太多?
If you do have persistent connections, do not forget this:
如果您确实有持久连接,请不要忘记:
mysql_close() will not close persistent links created by mysql_pconnect().
mysql_close() 不会关闭由 mysql_pconnect() 创建的持久链接。
As said in the comment, it is highly unlikely that a mysql_connect()
resource is not freed at the end of the script. From another manual page
正如评论中所说mysql_connect()
,在脚本末尾没有释放资源的可能性很小。从另一个手册页
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.
释放资源
多亏了 PHP 4 Zend Engine 引入的引用计数系统,没有更多引用的资源会被自动检测到,并被垃圾收集器释放。因此,很少需要手动释放内存。
注意:持久数据库链接是此规则的一个例外。它们不会被垃圾收集器销毁。有关更多信息,请参阅持久连接部分。
There could be a sidenote however, from the comments on the mysql_close
page
然而,可能有一个旁注,从页面上的评论mysql_close
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.
至少在 PHP5.3.2 和 Windows 通过 tcp 连接时,你应该总是使用这个 mysql_close() 函数来关闭和释放 PHP 正在使用的 tcp 套接字。脚本执行后的垃圾收集不会自行关闭 tcp 套接字。否则套接字将保持在“等待”状态大约 30 秒,并且任何额外的页面加载/连接尝试只会增加打开的 tcp 连接总数。此等待时间似乎无法通过 PHP 设置进行配置。
回答by jishi
mysql_connect would return an identifier for the actual connection, which you can use in your mysql_close call.
mysql_connect 将返回实际连接的标识符,您可以在 mysql_close 调用中使用该标识符。
$conn1 = mysql_connect(.....);
mysql_close($conn1);
You need to use the identifier if the page itself opens more than one connection, because otherwise mysql_close() would only close the last opened connection.
如果页面本身打开多个连接,则需要使用标识符,否则 mysql_close() 只会关闭最后打开的连接。
But as Nanne said, PHP usually cleans up connections after itself after page execution, so the question is if you don't close them, or if you open too many simultaneously. Usually you would only need 1 connection per request, unless you open and iterate through multiple resultsets at the same time.
但是正如 Nanne 所说,PHP 通常会在页面执行后自行清理连接,所以问题是您是否不关闭它们,或者您是否同时打开了太多连接。通常每个请求只需要 1 个连接,除非您同时打开和迭代多个结果集。
回答by BugFinder
If you are getting some unclosed connections, chances are you missed some pages, maybe via includes etc, or functions calling "exit" so you dont reach your close code.
如果您获得一些未关闭的连接,您可能错过了一些页面,可能是通过包含等,或调用“退出”的函数,因此您无法到达关闭代码。
I dont believe you can boot other connections from mysql using cron.
我不相信您可以使用 cron 从 mysql 启动其他连接。