Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close unclosed mysql connections?

Tags:

php

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?

like image 755
Jagadeesan Avatar asked Jun 12 '11 07:06

Jagadeesan


People also ask

How do I close all MySQL connections?

Run the following command: mysql> SELECT GROUP_CONCAT(CONCAT('KILL ',id,';') SEPARATOR ' ') FROM information_schema. processlist WHERE user <> 'system user'; This will kill all your MySQL queries.

How do I close a connection in MySQL workbench?

To quit MySQL Workbench select options File->Exit in a manu at the topmost line of MySQL Workbench window.

Should I close MySQL connection after every query?

So when you open a lot of connections (after closing the old ones) that might make your application a bit slower. So I'd always opt for opening a conection once at the start and close it after all queries have been processed.


1 Answers

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.

like image 57
Nanne Avatar answered Oct 22 '22 11:10

Nanne