19

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?

Uli Köhler
  • 13,012
  • 16
  • 70
  • 120
coool
  • 8,085
  • 12
  • 60
  • 80

6 Answers6

23

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

Uli Köhler
  • 13,012
  • 16
  • 70
  • 120
Sadegh
  • 6,654
  • 4
  • 34
  • 44
10

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.

source: http://www.php.net/manual/en/mysqli.persistconns.php

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

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

Benedict
  • 101
  • 1
  • 2
4

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:'

http://php.net/manual/en/mysqli.construct.php

madth3
  • 7,275
  • 12
  • 50
  • 74
Tyrael
  • 154
  • 1
  • 7
3

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. http://httpd.apache.org/docs/2.2/mod/mod_dbd.html

Dharman
  • 30,962
  • 25
  • 85
  • 135
snoopaloop
  • 31
  • 1
  • 1
    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 Jan 08 '13 at 02:31
1

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

daghan
  • 948
  • 10
  • 18
0

Connection pooling is absolutely possible in PHP - however you should use swoole or openswoole extension that brings along a lot of async/parallelization features:
https://openswoole.com/docs/get-started/installation

After that you can use normal mysqli or pdo php extensions to set up connection pools.

See these examples:
https://github.com/open-smf/connection-pool
https://openswoole.com/docs/modules/connection-pool

Some frameworks already handle that for you:
https://hyperf.wiki/2.2/#/en/pool

edit: i use smf connection pool with mezzio micro-framework and openswoole and its rock solid for mysql and redis.

Dannyboy
  • 1,963
  • 3
  • 20
  • 37