mysql_connect timeout ???

admin

Administrator
Staff member
Hi there,

The problem :
When our mysql server is down, our PHP scripts needs to return cached files data.

The problem is that mysql_connect has no timeout parameters, then the scripts give back cached data after a long period of time.

How can we get this done quickly with a short timeout ?

I tried the code below but I noticed that if the mysql server is ON, the socket is not closed properly : the mysql variable "Aborted_connects" in the mysql server is incremented... (u can see this with "show status" in mysql client). It is not the case when you use mysql_close.

When "Aborted_connects" is very high very quickly, then it is not possible to connect...

Any idea on how closing a mysql connection properly with the socket mechanism ??

Thanks for your help.

David.

================
Below is the code which does not close the mysql connection properly with fclose($fp)...


Step 5: An Improved Database Connection Routine
<?php

/********************************************************
function db_connect_plus()

returns a link identifier on success, or false on error
********************************************************/
function db_connect_plus(){
$username = "username";
$password = "password";
$primary = "10.1.1.1";
$backup = "10.1.1.2";
$timeout = 15; // timeout in seconds

if($fp = fsockopen($primary, 3306, &$errno, &$errstr, $timeout)){
fclose($fp);
return $link = mysql_connect($primary, $username, $password);
}
if($fp = fsockopen($secondary, 3306, &$errno, &$errstr, $timeout)){
fclose($fp);
return $link = mysql_connect($secondary, $username, $password);
}

return 0;
}

?>
 
Back
Top