mysql主主备份同步,其实就是互相主从,A对B主从,B对A主从
配置起来与mysql 主从差不多,只是有点小地方需要改动一下
注:在实验之前请先关闭iptables和selinux ,iptables -F && service iptables save
vim /etc/selinux/config 中selinux值改成disabled
环境描述:A:192.168.3.116
B:192.168.3.251
给ABmysql设置登录密码,命令:mysqladmin -uroot password 'yourpass'
各登录ABmysql
A:grant replication slave,file on *.* to 'repl'@'192.168.3.251' identified by '123456';
flush privileges;
B: grant replication slave,file on *.* to 'repl'@'192.168.3.116' identified by '123456';
flush privileges;
配置各mysql配置文件
A:
vim /etc/my.cnf
在server-id出修改并添加如下语句
server-id = 1
binlog-do-db=test
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
B:
server-id = 2
binlog-do-db=test
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=2
其中:log-slave-updates 将执行的复制sql记录到二进制日志
sync_binlog 当有二进制日志写入binlog文件的时候,mysql服务器将它同步到磁盘上
auto_increment_increment,auto_increment_offset 主要用于主主复制中,用来控制AUTO_INCREMENT列的操作
重启ABmysql服务,并进入MySQL数据库
A:flush tables with read lock;
show master status;查看此时A的master_log_file 和master_log_pos值
unlock tables;
slave stop;
change master to master_host='192.168.3.251',master_user='repl',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=106;#此处的master_log_file和master_log_pos为B上show master status值
start slave;
B:
flush tables with read lock;
show master status;查看此时B的master_log_file 和master_log_pos值
unlock tables;
slave stop;
change master to master_host='192.168.3.116',master_user='repl',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=106;#此处的master_log_file和master_log_pos为A上show master status值
start slave;
查看状态,在A和B上分别show slave status;若同时出现
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
则表示互为主从配置成功;
测试:针对同步的数据库test操作,分别在A上create table tb1 ,在B上show tables;若出现A上创建的tb1则表示A到B同步成功;在B上create table tb2,A上show tables;出现tb2,则表示B到A同步成功;