Fourth step: Modify your database connection program
Now that you have established a relationship between A and B machines, you need to modify the database connection program to get the benefit from this approach. The following function first attempts to connect with a machine, and if the connection cannot be established, it is connected to the B machine.
$#@60;? Php
/********************************************************
function Db_connect ()
Returns a link identifier on success, or false On error
********************************************************/
function Db_connect () {
$username = "Repluser";
$password = "password";
$primary = "10.1.1.1";
$backup = "10.1.1.2";
# attempt connection to Primary
if (! $link _id = @mysql_connect ($primary, $username, $password))
# Attempt connection to secondary
$link _id = @mysql_connect ($secondary, $username, $password)
return $link _id;
}
? $#@62;
In both cases, I tested the database connection setup process using the above technology, one of which was the main MySQL service was closed, but the server was still running, and the other was the primary server was down. If the mysqld is turned off, the connection will immediately turn to the standby machine, but if the entire server shuts down, there is an infinite wait (two minutes later I give up the trace-a short attention span) because PHP is looking for a nonexistent server. Unfortunately, unlike the Fsockopen function, the mysql_connect function does not have a timeout parameter, but we can use Fsockopen to simulate a timeout processing.
http://www.bkjia.com/PHPjc/532383.html www.bkjia.com true http://www.bkjia.com/PHPjc/532383.html techarticle Fourth Step: Modify your database connection program now that you have established a relationship between A and B machines, you need to modify the database connection program so that you can get it in this way ...