I have to remote mysql servers. One that writes/reads and one that just reads. To balance the load, I randomly choose which database to pull data from with this script:
$updater=mysql_connect ("xxx.196", "user", "pw") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("db",$updater);
$rand = rand(1,2);
if($rand == 1) {
$reader=mysql_connect ("xxx.200", "user", "pw") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("db",$reader);
$myserver = "Mysql2";
} else {
//same DB as the first one listed on top
$reader=mysql_connect ("xxx.196", "user", "pw") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("db",$reader);
$myserver = "Mysql1";
}
Since all my updates/inserts/deletes only go to one of the server, I have $updater connection. But then i have to randomly select one of the two mysql servers. If $updater is selected again, am I now double-connecting to the $updater database? Which results to something like:
$updater=mysql_connect ("xxx.196", "user", "pw") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("db",$updater);
$reader=mysql_connect ("xxx.196", "user", "pw") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("db",$reader);
Am I creating two connections because of this? If so, how can I better write this to avoid this outcome?