|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-10-23 08:10 UTC] ed at bellcpa dot com
Description:
------------
I have a script that attempts to contact/query an MySQL server on a LAN. If the server is available, the script runs successfully. If the MySQL server is offline, however, the script hangs along with the browser session indefinately. This is despite MYSQLI_OPT_CONNECT_TIMEOUT being set to 5 seconds.
Specifically, the script below hangs when "$mysqli->real_connect" is executed
Looking for a way to timeout connection attempt.
Running VC9 x86 Thread Safe 5.3. Default mysqli/mysqlnd php.ini settings.
The code below worked fine in the past in the same scenario on version 5.2.8.
Reproduce code:
---------------
$mysqli = mysqli_init();
if (!$mysqli) exit(1);
if (!$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5)) {
exit(1);
}
if (!$mysqli->real_connect('localhost', 'my_user', 'my_password', 'my_db')) {
exit(1);
}
Expected result:
----------------
Quit in around 5 sec with an code 1 if MySQL is offline.
Actual result:
--------------
Script hangs indefinately and so does a browser session through which the script is invoked. After some time (e.g. 2-5 min) the following is logged in php_errors.log:
[23-Oct-2009 00:48:26] PHP Warning: mysqli::real_connect(): [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://192.168.201.222:3306) in C:\app\php\script.php on line 30
[23-Oct-2009 00:48:26] PHP Warning: mysqli::real_connect(): (HY000/2002): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Nov 04 20:00:01 2025 UTC |
Oh but I did search and have seen #49511 propr to submitting.... #49511 mainly focuses on a query timeout and not a connection timeout, which is my issue. MYSQL_OPT_CONNECT_TIMEOUT mentioned is unaddressed. Also tried ini_set('default_socket_timeout', 5) as suggested in that submission with no effect.Hi Ed, as you may know from the Wireshark protocol you provided (mysql-noserver.pcap - MySQL is offline) there is a TCP SYN->RST,ACK ping-pong between the client machine and the server. PHP tries to establish a connection and sends SYN. The server's response is RST (, ACK) indicating that no process on the server is wating for requests on the requested port (MySQL is offline). At this point PHP should get ECONNREFUSED from connect() and bubble up the error. But it seems as if this is not the case. According to the Wireshark protocol the first SYN->RST,ACK is followed by another connection attempt after about 0.5s which fails. PHP continues to make another connection attempt after 1s which fails again. The ping-pong should start because PHP recognizes after 0.5s respectively 1s that there is time left for more connection attempts (you set the connect timeout to 5s). I don't know why the protocol shows no more records after 1s. However, can you try using fsockopen() to connect to the MySQL host if MySQL is offline? Something like this: sapi/cli/php -r '$start = microtime(true); $errno = 0; $error = ""; $fp = fsockopen("tcp://192.168.201.222", 3306, $errno, $error, 5); var_dump($fp); var_dump($errno); var_dump($error); var_dump(microtime(true) - $start); ' Again, I'd be interested in the Wireshark protocol (unless it is blocking endless). Thanks! UlfUlf, Here is the fsockopen() result, looks like timeout works as expected: C:\temp2>php testme.php PHP Warning: fsockopen(): unable to connect to tcp://192.168.201.222:3306 (A co nnection attempt failed because the connected party did not properly respond aft er a period of time, or established connection failed because connected host has failed to respond. ) in C:\temp2\testme.php on line 6 PHP Stack trace: PHP 1. {main}() C:\temp2\testme.php:0 PHP 2. fsockopen() C:\temp2\testme.php:6 Warning: fsockopen(): unable to connect to tcp://192.168.201.222:3306 (A connect ion attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has fail ed to respond. ) in C:\temp2\testme.php on line 6 Call Stack: 0.0312 324832 1. {main}() C:\temp2\testme.php:0 0.0491 325280 2. fsockopen() C:\temp2\testme.php:6 bool(false) int(10060) string(185) "A connection attempt failed because the connected party did not pro perly respond after a period of time, or established connection failed because c onnected host has failed to respond. " float(5.1100761890411)