php php连接池mysql
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/830707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php connection pooling mysql
提问by coool
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...
我打算使用 MYSQL。是否有可用的连接池扩展?或者连接的正常做法是什么?这是每个地方都用的那个吗...
mysqli_connect("localhost", "xxx", "xxx", "test");
Do people use just normal mysql_connector pconnect..? How better is pconnectand what setting should I do for PConnect?
人们使用普通mysql_connect还是pconnect..?pconnectPConnect有多好?我应该如何设置?
回答by Sadegh
have you ever used mysql_pconnect()?
mysql_pconnect()acts very much like mysql_connect()with two major differences.
你用过mysql_pconnect()吗?
mysql_pconnect()行为非常相似,但mysql_connect()有两个主要区别。
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()).
其次,脚本执行结束时不会关闭与 SQL 服务器的连接。相反,该链接将保持打开状态以备将来使用(mysql_close()不会关闭由 建立的链接mysql_pconnect())。
This type of link is therefore called 'persistent'
因此,这种类型的链接称为“持久”
Check it here
在这里检查
回答by Benedict
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.
PHP 5.3 中为 mysqli 扩展引入了持久连接支持。PDO MYSQL 和 ext/mysql 中已经提供了支持。持久连接背后的想法是客户端进程和数据库之间的连接可以被客户端进程重用,而不是被多次创建和销毁。这减少了每次需要时创建新连接的开销,因为未使用的连接被缓存并准备重用。
与 mysql 扩展不同,mysqli 不提供单独的打开持久连接的功能。要打开持久连接,您必须在连接时将 p: 添加到主机名。
source: http://www.php.net/manual/en/mysqli.persistconns.php
来源: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.
编辑:对不起,没有看到其他答案。
回答by Tyrael
use the mysqli or the PDO extension instead of the old mysql extension.
使用 mysqli 或 PDO 扩展而不是旧的 mysql 扩展。
you can tell the mysqli_connector mysqli::__constructto use persistent connection, if you prefix your hostname with 'p:'
你可以告诉mysqli_connectormysqli::__construct使用持久连接,如果你用“p:”前缀你的主机名
回答by snoopaloop
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.
这是一个老问题,但我想加上我的两分钱,因为我正在研究同样的问题。从 PHP 5.3 开始,mysqli 支持持久连接,您只需要在主机名前面添加 p: 即可。
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
如果您正在运行 Apache,您是否尝试通过 Apache mod_dbd 模块研究与 mysql 的连接池?它支持 MySQL 的连接池。http://httpd.apache.org/docs/2.2/mod/mod_dbd.html
回答by daghan
There are 3 connection functions:
有3个连接功能:
mysql_connect: normal connection, no pooling, you cannot execute stored procedures (just sql)
mysql_connect: 正常连接,没有池化,不能执行存储过程(只是sql)
mysql_pconnect: pooled connection, you cannot execute stored procedures (just sql)
mysql_pconnect: 池化连接,你不能执行存储过程(只是 sql)
mysqli_connect: normal connection, no pooling, you can execute stored procedures (needs mysql 5 or higher)
mysqli_connect: 正常连接,无池化,可以执行存储过程(需要mysql 5以上)
mysqli_pconnect: DOES NOT EXIST. There is no built in connection function both handling stored procedures and pooling
mysqli_pconnect:不存在。没有处理存储过程和池的内置连接函数
My advice (via experience and surfing):
我的建议(通过经验和冲浪):
If you need stored procedures, omit pooling and use mysqli_connect
如果您需要存储过程,请省略池并使用 mysqli_connect
If you do not need stored procedures, use mysql_pconnect
如果不需要存储过程,请使用 mysql_pconnect
回答by aviv
Not exactly an answer but i think you might also consider PHP Data Objects (PDO) http://php.net/manual/en/book.pdo.phpAnd PDO for MySQL http://php.net/manual/en/ref.pdo-mysql.php
不完全是一个答案,但我认为你也可以考虑 PHP 数据对象 (PDO) http://php.net/manual/en/book.pdo.php和 MySQL 的 PDO http://php.net/manual/en/ ref.pdo-mysql.php

