I'm using PHP with a replicated MySQL database.
I want to do some database load balancing. Currently I have the rather crude:
$ran = rand(1,10);
if ($ran < 5) {
$db = '10.0.0.2:3306';
} else {
$db = '10.0.0.3:3306';
}
// connect to the database
$con = mysql_connect($db,'elastic1','oag4Chai')
or die('Could not connect to the server!');
But obviously if one of the databases is down, then our web pages will show error messages half the time.
Is there a more robust way to do this - i.e. check if the database is up before connecting?
It could be just a try/catch statement I guess, or I could do some more sophisticated load balancing - suggestions gratefully received.