简体   繁体   English

未使用 PHP 到 mysql 的持久连接

[英]PHP to mysql persistent connections not being used

I have two instances of a apache server running PHP connecting to a local mysql server.我有两个运行 PHP 的 apache 服务器实例,它们连接到本地 mysql 服务器。

Due to the usage of many thousands requests for the same PHP page (with different arguments) coming in over a couple of minutes which complete within a second or so I'd like to make use of persistent mysql connections as I find I'm running out of sockets due to the OS not recycling them quickly enough.由于对同一个 PHP 页面(具有不同参数)的数千个请求的使用超过几分钟,这些请求在一秒左右完成,我想在我发现我正在运行时使用持久的 mysql 连接由于操作系统没有足够快地回收它们而导致套接字溢出。

I have this working on one instance with this config:我用这个配置在一个实例上工作:

$link = mysqli_connect("p:$db_server_ip",$dbuser,$dbpassword,"db","8080");
if (!$link) {
        error_log("Failed to connected ".mysqli_connect_error());
        die('Could not connect: ' . mysqli_connect_error());
}

And when I look at the number of mysql connections when I have lots of http requests coming in I only see a couple of hundred connections which is good on the first server.当我在收到大量 http 请求时查看 mysql 连接数时,我只看到几百个连接,这在第一台服务器上是好的。

On my second sever which I believe is the same OS, apache, php and mysql config and versions and php code I do not see connections being used and I'm hitting the max mysql connection limit and if I look at processlist in mysql they are all just sleeping.在我认为是相同的操作系统、apache、php 和 mysql 配置和版本以及 php 代码的我的第二台服务器上,我没有看到正在使用的连接,并且我达到了最大 mysql 连接限制,如果我查看 mysql 中的进程列表,它们是都只是睡觉。

Can someone explain why I'm seeing a difference in behavior, what other vairables are in play as to whether a connection is reused.有人可以解释为什么我看到行为上的差异,还有哪些其他变量在影响连接是否被重用。

PHP Version 5.3.3 Apache/2.2.15 (Red Hat) PHP 版本 5.3.3 Apache/2.2.15(红帽)

Persistent connections are a bad idea for your application.持久连接对您的应用程序来说是个坏主意。 In fact, it will probably max out your MySQL server faster than the current way you're doing it now.事实上,它可能会比您现在使用的当前方式更快地最大化您的 MySQL 服务器。 What a persistent connection does is keep the connection open after the PHP thread has terminated.持久连接的作用是在 PHP 线程终止后保持连接打开。 This means you have a connection constantly open connection that another PHP thread may or may not connect to.这意味着您有一个不断打开的连接,另一个 PHP 线程可能会也可能不会连接到该连接。 As such, you'll reach your max connections much faster.因此,您将更快地达到最大连接数。

The best solution here is to increase the max connections and let PHP terminate the connections when the PHP threads close.这里最好的解决方案是增加最大连接数,并让 PHP 在 PHP 线程关闭时终止连接。

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

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