Test New MySQL functions (2) _ MySQL

Source: Internet
Author: User
Tags mysql functions
MySQL new function test (2) prompt: the MySQL internal replication function is implemented between two or more servers by setting the master-slave relationship between them. One of them serves as the master server and the other as the slave server. I will discuss in detail how to configure two servers, one as the master server and the other as the slave server. And describes the process of switching between them. I am

Step 3: Create a master-slave relationship

First, my. in the cnf file, add log-bin to the [mysqld] Section, restart mysqld, and create a user account on which the copy function can be executed. use:

Grant file on *. * TO replicate@10.1.1.1 identified by password;

Run the flush privileges command on Machine B to load the new authorization table after the user is copied. then, return to machine A and add the following lines to its my. cnf:

Master-host = 10.1.1.2

Master-user = replicate

Master-password = password

After restarting the Service Program of host A, we now have A master-slave relationship between host A and host B. No matter which server updates a record or inserts a record, it will be copied to another server. Note: I am not sure how fast a slave machine can merge binary logs. Therefore, using this method for load balancing of insert or update statements may not be a good solution.

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.

/*************************************** *****************
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;

}

?>

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.