php connection pooling mysql



I am planning to use MYSQL. Is there a connection pooling extension available? Or what is the normal practice for connection? Is this the one used in every where...

mysqli_connect("localhost", "xxx", "xxx", "test");

Do people use just normal mysql_connect or pconnect..? How better is pconnect and what setting should I do for PConnect?


Posted 2009-05-06T17:09:56.430

Reputation: 2 932



have you ever used mysql_pconnect() ? mysql_pconnect() acts very much like mysql_connect() with two major differences.

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

This type of link is therefore called 'persistent'

Check it here


Posted 2009-05-06T17:09:56.430

Reputation: 5 855


Persistent connection support was introduced in PHP 5.3 for the mysqli extension. Support was already present in PDO MYSQL and ext/mysql. The idea behind persistent connections is that a connection between a client process and a database can be reused by a client process, rather than being created and destroyed multiple times. This reduces the overhead of creating fresh connections every time one is required, as unused connections are cached and ready to be reused.

Unlike the mysql extension, mysqli does not provide a separate function for opening persistent connections. To open a persistent connection you must prepend p: to the hostname when connecting.


sample code:
$GLOBALS["mysqli"] = new mysqli('p:localhost', 'username', 'password', 'db_name');

edit: Sorry for the dupe, didn't see the other answers.


Posted 2009-05-06T17:09:56.430

Reputation: 91


use the mysqli or the PDO extension instead of the old mysql extension.

you can tell the mysqli_connect or mysqli::__construct to use persistent connection, if you prefix your hostname with 'p:'


Posted 2009-05-06T17:09:56.430

Reputation: 145


This is an old question, but I wanted to add my two cents, as I was looking at this same issue. As of PHP 5.3, mysqli supports persistent connections, you just need to prepend p: to the front of the host name.

If you are running Apache, have you tried looking into connection pooling with mysql through the Apache mod_dbd module? It supports connection pooling for MySQL.


Posted 2009-05-06T17:09:56.430

Reputation: 21

Thanks for noting the 'p:' host prefix for persistent connections requires PHP version 5.3. Haven't found that little nugget anywhere else. – Bob Stein – 2013-01-08T02:31:37.527


There are 3 connection functions:

mysql_connect: normal connection, no pooling, you cannot execute stored procedures (just sql)

mysql_pconnect: pooled connection, you cannot execute stored procedures (just sql)

mysqli_connect: normal connection, no pooling, you can execute stored procedures (needs mysql 5 or higher)

mysqli_pconnect: DOES NOT EXIST. There is no built in connection function both handling stored procedures and pooling

My advice (via experience and surfing):

If you need stored procedures, omit pooling and use mysqli_connect

If you do not need stored procedures, use mysql_pconnect


Posted 2009-05-06T17:09:56.430

Reputation: 678


Not exactly an answer but i think you might also consider PHP Data Objects (PDO) And PDO for MySQL


Posted 2009-05-06T17:09:56.430

Reputation: 1 714