Step 4: Modify Your Database Connection program. since you have established A relationship between server A and server B, you need to modify the Database Connection program to get the benefit from this method. The following function first tries to connect to machine A. If A connection cannot be established, it will connect to Machine B. $ #@ 60 ;? Php/*** SyntaxHighlighter. all ();
Step 4: Modify Your Database Connection program
Since you have established A relationship between Machine A and Machine B, you need to modify the Database Connection program to get the benefit from this method. The following function first tries to connect to machine A. If A connection cannot be established, it will connect to Machine B.
$ #@ 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 two cases, I tested the process of establishing a database connection using the above Technology. One is that the main MySQL service program is closed, but the server is still running, in another case, the master server is disabled. If mysqld is disabled, the connection is immediately switched to the standby server. However, if the entire server is closed, there was an infinite wait (two minutes later I gave up tracking-a very short span of attention), because PHP was looking for a nonexistent server. Unfortunately, unlike the fsockopen function, the mysql_connect function does not have a timeout parameter. However, we can use fsockopen to simulate a timeout process.