简体   繁体   English

连接池与持久连接mysqli

[英]Connection pooling vs persist connection mysqli

I've read that mysqli introduced connection pooling that was not available in mysql. 我读过mysqli介绍了mysql中没有的连接池。 http://php.net/manual/en/mysqli.quickstart.connections.php 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: Persistent connectionmysqli文档中,他们写道:

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. 如果在连接池中找不到主机,用户名,密码,套接字,端口和默认数据库的给定组合的未使用的持久连接,则mysqli将打开一个新连接。

So, does mysqli use connection pooling for persistent connections? 那么,mysqli是否使用连接池来实现持久连接?

If yes, then what is difference between connection pooling and persistent connections in mysqli? 如果是,那么mysqli中连接池和持久连接之间有什么区别?

If no, then how does mysqli perform a lookup for a connection object for a persistent connection? 如果不是,那么mysqli如何为持久连接执行连接对象的查找?

Actually terms connection pooling and persistent connection refer to the same thing in case of mysqli in PHP. 实际上,术语连接池持久连接在PHP中的mysqli的情况下引用相同的东西。

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. 在这种情况下, 持久连接是指从PHP脚本打开的MySQL连接,该脚本在脚本执行完毕后保持打开状态,以便在稍后的某些执行中再次使用。

Connection pooling means that there is a pool of persistent connections maintained by PHP. 连接池意味着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. 来自此池的一个空闲连接被提供给PHP脚本,该脚本想要连接到MySQL并在脚本完成时返回到池。

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? 您可能想知道为什么我们需要MySQL连接池,为什么我们不为所有脚本只使用一个持久连接?

There are two reasons for this: 有两个原因:

  • PHP creates a pool of MySQL connections based on host/port/username/password used. PHP根据使用的host/port/username/password创建一个MySQL连接池。 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. 如果一个脚本想要使用某些host/port/username/password组合连接到MySQL,PHP将搜索具有相同值的空闲持久连接。 If it's not found, then a new persistent connection is created with this host/port/username/password combination. 如果找不到,则使用此host/port/username/password组合创建新的持久连接。 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. 因此,我们需要至少与所有脚本使用的不同host/port/username/password值一样多的不同持久连接。
  • You cannot execute two SQL commands on one MySQL connection at the same time. 您不能同时在一个MySQL连接上执行两个SQL命令。 This can happen when two PHP scripts are executing simultaneously. 当两个PHP脚本同时执行时,可能会发生这种情况。 When two scripts want to communicate with MySQL at the same time, two persistent MySQL connections are created. 当两个脚本想要同时与MySQL通信时,会创建两个持久的MySQL连接。 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 . 池中的持久连接数等于执行的最大并行PHP脚本的最后数,或等于php.ini设置的上限。

Important notice: 重要的提醒:

MySQL connection pools (and any other connection pools) can exist only if PHP is executing as a web server plugin . 仅当PHP作为Web服务器插件执行时, MySQL连接池(以及任何其他连接池) 才能存在 Pools do not work when it is working in fast-cgi mode or in any other way when PHP executable terminates after script execution. 池在以快速cgi模式工作时不起作用,或者在脚本执行后PHP可执行文件终止时以任何其他方式工作。

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. 编辑:如果将Web服务器配置为针对多个请求重用一个PHP fast-cgi进程,则可以在PHP的fast-cgi模式中使用MySQL连接池。 If PHP fast-cgi process is configured to exit after serving one request then all of it's MySQL connections are closed. 如果PHP fast-cgi进程配置为在提供一个请求后退出,那么它的所有MySQL连接都将被关闭。

in libmysql18 , it looks for any existing default mysql sock/port and tests it with the credentials. libmysql18 ,它查找任何现有的默认mysql sock/port并使用凭据对其进行测试。 if it can't be found, the c++ function clone() is called, creating the new connection object(Resource). 如果找不到,则调用c ++函数clone() ,创建新的连接对象(Resource)。 in certain cases this can result in a stack overflow. 在某些情况下,这可能导致堆栈溢出。 i'm not familiar with how it works in mysqlnd (native driver). 我不熟悉它在mysqlnd (本机驱动程序)中的工作原理。 maybe someone else is. 也许别人是。 if you want more information about how it works, install a version of facebooks HHVM-debug package and attempt to pass a connection via global in multiple nested functions. 如果您想了解有关其工作原理的更多信息,请安装一个版本的facebooks HHVM-debug软件包,并尝试通过多个嵌套函数中的全局传递连接。 this can trigger a stack overflow under HHVM, which will show you every function called prior to the exception up to that point. 这可以在HHVM下触发堆栈溢出,这将显示在该异常之前调用的每个函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM