mysql如何关闭连接,如何关闭未关闭的mysql连接?

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.

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..

What I need is to close the unclosed mysql connections in the server using a cron file or some thing..

What do I have to do?

Is it possible to close all the connections at once? if so, how?

解决方案

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.

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

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

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.

There could be a sidenote however, from the comments on the 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值