흠. 결론적이지 않다.
샘플 PHP 스크립트를 동일한 설정으로 두 번 연결하여 mysql_close()를 실행 한 다음 세 번째 연결을 실행하십시오. 다른 창에서 tcpdump도 실행했습니다.
처음 몇 번의 실행에서 후속 연결시 서버에 약간의 트래픽이 발생했습니다. 그런 다음이 교통 체증이 사라졌습니다.
결과적으로 mysql_connect()가 없으면 mysql_connect()는 이전 연결을 실제로 사용하며 이후의 mysql_connect()에서는 트래픽이 제로입니다.
mysql_close()를 실행할 때 덤프는 TCP 연결이 종료되었음을 나타냅니다.
세 번째 연결에서 연결이 처음부터 다시 설정됩니다.
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>
그리고 스크립트 :
<?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";
}
왜 당신이 데이터베이스에 다섯 번 연결? 다섯 가지 데이터베이스가 있습니까? (왜 루프 전에 한 번 연결을 연 다음 끝날 때까지 연결을 사용하지 않는 것이 좋을까요?) –
@TerrySeidler 포기할 때까지 최대 5 회 루프를 연결하려고합니다. 연결은 항상 "사라짐"오류를 반환합니다. – anttir
우 ... 알 겠어. 지금 더 의미가 있습니다 :} –