PHP MySQL连接持久性 [英] PHP MySQL connection persistence

查看:189
本文介绍了PHP MySQL连接持久性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了大量有关PHP和MySQL之间的持久数据库连接(mysql_connect与mysql_pconnect)的信息.与PDO和MySQLi相同.绝对是我对此没有足够的了解,但是如何在网页之间保持数据库连接的持久性呢?在此代码中:

$conn = mysql_pconnect( $server , $user, $pass );
mysql_select_db( $dbname );

如果两个用户同时使用两个不同的$ dbname变量加载该页面,PHP将仅建立一个到数据库的连接还是两个?我相当确定

$conn = mysql_connect( $server , $user, $pass );

将建立两个连接.

如果pconnect重用了第一个用户打开的连接,那么mysql_select_db调用对第二个用户有效吗?

理想情况下,我正在寻找一种减少数据库连接但仍能够在每个PHP脚本中设置默认数据库的方法.我有所有使用相同PHP脚本的客户端,但是数据存储在自己的客户端数据库中(因此,$ dbname始终不同,但是MySQL连接参数相同-相同的mysql IP地址,用户和密码). /p>

希望如此.我们可以使用MySQL,MySQLi或PDO,只需要知道如何以最佳方式完成此操作,而不必让客户端将数据意外写入其他人的数据库中!预先感谢.

解决方案

从我阅读的文档和评论中,我看到:

有关mysql_pconnect(不建议使用的方法)的文档

第二,在脚本执行结束时,不会关闭与SQL Server的连接.相反,该链接将保持打开状态以供将来使用(mysql_close()不会关闭mysql_pconnect()建立的链接).

以及对该页面的评论

持久连接对于由fastCGI管理的CGI PHP很好用,这与上面的建议仅适用于模块版本相反.那是因为fastCGI使PHP进程在请求之间保持运行.由于可以将PHP_FCGI_CHILDREN设置为<< ;,因此可以很容易地使此模式下的持久连接不受连接限制的影响. mysql的max_connections<<< Apache的MaxClients.这样也可以节省资源.

有关mysqli_connect(新方法)的文档

通过p预先设置主机:打开一个持久连接.在从连接池打开的连接上会自动调用mysqli_change_user().

mysqli_change_user的文档:

更改指定数据库连接的用户并设置当前数据库.

因此,我的理解如下:pconnect在脚本结束后但在进程(或进程组)仍处于活动状态(例如在已设置FCGI的服务器中)的情况下使连接保持打开状态.一次仅一个脚本使用一个连接,当新脚本抓住该连接时,用户和数据库将更新.

因此,如果使用FCGI和持久连接,则可以减少打开的数据库连接的数量,但是同时运行的脚本将不会共享同一连接.对于选择哪个数据库,连接不会出现问题.

I've read a ton about persistent database connections between PHP and MySQL (mysql_connect vs. mysql_pconnect). Same with PDO and MySQLi. It's definitely just my lack of understanding on this one, but how can a database connection be persistent between webpages? In this code:

$conn = mysql_pconnect( $server , $user, $pass );
mysql_select_db( $dbname );

If two users load this page at the same time, with two different $dbname variables, will PHP only make one connection to the database or two? I am fairly certain that

$conn = mysql_connect( $server , $user, $pass );

would make two connections.

If pconnect reuses the connection opened by the first user, will the mysql_select_db call work for the second user?

Ideally, what I am looking for is a way to have fewer database connections but still be able to set the default database in each PHP script. I have clients who all use the same PHP scripts, but the data is stored in their own client database (hence, $dbname is always different, but the MySQL connection parameters are the same - same mysql ip address, user and password).

Hope that makes sense. We can use MySQL, MySQLi or PDO, just need to know how to accomplish this the best way without having the possibility for clients to accidently write data to someone else's database! Thanks in advance.

解决方案

From my reading of documentation and comments, I see:

Docs on mysql_pconnect (deprecated method)

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

and a comment on that page

Persistent connections work well for CGI PHP managed by fastCGI, contrary to the suggestion above that they only work for the module version. That's because fastCGI keeps PHP processes running between requests. Persistent connections in this mode are easily made immune to connection limits too, because you can set PHP_FCGI_CHILDREN << mysql's max_connections <<< Apache's MaxClients. This also saves resources.

Docs on mysqli_connect (new method)

Prepending host by p: opens a persistent connection. mysqli_change_user() is automatically called on connections opened from the connection pool.

Docs for mysqli_change_user:

Changes the user of the specified database connection and sets the current database.

So my understanding is as follows: pconnect keeps the connection open after a script ends but while a process (or maybe group of processes) is still alive (like in a server with FCGI set up). Only one script at a time uses a connection, and when a new script grabs that connection the user and database are updated.

Thus if you use FCGI and persistent connections you can reduce the number of db connections open, but scripts running simultaneously will not be sharing the same connection. There is no problem with the connection being confused as to which database is selected.

这篇关于PHP MySQL连接持久性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆