How to close unclosed mysql connections?

My client-hosting provider is blocking the site due to too many open mysql connections. The site was previously created by someone, and I currently support it.

I added the mysql_close function at the end of the pages. It closes the connections well, but still I get some connections left unclosed. I can leave some where ..

I need to close closed mysql connections on the server using a cron file or something like that.

What should I do?

Can all connections be closed immediately? if so, how?

+6
source share
3 answers

Do you use persistent connections? IF not, then you really shouldn't worry too much about closing your connections. From manual

Using mysql_close() usually not needed, since fake open links are automatically closed at the end of the script. Seeing also freeing up resources.

Instead of too many open connections, could it not be (which is essentially the same) that you have too many open connections? For example, too many users on your site at once?

If you have persistent connections, don't forget about this:

mysql_close () will not close permalinks created by mysql_pconnect ().

As the comment says, it is unlikely that the mysql_connect() resource is not freed at the end of the script. From another page

Release of resources

Thanks to the reference counting system with the PHP 4 Zend Engine, a resource of no more links to it is detected automatically, and it is freed by the garbage collector. For this reason, it is rarely required to free memory manually.

Note. Permanent database links are an exception to this rule. They are not destroyed by the garbage collector. See the Persistent Connections section for more information.

However, there may be a side effect from the comments on the mysql_close page mysql_close

At least with PHP5.3.2 and Windows tcp connection, you should always use this function mysql_close () to close and release the tcp connector used by PHP. Garbage collection after script execution does not close tcp socket on its own. The socket would otherwise remain idle for approximately 30 seconds, and any additional download / connection pages of an attempt would only add to the sum the number of open tcp connections. This latency does not seem to be configured using PHP settings.

+6
source

mysql_connect will return an identifier for the actual connection, which you can use in your mysql_close call.

 $conn1 = mysql_connect(.....); mysql_close($conn1); 

You need to use an identifier if the page itself opens multiple connections, because otherwise mysql_close () closes only the last open connection.

But, as Nanna said, PHP usually cleans up the connections after itself after the page is executed, so the question is if you don’t close them, or if you open too much at the same time. Usually you only need one connection for each query, unless you open and repeat multiple result sets at the same time.

+2
source

If you get some closed connections, you may have missed some pages, maybe through include, etc., or functions that call exit to prevent you from reaching your closed code.

I do not believe that you can load other connections from mysql using cron.

0
source

Source: https://habr.com/ru/post/890329/


All Articles