Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

How to mysql connect() to a different server if the other is overloade

6:13 am on Nov 1, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:Oct 5, 2006
votes: 0

Ok, I know you can simply write a script that detect if a database is up or not, and then reconnect to another like so:

$con = mysql_connect($host, $user, $pass);
if (!$con) //try to connect to another database now


My question is, sometimes one of our databases is overloaded, it is still responsive but simply waits to connect because the load is to high. So the connect script just keeps loading, because the database is there. It just will never stop loading the php script because the database is alive, but is to overloaded to connect fast, so it just waits untill the load passes and then connects after its done.

Is there any way to modify it, where it'd say, if it can not connect within 5 seconds, for instance (because its overloaded) then try connecting to a different one.

if ( can't connect within 5 seconds to this database ) do something else?

12:29 am on Nov 2, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Oct 4, 2001
posts: 1262
votes: 12

You should be able to accomplish what you need with the mysql.connect_timeout ini setting.

Another option would be to check the load on the database server (using a shell command) and use a second server if it's over (for example) 5.