MySQLAB双向复制
好了,MySQL AB单向复制介绍完毕。接下来想想,会有这样的应用场景。比如Master和Slave之间都要进行数据同步,那么单向复制是无法完成的,因为一个是Master,一个是Slave,只能单向操作,这就像网络里的半双工一样。既然一方可以向另一方同步数据,那么两方都做成Master 不就可以实现互相同步数据了。这就是接下来要介绍的MySQL AB双向复制。同样我们来看看MySQL AB双向复制的拓扑图。

图二 MySQL AB双向复制
既然对拓扑图和原理有所了解,我们做一个实验,介绍如何使用MySQL AB双向复制,注意该实验是在MySQL单级复制的基础上做的。
实验环境介绍
主机 IP地址 主机名 备注
serv01: 192.168.1.11 serv01.host.com master
serv08: 192.168.1.18 serv08.host.com slave01
操作系统版本:rhel server 6.1
所需要的软件包:mysql-5.5.29-linux2.6-x86_64.tar.gz
第一步,serv08创建授权用户
mysql> grant replication client, replication slave on *.* to 'larry'@'192.168.1.%' identified by 'larry'; Query OK, 0 rows affected (0.01 sec)
第二步,serv08清空日志
mysql> show binary logs; +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000001 | 286 | +------------------+-----------+ 1 row in set (0.00 sec) mysql> reset master; Query OK, 0 rows affected (0.00 sec) mysql> show binary logs; +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000001 | 107 | +------------------+-----------+ 1 row in set (0.00 sec)
第三步,serv01使用change master to命令修改从服务器设置
mysql> show slave status;
Empty set (0.00 sec)
mysql> change master to
-> master_host='192.168.1.18',
-> master_user='larry',
-> master_password='larry',
-> master_port=3306,
-> master_log_file='mysql-bin.000001',
-> master_log_pos=107;
Query OK, 0 rows affected (0.01 sec)
第四步,serv01开启slave
mysql> start slave; Query OK, 0 rows affected (0.01 sec) mysql> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.18 Master_User: larry Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 107 Relay_Log_File: serv01-relay-bin.000002 Relay_Log_Pos: 253 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 107 Relay_Log_Space: 410 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 2 1 row in set (0.00 sec) ERROR: No query specified
第五步,测试
--serv01查看数据 mysql> use larrydb; Database changed mysql> select * from test; +------+ | id | +------+ | 1 | +------+ 1 row in set (0.00 sec) --serv08插入数据 mysql> use larrydb; Database changed mysql> select * from test; +------+ | id | +------+ | 1 | +------+ 1 row in set (0.00 sec) mysql> insert into test values(2); Query OK, 1 row affected (0.00 sec) --serv01查看数据,数据更新 mysql> select * from test; +------+ | id | +------+ | 1 | | 2 | +------+ 2 rows in set (0.00 sec) --serv01插入数据 mysql> insert into test values(3); Query OK, 1 row affected (0.01 sec) --serv01查询数据 mysql> select * from test; +------+ | id | +------+ | 1 | | 2 | | 3 | +------+ 3 rows in set (0.00 sec) --serv08查询数据,数据已更新 mysql> select * from test; +------+ | id | +------+ | 1 | | 2 | | 3 | +------+ 3 rows in set (0.00 sec)
