php 关闭mysql连接重要吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/880885/
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
Is closing the mysql connection important?
提问by outis
Is it crucial to close mysql connections efficiency wise, or does it automatically close after php file has run?
明智地关闭mysql连接是否至关重要,还是在php文件运行后自动关闭?
回答by outis
From the documentation:
从文档:
Note: The link to the server will be closed as soon as the execution of the script ends, unless it's closed earlier by explicitly calling mysql_close().
注意:一旦脚本执行结束,到服务器的链接将立即关闭,除非它提前通过显式调用 mysql_close() 关闭。
If your script has a fair amount of processing to perform after fetching the result and has retrieved the full result set, you definitely should close the connection. If you don't, there's a chance the MySQL server will reach it's connection limit when the web server is under heavy usage. If you can't close the MySQL connection until near the end of the script, it's cleaner though unnecessary to do so explicitly.
如果您的脚本在获取结果后需要执行大量处理并检索到完整的结果集,则您绝对应该关闭连接。如果不这样做,当 Web 服务器使用量很大时,MySQL 服务器可能会达到它的连接限制。如果您在脚本接近尾声之前无法关闭 MySQL 连接,尽管没有必要明确这样做,但它会更清晰。
I'm not certain how fastcgi affects things. One pageclaims that a build of PHP that supports fastcgi will create persistent connections, even for mysql_connect. This contradicts the documentation in that the connection is closed when the process, rather than the script, ends. Rather than testing it, I'm going to recommend using mysql_close(). Actually, I recommend using PDO, if it's available.
我不确定 fastcgi 如何影响事情。 一个页面声称支持 fastcgi 的 PHP 版本将创建持久连接,即使是 mysql_connect。这与文档相矛盾,因为连接在进程而不是脚本结束时关闭。与其进行测试,不如推荐使用 mysql_close()。实际上,我建议使用PDO(如果可用)。
回答by ist_lion
Is it crucial? Not so much
关键吗?没那么多
Is it considered to be a good practice to follow? Yes.
遵循它是否被认为是一个好习惯?是的。
I don't see why you wouldn't want to close it.
我不明白你为什么不想关闭它。
回答by jess
When using something like cgi, it's completely unnecessary to close your mysql connections since they close automatically at the end of script execution. When using persistent technologies like mod_perl and others, which maintain your connections between requests, then it's important to keep track of connections, global variables, etc..
使用 cgi 之类的东西时,完全没有必要关闭 mysql 连接,因为它们会在脚本执行结束时自动关闭。当使用诸如 mod_perl 和其他持久性技术来维护请求之间的连接时,跟踪连接、全局变量等很重要。
Basically, for persistent data, clean up after yourself. For trivial, non-persistent data, it'll all go away when the request finishes anyway. Either way, best practice is to always close your connections.
基本上,对于持久数据,请自行清理。对于琐碎的、非持久性的数据,不管怎样,当请求完成时它都会消失。无论哪种方式,最佳做法是始终关闭您的连接。
回答by neal aise
Gets closed as soon as the script completes execution. Unless you've opened a persistent connection. Ideally you should release a resource (a connection here) as soon as you are done with it. Unless there is a good chance that you will be needing it again very soon in the execution.
脚本完成执行后立即关闭。除非您打开了持久连接。理想情况下,您应该在完成后立即释放资源(此处为连接)。除非您很有可能在执行过程中很快再次需要它。
Connection pooling or using persistent connections (if that's what you meant) is a good idea if you are behind a single database server. However if there are more servers and you are load balancing, it might hurt the distribution of work. Typically some clients run heavy queries while others run lighter ones. So if the same connection is used over n over, some servers would hit heavy load while others would be under utilized. Consider using smaller ttls and variable connection pool size.
如果您在单个数据库服务器后面,连接池或使用持久连接(如果这就是您的意思)是一个好主意。但是,如果有更多服务器并且您正在负载平衡,则可能会影响工作分配。通常,一些客户端运行大量查询,而其他客户端运行较轻的查询。因此,如果在 n 次以上使用相同的连接,则某些服务器将承受沉重的负载,而其他服务器则未得到充分利用。考虑使用较小的 ttls 和可变连接池大小。
回答by itoctopus
Most CMSs close the MySQL connection at the end of the request, which is really meaningless, because PHP will do it anyway.
大多数CMS在请求结束时关闭MySQL连接,这确实没有意义,因为无论如何PHP都会这样做。
However, if you have a script where the connection is no longer needed say towards the middle of the script, and then other heavy activities take place, then it's a good idea to explicitly close the connection. This will free some resources.
但是,如果您有一个不再需要连接的脚本,比如在脚本中间,然后发生其他繁重的活动,那么明确关闭连接是个好主意。这将释放一些资源。
Now, much has been said about the benefits of closing a connection, but nearly nothing has been said about the benefits of not closing it. Essentially, if you do not close the connection at the end of a script, then you really are saving some resources. Imagine a web application (or any application) receiving 100 pageviews/second. So, every second, you will need to invoke mysqli_close100 times - which means that in every second, you have 100 unnecessary roundtrips to the database server to close the open connections. From a performance perspective, this is pure overhead, since PHP will check for open connections when the script is finished anyway and will close those connections, and it might be that, because everything happens so quickly, that PHP doesn't see that you have closed those connections and will try to close them again.
现在,关于关闭连接的好处已经说了很多,但几乎没有说不关闭连接的好处。本质上,如果您没有在脚本结束时关闭连接,那么您确实是在节省一些资源。想象一个 Web 应用程序(或任何应用程序)每秒接收 100 次浏览量。因此,每一秒,您都需要调用mysqli_close100 次——这意味着每一秒,您有 100 次不必要的往返数据库服务器来关闭打开的连接。从性能的角度来看,这是纯粹的开销,因为 PHP 会在脚本完成后检查打开的连接并关闭这些连接,而且可能是因为一切发生得太快,PHP 没有看到你有关闭这些连接并将尝试再次关闭它们。
Note: the answer above assumes that you are not using persistent connections (persistent connections are not used in any of the major CMSs).
注意:上面的答案假设您没有使用持久连接(在任何主要 CMS 中都没有使用持久连接)。

