Master-master cyclic synchronization from a MySQL master to a master

Source: Internet
Author: User
I just took the time to perform master-master synchronization for MySQL.
Write down the steps and update them at any time for any problems. Here, the database I synchronized is test.
1. Environment Description.
HOST: 192.168.0.231 ()
HOST: 192.168.0.20.( B)
MySQL version 5.1.21
2. Authorize the user.
A:
Mysql> grant replication slave, file on *. * To 'repl1' @ '192. 168.0.20.' identified
By '123 ';
Query OK, 0 rows affected (0.00 Sec)

Mysql> flush privileges;
Query OK, 0 rows affected (0.00 Sec)
B:
Mysql> grant replication slave, file on *. * To 'repl2' @ '192. 168.0.231 'identified
By '123 ';
Query OK, 0 rows affected (0.00 Sec)

Mysql> flush privileges;
Query OK, 0 rows affected (0.00 Sec)
Then stop the MySQL server.

3. configuration file.
Enable binary logs in my. CNF on both machines.
A:
User = MySQL
Log-bin = mysql-bin
Server-id = 1
BINLOG-do-DB = test
BINLOG-ignore-DB = MySQL
Replicate-do-DB = test
Replicate-ignore-DB = MySQL
Log-slave-Updates
Slave-Skip-errors = all
Sync_binlog = 1
Auto_increment_increment = 2
Auto_increment_offset = 1

B:
User = MySQL
Log-bin = mysql-bin
Server-id = 2
BINLOG-do-DB = test
BINLOG-ignore-DB = MySQL
Replicate-do-DB = test
Replicate-ignore-DB = MySQL
Log-slave-Updates
Slave-Skip-errors = all
Sync_binlog = 1
Auto_increment_increment = 2
Auto_increment_offset = 2
For more information about these parameters, see the manual.
The red part is very important. If one master fails, the other master will take over immediately.
The red part indicates frequent log refreshing on the server. This ensures that the log is Refresh to the other if one fails. This ensures data synchronization.
4. Restart the MySQL server.
Perform the same steps on A and B
[Root @ localhost ~] #/Usr/local/MySQL/bin/mysqld_safe &
[1] 4264
[Root @ localhost ~] #071213 14:53:20 mysqld_safe logging to '/usr/local/MySQL/data/localhost. localdomain. err '.
/Usr/local/MySQL/bin/mysqld_safe: Line 366: [:-EQ: unary operator expected
071213 14:53:20 mysqld_safe starting mysqld daemon with databases from/usr/local/MySQL/Data

5. Enter the MySQL shell.
A:
Mysql> flush tables with read lock \ G
Query OK, 0 rows affected (0.00 Sec)

Mysql> show Master Status \ G
* *************************** 1. row ***************************
File: mysql-bin.000007
Position: 528
Binlog_do_db: Test
Binlog_ignore_db: MySQL
1 row in SET (0.00 Sec)

B:
Mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 Sec)

Mysql> show Master Status \ G
* *************************** 1. row ***************************
File: mysql-bin.000004
Position: 595
Binlog_do_db: Test
Binlog_ignore_db: MySQL
1 row in SET (0.00 Sec)
Back up your data to ensure the data consistency between the two machines.
There are many methods. Check the next step.
6. Execute the change master to command on each machine.
A:
Mysql> change master
-> Master_host = '1970. 168.0.20 ',
-> Master_user = 'repl2 ',
-> Master_password = '123 ',
-> Master_log_file = 'mysql-bin.000004 ',
-> Master_log_pos = 595;
Query OK, 0 rows affected (0.01 Sec)
Mysql> Start slave;
Query OK, 0 rows affected (0.00 Sec)

B:
Mysql> change master
-> Master_host = '192. 168.0.231 ',
-> Master_user = 'repl1 ',
-> Master_password = '123 ',
-> Master_log_file = 'mysql-bin.000007 ',
-> Master_log_pos = 528;
Query OK, 0 rows affected (0.01 Sec)
Mysql> Start slave;
Query OK, 0 rows affected (0.00 Sec)

7. Check whether the I/O and slave processes on the respective machines are enabled.
A:

Mysql> show processlist \ G
* *************************** 1. row ***************************
ID: 2
User: repl
HOST: 192.168.0.20.: 54475
DB: NULL
Command: BINLOG dump
Time: 1590
State: has sent all BINLOG to slave; waiting for BINLOG to be updated
Info: NULL
* *************************** 2. row ***************************
ID: 3
User: system user
Host:
DB: NULL
Command: connect
Time: 1350
State: Waiting for Master to send event
Info: NULL
* *************************** 3. row ***************************
ID: 4
User: system user
Host:
DB: NULL
Command: connect
Time: 1149
State: has read all relay log; waiting for the slave I/O thread to update it
Info: NULL
* *************************** 4. row ***************************
ID: 5
User: Root
HOST: localhost
DB: Test
Command: Query
Time: 0
State: NULL
Info: Show processlist
4 rows in SET (0.00 Sec)

B:

Mysql> show processlist \ G
* *************************** 1. row ***************************
ID: 1
User: system user
Host:
DB: NULL
Command: connect
Time: 2130
State: Waiting for Master to send event
Info: NULL
* *************************** 2. row ***************************
ID: 2
User: system user
Host:
DB: NULL
Command: connect
Time: 1223
State: has read all relay log; waiting for the slave I/O thread to update it
Info: NULL
* *************************** 3. row ***************************
ID: 4
User: Root
HOST: localhost
DB: Test
Command: Query
Time: 0
State: NULL
Info: Show processlist
* *************************** 4. row ***************************
ID: 5
User: repl2
HOST: 192.168.0.231: 50718
DB: NULL
Command: BINLOG dump
Time: 1398
State: has sent all BINLOG to slave; waiting for BINLOG to be updated
Info: NULL
4 rows in SET (0.00 Sec)

If the red part does not appear, check the error file in the data directory.

8. Release the locks and test data insertion.
Mysql> unlock tables;
Query OK, 0 rows affected (0.00 Sec)

Comparison of the previous two machine tables:
A:

Mysql> show tables;
+ ---------------- +
| Tables_in_test |
+ ---------------- +
| T11_innodb |
| T22 |
+ ---------------- +
B:

Mysql> show tables;
+ ---------------- +
| Tables_in_test |
+ ---------------- +
| T11_innodb |
| T22 |
+ ---------------- +
Insert from machine
A:
Mysql> Create Table t11_replicas
-> (ID int not null auto_increment primary key,
-> STR varchar (255) not null) engine MyISAM;
Query OK, 0 rows affected (0.01 Sec)

Mysql> insert into t11_replicas (STR) Values
-> ('This is a master to master test table ');
Query OK, 1 row affected (0.01 Sec)

Mysql> show tables;
+ ---------------- +
| Tables_in_test |
+ ---------------- +
| T11_innodb |
| T11_replicas |
| T22 |
+ ---------------- +
3 rows in SET (0.00 Sec)

Mysql> select * From t11_replicas;
+ ---- + --------------------------------------- +
| ID | STR |
+ ---- + --------------------------------------- +
| 1 | this is a master to master test table |
+ ---- + --------------------------------------- +
1 row in SET (0.00 Sec)

Now let's look at machine B:

Mysql> show tables;
+ ---------------- +
| Tables_in_test |
+ ---------------- +
| T11_innodb |
| T11_replicas |
| T22 |
+ ---------------- +
3 rows in SET (0.00 Sec)

Mysql> select * From t11_replicas;
+ ---- + --------------------------------------- +
| ID | STR |
+ ---- + --------------------------------------- +
| 1 | this is a master to master test table |
+ ---- + --------------------------------------- +
1 row in SET (0.00 Sec)

Insert data from the B server in turn:
B:

Mysql> insert into t11_replicas (STR) values ('this is a test 2 ');
Query OK, 1 row affected (0.00 Sec)

Mysql> select * From t11_replicas;
+ ---- + --------------------------------------- +
| ID | STR |
+ ---- + --------------------------------------- +
| 1 | this is a master to master test table |
| 2 | this is a test 2 |
+ ---- + --------------------------------------- +
2 rows in SET (0.00 Sec)
Let's look at.
A:
Mysql> select * From t11_replicas;
+ ---- + --------------------------------------- +
| ID | STR |
+ ---- + --------------------------------------- +
| 1 | this is a master to master test table |
| 2 | this is a test 2 |
+ ---- + --------------------------------------- +
2 rows in SET (0.00 Sec)

Okay. Now the two tables are master nodes.

Multi-master auto-increment field conflict.
For more information, see:
Http://dev.mysql.com/tech-resources/articles/advanced-mysql-replication.html

In the mail list, we can see someone discussing online synchronization and ignoring database and table issues. For details, refer:
Http://dev.mysql.com/doc/refman/5.1/en/replication-rules.html

Related Article

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.