mysql_close and mysql_pconnect not working

555 Views Asked by At

I have this scenario:

[ file: config.php ]
$conn=mysql_connect(...
mysql_select_db

[ file: function.php ]
include('db.php');
function a()
{
mysql_query("INSERT INTO...");
}

[ file: index.php ]
include('function.php');
a();
mysql_close($conn);

in the file config.php I've the mysql_connect and mysql_select_db function, in file function.php I have a very long function, in "index.php" file I call this function (that execute an INSERT query) and after that, I close the mysql connection.

It seems that all was fine, but if I test the performance of that script with ApacheBenchmark, I see a lot of sleep connection and mysql reach the limit of max_user_connection:

| 20592 | admin_tracker        | localhost       | admin_tracker | Sleep   | 0    |       |                  | 0.000    |
| 20593 | admin_tracker        | localhost       | admin_tracker | Sleep   | 0    |       |                  | 0.000    |
| 20594 | admin_tracker        | localhost       | admin_tracker | Sleep   | 0    |       |                  | 0.000    |
| 20595 | admin_tracker        | localhost       | admin_tracker | Sleep   | 0    |       |                  | 0.000    |
| 20596 | admin_tracker        | localhost       | admin_tracker | Sleep   | 0    |       |                  | 0.000    |
| 20597 | admin_tracker        | localhost       | admin_tracker | Sleep   | 0    |       |                  | 0.000    |

So I used the mysql_pconnect function:

**First, when connecting, the function would first try to find a (persistent) link that's already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection.

Second, the connection to the SQL server will not be closed when the execution of the script ends. Instead, the link will remain open for future use (mysql_close() will not close links established by mysql_pconnect()).**

But the problem persist:

[Wed Jul 27 07:12:46.153812 2016] [:error] [pid 29741] [client XX.XX.XX.XX:43853] PHP Warning:  mysql_pconnect(): Too many connections in /home/admin/web/XXXXXXXXXXX/public_html/config.php on line 2
[Wed Jul 27 07:12:46.177555 2016] [:error] [pid 29744] [client XX.XX.XX.XX:43854] PHP Warning:  mysql_pconnect(): Too many connections in /home/admin/web/XXXXXXXXXXX/public_html/config.php on line 2
[Wed Jul 27 07:12:46.202182 2016] [:error] [pid 29738] [client XX.XX.XX.XX:43851] PHP Warning:  mysql_pconnect(): Too many connections in /home/admin/web/XXXXXXXXXXX/public_html/config.php on line 2
[Wed Jul 27 07:12:46.446825 2016] [:error] [pid 29729] [client XX.XX.XX.XX:43852] PHP Warning:  mysql_pconnect(): Too many connections in /home/admin/web/XXXXXXXXXXX/public_html/config.php on line 2

Why that happen if the file are the same and the mysql_pconnect use the same user/password/database?

How can I solve this problem? Thanks

0

There are 0 best solutions below