连接池与持久连接mysqli [英] Connection pooling vs persist connection mysqli

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

问题描述

我读到mysqli引入了mysql中不可用的连接池. http://php.net/manual/en/mysqli.quickstart.connections. php

我已阅读此问题,它解释了连接池之间的区别并保持连接.

Persistent connection 的mysqli文档中,他们已经写了

如果给定主机组合未使用的持久连接, 找不到用户名,密码,套接字,端口和默认数据库 在连接池中,然后mysqli打开一个新连接.

那么,mysqli是否将连接池用于持久连接?<​​/p>

如果是,那么mysqli中的连接池和持久连接之间有什么区别?

如果否,那么mysqli如何为持久连接的连接对象执行查找?

解决方案

实际上是 连接池 持久连接 在PHP中使用 mysqli 时指的是同一件事.

持久连接 在这种情况下是指从PHP脚本打开的MySQL连接,该连接在脚本执行完成后保持打开状态,以后将在以后的执行中再次使用. /p>

连接池 表示有一个由PHP维护的持久连接池.来自该缓冲池的一个空闲连接已分配给PHP脚本,该脚本希望连接到MySQL,并在脚本完成后返回缓冲池.

您可能想知道为什么我们完全需要MySQL连接池,为什么我们不对所有脚本仅使用一个持久连接?<​​/em>

有两个原因:

  • PHP根据使用的host/port/username/password创建一个MySQL连接池.如果一个脚本想要使用某些host/port/username/password组合连接到MySQL,PHP将搜索具有相同值的空闲持久连接.如果找不到,则使用此host/port/username/password组合创建一个新的持久连接.因此,我们至少需要与所有脚本使用的不同host/port/username/password值一样多的不同持久连接.
  • 不能同时在一个MySQL连接上执行两个SQL命令.当两个PHP脚本同时执行时,可能会发生这种情况.当两个脚本希望同时与MySQL通信时,将创建两个持久的MySQL连接.池中的持久连接数等于已执行的最大并行PHP脚本的最后数目,或等于php.ini中设置的上限.

重要通知:

MySQL连接池(和任何其他连接池)仅在PHP作为Web服务器插件执行时可以存在.当池在快速cgi模式下运行时,或者在脚本执行后PHP可执行文件终止时,池不起作用.

如果将Web服务器配置为对多个请求重用一个PHP快速cgi进程,则可以在PHP的快速cgi模式下使用MySQL连接池.如果将PHP fast-cgi进程配置为在处理一个请求后退出,那么它将关闭所有MySQL连接.

I've read that mysqli introduced connection pooling that was not available in mysql. http://php.net/manual/en/mysqli.quickstart.connections.php

I've read this question that explains the difference between connection pooling and persist connections.

In the mysqli documentation for Persistent connection they have written that:

If a unused persistent connection for a given combination of host, username, password, socket, port and default database can not be found in the connection pool, then mysqli opens a new connection.

So, does mysqli use connection pooling for persistent connections?

If yes, then what is difference between connection pooling and persistent connections in mysqli?

If no, then how does mysqli perform a lookup for a connection object for a persistent connection?

解决方案

Actually terms connection pooling and persistent connection refer to the same thing in case of mysqli in PHP.

Persistent connection in this case refers to MySQL connection open from PHP script which stays open after the script has finished executing, to be used again in some later executions.

Connection pooling means that there is a pool of persistent connections maintained by PHP. One idle connection from this pool is given to PHP script which wants to connect to MySQL and returned to pool when script finishes.

You might wonder why do we need the pool of MySQL connections at all, why don't we use just one persistent connection for all of the scripts?

There are two reasons for this:

  • PHP creates a pool of MySQL connections based on host/port/username/password used. If one script wants to connect to MySQL with some host/port/username/password combination, PHP searches for idle persistent connection which has the same values. If it's not found, then a new persistent connection is created with this host/port/username/password combination. So we need at least as many different persistent connection as there are different host/port/username/password values used by all of the scripts.
  • You cannot execute two SQL commands on one MySQL connection at the same time. This can happen when two PHP scripts are executing simultaneously. When two scripts want to communicate with MySQL at the same time, two persistent MySQL connections are created. Number of persistent connections in pool is equal to last number of maximum parallel PHP scripts executed, or equal to upper limit set in php.ini.

Important notice:

MySQL connection pools (and any other connection pools) can exist only if PHP is executing as a web server plugin. Pools do not work when it is working in fast-cgi mode or in any other way when PHP executable terminates after script execution.

Edit: MySQL connection pooling can be used in fast-cgi mode of PHP if web server is configured to reuse one PHP fast-cgi process for multiple requests. If PHP fast-cgi process is configured to exit after serving one request then all of it's MySQL connections are closed.

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

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