master1:192.168.128.47
master2:192.168.128.96
slave:192.168.128.97
master1与master2互备,master2作为slave的master
①,在master1端:
修改my.cnf
server-id = 1
auto_increment_increment = 2
auto_increment_offset = 1
#binlog_do_db = sssjpt,sakila,test --可选,用来指定master端的那些db操作记录binlog
#binlog_ignore_db = mysql --可选,用来指定master端的那些db操作不记录binlog
#replicate_do_db = sssjpt,sakila,test --可选,用来指定slave端的SQL线程应用master端传来的哪些DB binlog
#replicate_ignore_db = mysql
log_slave_updates --同步master端的二进制日志来完成同步时的操作也记录当前库二进制日志,这样可以将该slave作为另一个slave的master
参数解释:
auto_increment_increment = 2
auto_increment_offset = 1
主要用于master-master端,因为两台master节点都有可能同时发生写操作,这样的话就有可能发生auto_increment 键值冲突,现在将auto_increment_increment=2,auto_increment_offset=1,在master1端每次执行自增操作时都会增加比当前数据最大值大2的数据,并且从最大值的后面第1个位置开始,每次增加2个数。如,1,3,5,7 。。。
将auto_increment_increment=2,auto_increment_offset=2,在master2端每次执行自增操作时都会增加比当前数据最大值大2的数据,并且从最大值的后面第2个位置开始,每次增加2个数,如,2,4,6,8 。。。
如果在主从同步中只让一个master执行写的话,则没有比较设置auto_increment_increment,auto_increment_offset参数
不过在新版本的mysql里面,可以通过innodb_autoinc_lock_mode参数来限制
②,
master1:
grant replication slave to *.* on [email protected]‘192.168.128.96‘ identified by ‘123456‘;
show master status;
mysqldump -uroot -p --databases db1 db2 --lock-all-tables > /tmp/dump.sql
scp /tmp/dump.sql [email protected]:/tmp
master2:
mysql -uroot -p < /tmp/dump.sql
change master to master_host=‘192.168.128.47‘,master_user=‘backup‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000001‘,master_log_pos=120;
start slave;
③,在master2端:
修改my.cnf
server-id = 2
auto_increment_increment = 2
auto_increment_offset = 2
#binlog_do_db = sssjpt,sakila,test
#binlog_ignore_db = mysql
#replicate_do_db = sssjpt,sakila,test
#replicate_ignore_db = mysql
log_slave_updates --同步master端的二进制日志来完成同步时的操作也记录当前库二进制日志,这样可以将该slave作为另一个slave的master
④,
master2:
grant replication slave to *.* on [email protected]‘192.168.128.47‘ identified by ‘123456‘;
show master status;
master1:
change master to master_host=‘192.168.128.96‘,master_user=‘backup‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000001‘,master_log_pos=120;
start slave;
⑤,
master1:
scp /tmp/dump.sql [email protected]:/tmp
slave:
修改my.cnf
server-id = 3
#binlog_do_db = sssjpt,sakila,test
#binlog_ignore_db = mysql
#replicate_do_db = sssjpt,sakila,test
#replicate_ignore_db = mysql
mysql -uroot -p < /tmp/dump.sql
change master to master_host=‘192.168.128.97‘,master_user=‘backup‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000001‘,master_log_pos=120;
start slave;