php connection pooling mysql

后端 未结 6 1108
眼角桃花
眼角桃花 2020-12-02 23:55

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

相关标签:
6条回答
  • 2020-12-03 00:11

    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

    0 讨论(0)
  • 2020-12-03 00:12

    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

    0 讨论(0)
  • 2020-12-03 00:21

    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

    0 讨论(0)
  • 2020-12-03 00:27

    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

    0 讨论(0)
  • 2020-12-03 00:30

    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

    0 讨论(0)
  • 2020-12-03 00:31

    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.

    0 讨论(0)
提交回复
热议问题