php connection pooling mysql

2019-01-11 02:03发布

问题:

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?

回答1:

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



回答2:

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.



回答3:

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



回答4:

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



回答5:

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



回答6:

Not exactly an answer but i think you might also consider PHP Data Objects (PDO) http://php.net/manual/en/book.pdo.php And PDO for MySQL http://php.net/manual/en/ref.pdo-mysql.php