Jump to content

Recommended Posts

Hi

 

If my MySQL server goes down for some reason, and then a user trys to access my site while its down, id like PHP to fire off an email to me going "Ahhh, your server is down!!!". Below is the code I use to connect to MySQL (It uses PDO, ive only copied the relevant lines of the class).

 

try
{
$this->PDO = new PDO($this->PDOConnectionURI, $this->DatabaseUser(), $this->DatabasePassword());
return TRUE;
}
catch(Exception $e)
{
$this->Error = "PDOConnection Class - Connect() - " . $e->getMessage();
return FALSE;
}

 

The above code connects fine when MySQL is running.

 

However when I shutdown my  MySQL server to simulate an outage, then try to connect, PHP hangs for 30 seconds before reporting

 

A - Warning - [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://127.0.0.1:3306:3306)

B - Fatal - an exceeded 30 second maximum execution time.

 

I'd prefer PHP to try and connect and if it cant get a connection after 5 seconds, report a MySQL Error to the user (Some kind of splash page) and email me.

 

I guess my question is -

 

Is PDO capable of detecting wether MySQL is running or not? Failing that is there some kind of PDO timeout setting I could configure in PDO that im not aware of?

 

It really bugs me that it hangs so long waiting for a connection that PHP isnt going to find.

Link to comment
https://forums.phpfreaks.com/topic/208681-pdo-problem/
Share on other sites

You could try using the timeout flag with PDO::setAttribute.

 

Yeah I tried but it made no difference, just sits there trying to connect to the stopped server. I extended PHPs maximum execution time to 45 seconds and it still reported the same error (I was hoping that PDO's default timeout was equal or bigger than PHP's execution time).

 

I just tried connecting to a Stopped MySQL server using mysql_connect() and even that will eventually report that it can't connect and process my site the way I want it to. Ive spent the past week at work convincing myself that PDO will be better than using mysql/mysqli but im having a hard time believing myself tbh.

Link to comment
https://forums.phpfreaks.com/topic/208681-pdo-problem/#findComment-1090227
Share on other sites

tbh the only answer I can see is by checking the connection using the original mysql_connect() first, if thats o.k., close that connection, then attempt to open a connection with PDO, its a rubbish work around but the only one I can see.

 

Cheers anyway

Link to comment
https://forums.phpfreaks.com/topic/208681-pdo-problem/#findComment-1090233
Share on other sites

Instead of trying to connect to the server for testing, just PING it.  Seems like it would use less resources.

 

You still need an active connection identifier to run the ping on. When I try to connect to obtain that identifer, PHP hangs waiting for the connection, then dies becuase it reaches its maximum execution time. The only way I have been able to get it to work correctly is by doing the following -

 

function Connect()
{
	static $connected = FALSE;
      	$mysql = @mysql_connect($this->DatabaseHost(), $this->DatabaseUser(), $this->DatabasePassword());
	if($mysql)
	{
		mysql_close($mysql);
		if ($connected === FALSE)
		{ 		
			try
			{
				$this->PDO = new PDO($this->PDOConnectionURI, $this->DatabaseUser(), $this->DatabasePassword());
				return TRUE;
			}
			catch(Exception $e)
			{
				$this->Error = "PDOConnection Class - Connect() - " . $e->getMessage();
				return FALSE;
			}
		}
	}
	else
	{
		$this->Error = "PDOConnection Class - Connect() - Could not establish connection to MySQL in the required timeframe.";
		return FALSE;
	}	
}

 

PHP only seems to respect the mysql timeout setting using mysql_connect, mysqli / PDO just seem to hang. Maybe im going about this the wrong way, I don't know. The above script has solved my problem anyway. Thanks.

Link to comment
https://forums.phpfreaks.com/topic/208681-pdo-problem/#findComment-1091173
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.