php mysql持久连接是怎样的_PHP持久连接是怎么一回事?

前言

PHP实现了持久连接(也叫长连接),能复用已有连接。听起来挺神的,但是用起来可能会有些误区,尤其是在不同的SAPI下,持久连接表现不一样。

这篇文章主要是从一个简单实验出发,以连接mysql为例,探讨PHP持久连接是怎么一回事。

实验材料

nginx+fastcgi+php(fpm)5.4+mysql

php-fpm.conf: pm.max_children = 4

yum install strace

实验脚本

短连接 connect.php

$user = 'root';

$password = '';

$link = mysql_connect('127.0.0.1', $user, $password);

var_dump($link);

长连接 pconnect.php

$user = 'root';

$password = '';

$link = mysql_pconnect('127.0.0.1', $user, $password);

var_dump($link);

步骤一:fastcgi下测试短连接脚本

在服务器开启strace

[root]#strace -f -e trace=network -e trace=connect $(pidof php-fpm | sed 's/\([0-9]*\)/\-p \1/g')

打开浏览器,F5多刷几次脚本:http://xxx.com/connect.php

留意服务器strace的输出结果

[root]# strace -f -e trace=network -e trace=connect $(pidof php-fpm | sed 's/\([0-9]*\)/\-p \1/g')

Process 3828 attached

Process 3827 attached

Process 3826 attached

Process 3825 attached

Process 3824 attached

[pid 3828] connect(5, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)

[pid 3826] connect(5, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)

[pid 3827] connect(5, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)

[pid 3825] connect(5, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)

[pid 3828] connect(5, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)

[pid 3826] connect(5, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)

[pid 3827] connect(5, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)

[pid 3825] connect(5, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)

[pid 3828] connect(5, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)

留意观察:3824是fpm主进程,3825、3826、3827和3828是子进程。

刚刚我刷新了9次,可见子进程每次都会connect一次mysql。

步骤二:fastcgi下测试持久连接脚本

重启fpm

在服务器敲好命令准备

strace -f -e trace=network -e trace=connect $(pidof php-fpm | sed 's/\([0-9]*\)/\-p \1/g')

打开浏览器,F5多刷几次脚本:http://xxx.com/connect.php

留意服务器strace的输出结果

[root]# strace -f -e trace=network -e trace=connect $(pidof php-fpm | sed 's/\([0-9]*\)/\-p \1/g')

Process 7557 attached

Process 7556 attached

Process 7555 attached

Process 7554 attached

Process 7553 attached

[pid 7555] connect(5, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)

[pid 7557] connect(5, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)

[pid 7554] connect(5, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)

[pid 7556] connect(5, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)

留意观察,7553是fpm主进程,54、55、56和57是子进程。

刚刚请求了N遍,当4个进程都连接了一次mysql后,同一子进程后续所有的请求都不需要再connectmysql了,这就是php-mysql持久连接的作用。

步骤三:cli下测试持久连接脚本

执行命令并留意结果

[root]# strace -f -e trace=network -e trace=connect php pconnect.php

Process 11515 attached

[pid 11515] +++ exited with 0 +++

connect(3, {sa_family=AF_LOCAL, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)

connect(3, {sa_family=AF_LOCAL, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)

connect(4, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)

resource(4) of type (mysql link persistent)

+++ exited with 0 +++

[root]# strace -f -e trace=network -e trace=connect php pconnect.php

Process 11526 attached

[pid 11526] +++ exited with 0 +++

connect(3, {sa_family=AF_LOCAL, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)

connect(3, {sa_family=AF_LOCAL, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)

connect(4, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)

resource(4) of type (mysql link persistent)

+++ exited with 0 +++

[root]# strace -f -e trace=network -e trace=connect php pconnect.php

Process 11596 attached

[pid 11596] +++ exited with 0 +++

connect(3, {sa_family=AF_LOCAL, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)

connect(3, {sa_family=AF_LOCAL, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)

connect(4, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)

resource(4) of type (mysql link persistent)

+++ exited with 0 +++

.....

无论执行多少遍,就算是mysql_pconnect,每次都是重新connectmysql,原因是什么?因为每次执行新起了一个进程

结论

简单来说就是一句话:php-mysql的持久连接只在同一个进程里可“复用”,不同进程之间玩不了。

步骤二,fpm的子进程可以常驻,所以只要子进程还在,那么上一次请求创建的持久连接,下一次请求还可以复用。

步骤三,cli每次都新起一个进程,没法把连接复用,那么短连接和持久连接其实是一样的。

附.PHP底层原理

如果没有手动close,短连接资源变量会在脚本执行完毕回收,但持久连接资源在脚本执行完并不回收,只有在进程结束,mshutdown统一回收。

persistent_list里面的资源不会在脚本结束时回收。当后续请求来了,先检查persistent_list,若有可用连接,直接用,实现所谓的“持久连接”。

persistent_list里面的东西大概这样子,包含key和zend_rsrc_list_entry结构体:

326d08f9fa5a?tdsourcetag=s_pctim_aiomsg

所以PHP的持久连接其实就是变量的玩法,不算连接池。

回到上面说的,持久连接只能在同一个进程里复用也是这个原因。

思考题

想一下,persistent_list里的key如何产生?

php在连接之前要先去检查有没有可用的资源,拿什么去检查?

翻一下文档或看源码就知道了:

https://github.com/php/php-src/blob/PHP-5.4.41/ext/mysql/php_mysql.c#L813

hashed_details_length = spprintf(&hashed_details, 0, "mysql_%s_%s_%s_%ld", SAFE_STRING(host_and_port), SAFE_STRING(user), SAFE_STRING(passwd), client_flags);

如果这次连接的host、port、user、`passwd和flag``跟已保存的连接资源一致,那么就可以复用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值