简体   繁体   中英

The connection to Mysql not closing

One of my site using Apache + PHP + Mysql did not releasing connections. 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:

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.

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

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); kept the script never die.

  2. there is an error in the code, and the connection not closed.

Add 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()

It will close the connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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