首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >php mysql断开连接真的断开连接吗?

问php mysql断开连接真的断开连接吗?
EN

Stack Overflow用户
提问于 2012-10-11 15:33:18
回答 2查看 545关注 0票数 0

在我的24/7 PHP shell脚本中,我遇到了MySQL超时的问题。

我的印象是Pear DB DB::connect()会创建新的句柄,但事实似乎并非如此。DB::connect()即使在循环中使用了五次,实际上也会返回“服务器已离开”错误。

现在,我将db->disconnect()和unset( $db)添加到脚本中的DB::connect()之前。这是否会导致底层mysql模块在连接之前释放资源(localhost)?如果不是,那么除了使用专有的new_link之外,我看不到其他的选择。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-11 18:47:59

嗯。没有定论。

我做了一个示例php脚本,用相同的设置连接两次,运行mysql_close(),然后第三次连接。我还在另一个窗口中运行了tcpdump。

在最初的几次运行中,在随后的连接中有一些到服务器的流量。然后这个流量就消失了。

现在的结果是,如果没有mysql_close(),mysql_connect()确实使用了前一个连接,并且在后续的mysql_connect()上没有流量。

在运行mysql_close()时,转储将指示TCP连接已关闭。

在第三次连接时,将再次从头开始建立连接。

代码语言:javascript
复制
13:37:57.842598 IP 192.168.1.1.58772 > 192.168.2.2.mysql: S 3281731243:3281731243(0) win 5840 <mss 1460,sackOK,timestamp 503312235 0,nop,wscale 7>
13:37:57.887534 IP 192.168.2.2.mysql > 192.168.1.1.58772: S 1293434507:1293434507(0) ack 3281731244 win 5792 <mss 1460,sackOK,timestamp 3402066848 503312235,nop,wscale 7>
13:37:57.887639 IP 192.168.1.1.58772 > 192.168.2.2.mysql: . ack 1 win 46 <nop,nop,timestamp 503312245 3402066848>
13:37:57.932750 IP 192.168.2.2.mysql > 192.168.1.1.58772: P 1:61(60) ack 1 win 46 <nop,nop,timestamp 3402066859 503312245>
13:37:57.932843 IP 192.168.1.1.58772 > 192.168.2.2.mysql: . ack 61 win 46 <nop,nop,timestamp 503312256 3402066859>
13:37:57.935983 IP 192.168.1.1.58772 > 192.168.2.2.mysql: P 1:62(61) ack 61 win 46 <nop,nop,timestamp 503312258 3402066859>
13:37:57.981130 IP 192.168.2.2.mysql > 192.168.1.1.58772: . ack 62 win 46 <nop,nop,timestamp 3402066871 503312258>
13:37:57.981181 IP 192.168.2.2.mysql > 192.168.1.1.58772: P 61:72(11) ack 62 win 46 <nop,nop,timestamp 3402066871 503312258>
13:37:58.017172 IP 192.168.1.1.58772 > 192.168.2.2.mysql: . ack 72 win 46 <nop,nop,timestamp 503312279 3402066871>

at 13:38:01 there was the second mysql connection

at 13:38:04 now the mysql_close():    
13:38:03.989796 IP 192.168.1.1.58772 > 192.168.2.2.mysql: P 62:67(5) ack 72 win 46 <nop,nop,timestamp 503313772 3402066871>
13:38:03.989844 IP 192.168.1.1.58772 > 192.168.2.2.mysql: F 67:67(0) ack 72 win 46 <nop,nop,timestamp 503313772 3402066871>
13:38:04.034818 IP 192.168.2.2.mysql > 192.168.1.1.58772: F 72:72(0) ack 68 win 46 <nop,nop,timestamp 3402068385 503313772>
13:38:04.034920 IP 192.168.1.1.58772 > 192.168.2.2.mysql: . ack 73 win 46 <nop,nop,timestamp 503313783 3402068385>

at 13:38:07 the third connection after mysql_close(). Afterwards script exits and mysql connection is closed.
13:38:06.995120 IP 192.168.1.1.58773 > 192.168.2.2.mysql: S 3287179963:3287179963(0) win 5840 <mss 1460,sackOK,timestamp 503314523 0,nop,wscale 7>
13:38:07.035065 IP 192.168.2.2.mysql > 192.168.1.1.58773: S 1439831970:1439831970(0) ack 3287179964 win 5792 <mss 1460,sackOK,timestamp 3402069136 503314523,nop,wscale 7>
13:38:07.035166 IP 192.168.1.1.58773 > 192.168.2.2.mysql: . ack 1 win 46 <nop,nop,timestamp 503314533 3402069136>
13:38:07.075107 IP 192.168.2.2.mysql > 192.168.1.1.58773: P 1:61(60) ack 1 win 46 <nop,nop,timestamp 3402069146 503314533>
13:38:07.075188 IP 192.168.1.1.58773 > 192.168.2.2.mysql: . ack 61 win 46 <nop,nop,timestamp 503314543 3402069146>
13:38:07.075437 IP 192.168.1.1.58773 > 192.168.2.2.mysql: P 1:62(61) ack 61 win 46 <nop,nop,timestamp 503314543 3402069146>
13:38:07.115123 IP 192.168.2.2.mysql > 192.168.1.1.58773: . ack 62 win 46 <nop,nop,timestamp 3402069156 503314543>
13:38:07.115178 IP 192.168.2.2.mysql > 192.168.1.1.58773: P 61:72(11) ack 62 win 46 <nop,nop,timestamp 3402069156 503314543>
13:38:07.115821 IP 192.168.1.1.58773 > 192.168.2.2.mysql: P 62:67(5) ack 72 win 46 <nop,nop,timestamp 503314553 3402069156>
13:38:07.115844 IP 192.168.1.1.58773 > 192.168.2.2.mysql: F 67:67(0) ack 72 win 46 <nop,nop,timestamp 503314553 3402069156>
13:38:07.155524 IP 192.168.2.2.mysql > 192.168.1.1.58773: F 72:72(0) ack 68 win 46 <nop,nop,timestamp 3402069166 503314553>
13:38:07.155606 IP 192.168.1.1.58773 > 192.168.2.2.mysql: . ack 73 win 46 <nop,nop,timestamp 503314563 3402069166>

和脚本:

代码语言:javascript
复制
<?php

output( "starting");
$start = microtime(true);
$res = mysql_connect('192.168.2.2','user','pass');
if( $res === FALSE )
        die(output("SQL error on connect\n"));
output(sprintf("Connect took: %.3f", (microtime(true)-$start)));

output("sleep 3");
sleep(3);

$start = microtime(true);
$res = mysql_connect('192.168.2.2','user','pass');
if( $res === FALSE )
        die(output("SQL error on connect\n"));
output(sprintf("Connect took: %.3f", (microtime(true)-$start)));

output("sleep 3");
sleep(3);

output("closing");
mysql_close( $res );

output("sleep 3");
sleep(3);

$start = microtime(true);
$res = mysql_connect('192.168.2.2','user','pass');
if( $res === FALSE )
        die(output("SQL error on connect\n"));
output(sprintf("Connect took: %.3f", (microtime(true)-$start)));

output("all done");
exit;

function output($str)
{
        echo date('Y-m-d H:i:s')." ".$str."\n";
}
票数 0
EN

Stack Overflow用户

发布于 2012-10-11 15:40:22

从manual

许多web应用程序将从与数据库服务器的持久连接中受益。持久连接不会在脚本结束时关闭,而是在另一个脚本使用相同的凭据请求连接时缓存并重新使用。持久连接缓存允许您避免每次脚本需要与数据库对话时建立新连接的开销,从而实现更快的web应用程序。

这被称为连接池。连接是使用连接池进行的。即使您创建了一个新连接,它也会从池中取出并提供给您。

Do not connections,因为这会降低性能,并且不会给你带来任何好处。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12834431

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档