0.环境介绍
设计的集群节点为master1、master2、slave1。其中master2到slave1的主从复制已经完成。现在需要配置master1和master2之间的相互复制。
1. 修改master2参数文件(/etc/my.cnf)增加/修改如下参数:log-bin=mysql-binlog-bin-index=mysql-bin.indexserver-id = 114binlog-do-db=mytest_dbrelay-log=relay-binrelay-log-index=relay-bin-indexreplicate-do-db=mytest_dblog_slave_updates=1auto_increment_increment=2auto_increment_offset=12. 重启master2
# service mysqld restart3. 修改master1参数
log-bin-index=mysql-bin.index #新增server-id = 113 #修改binlog-do-db=mytest_db #新增relay-log=relay-bin #新增relay-log-index=relay-bin-index #新增replicate-do-db=mytest_db #新增log_slave_updates=1 #新增auto_increment_increment=2 #新增auto_increment_offset=2 #新增#auto-increment-increment= 2 #删除#auto-increment-offset= 2 #删除4. 重启master1
# service mysqld restart5. 在master1添加一个用户 repl 并指定replication权限
create user 'repl'@'master2_IP' identified by 'password';GRANT REPLICATION SLAVE ON *.* TO 'repl'@'master2_IP';6. 生成master1备份数据
#锁定表mysql> FLUSH TABLES WITH READ LOCK;Query OK, 0 rows affected (0.00 sec)#查看状态mysql> show master status;+------------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin.000021 | 5337 | mytest_db | |+------------------+----------+--------------+------------------+1 row in set (0.00 sec)#导出数据[root@localhost ~]# mysqldump -p mytest_db > /usr/software/tianyc/mytest_db_master.dumpEnter password: [root@localhost ~]##解锁表mysql> unlock tables;Query OK, 0 rows affected (0.00 sec)7. 在master2上还原数据
#拷贝文件到master2,执行导入[root@localhost tianyc]# mysql -p mytest_db < /usr/software/tianyc/mytest_db_master.dumpEnter password: [root@localhost tianyc]#8. 在master2上设置它的主库为master1,设置binlog位置,并开启复制。
mysql> change master to master_host='master1_IP',master_port=3306,master_user='repl',master_password='password',master_log_file='mysql-bin.000021',master_log_pos=5337;Query OK, 0 rows affected (0.05 sec)mysql> start slave;
Query OK, 0 rows affected (0.00 sec)mysql> show slave status\G
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: master1_IPMaster_User: replMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000021Read_Master_Log_Pos: 6036Relay_Log_File: relay-bin.000002Relay_Log_Pos: 952Relay_Master_Log_File: mysql-bin.000021Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: mytest_dbReplicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 6036Relay_Log_Space: 1102Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1131 row in set (0.00 sec)9. 此时可验证master1到master2的复制过程。在master1上建表,可以传递给master2,master2又生成binlog传递给slave1
10. 采用同样的方式,创建master2到master1的映射。
#在master2上创建复制用户repl,并授权给master1使用。create user 'repl'@'master1_IP' identified by 'password';GRANT REPLICATION SLAVE ON *.* TO 'repl'@'master1_IP';#查看master2的master状态:
mysql> show master status;+------------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin.000006 | 610578 | mytest_db | |+------------------+----------+--------------+------------------+1 row in set (0.00 sec)#此时master1和master2数据库一致,且master2不做更改,所以省去了master2到master1同步数据的过程。
#直接在master1上设置它的主节点:mysql> change master to master_host='master2_IP',master_port=3306,master_user='repl',master_password='password',master_log_file='mysql-bin.000006',master_log_pos=610578;Query OK, 0 rows affected (0.02 sec)mysql> start slave;
Query OK, 0 rows affected (0.00 sec)mysql> show slave status\G
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: master2_IPMaster_User: replMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000006Read_Master_Log_Pos: 611282Relay_Log_File: relay-bin.000002Relay_Log_Pos: 253Relay_Master_Log_File: mysql-bin.000006Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: mytest_dbReplicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 611282Relay_Log_Space: 403Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1141 row in set (0.00 sec)11. 再将maste1、master2、slave1用amoeba整合成集群,测试集群复制情况。