Optimize availability with MySQL built-in replication (5)

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.