简体   繁体   English

与Mysql的连接没有关闭

[英]The connection to Mysql not closing

One of my site using Apache + PHP + Mysql did not releasing connections. 我的一个使用Apache + PHP + Mysql的网站没有发布连接。 The connection number kept growing. 连接数量不断增长。 And the connection status is sleep. 并且连接状态是睡眠。

| 138 | root | localhost       | NULL  | Sleep   |  183 |       | NULL             |
| 140 | root | localhost:50488 | A | Sleep   |  256 |       | NULL             |
| 142 | root | localhost:50512 | A | Sleep   |  253 |       | NULL             |
| 151 | root | localhost:50668 | A | Sleep   |  222 |       | NULL             |
| 152 | root | localhost:50684 | A | Sleep   |  221 |       | NULL             |
| 155 | root | localhost:50714 | A | Sleep   |  214 |       | NULL             |
| 157 | root | localhost:50751 | A | Sleep   |  207 |       | NULL             |

Check which connected to mysql using port 50488: 使用端口50488检查哪个连接到mysql:

COMMAND   PID   USER   FD   TYPE    DEVICE SIZE/OFF NODE NAME
mysqld  17147  mysql   15u  IPv4 150696739      0t0  TCP localhost:mysql->localhost:50488 (ESTABLISHED)
httpd   17487 apache   18u  IPv4 150696738      0t0  TCP localhost:50488->localhost:mysql (ESTABLISHED)

It is ridiculous since PHP closing the connection when script end automatically. 自从PHP在脚本自动结束时关闭连接是荒谬的。

Any reason may cause this problem? 任何原因都可能导致这个问题?

Are you using persistent connections? 你使用持久连接吗? Don't use it. 不要使用它。

More info: http://www.php.net/manual/en/features.persistent-connections.php 更多信息: http//www.php.net/manual/en/features.persistent-connections.php

And, from my own experience, once we had the very same problem, and the solution was to change persistent connections to normal connections. 而且,根据我自己的经验,一旦我们遇到了同样的问题,解决方案就是将持久连接更改为正常连接。 I don't remember the details. 我不记得细节。 This may be your case, or not. 这可能是你的情况,或不是。

The problem is caused by 问题是由

  1. @ignore_user_abort(TRUE); @ignore_user_abort(TRUE); kept the script never die. 让剧本永远不会死。

  2. there is an error in the code, and the connection not closed. 代码中有错误,连接未关闭。

Add mysql_close($connection); 添加mysql_close($connection); at the end of query... If you have multiple query in one file, add it at the end of the file only, like this: 在查询结束时...如果您在一个文件中有多个查询,请将其添加到文件的末尾,如下所示:

</body>
</html>
<?php
    mysql_close($connection);
?>

* WHERE $connection is the variable gived to the mysql_connect() * WHERE $connection是赋予mysql_connect()的变量

It will close the connection. 它将关闭连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM