本文简单介绍MySQL双主复制原理及1个简单是双主复制验证。
master-master复制的两台服务器,既是master,又是另一台服务器的slave,本质上互为主从。
CentOS-6.7-x86_64
MySQL版本是5.6.36:https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.36.tar.gz
- #在主从复制配置文件的基础上增加3个参数项 [root@master~]#vim / etc / my.cnf[mysqld] server_id = 196 log_bin = /mysql/mysql - bin max_binlog_size = 1G sync_binlog = 0 binlog - format = mixed binlog - ignore - db = information_schema,
- mysql,
- performance_schema,
- test
- #中继日志执行之后将变化写入自己的binlog文件,
- 即从库从主库复制的文件默认不会写入自己的binlog文件,
- 需要开启后才生效;#通常此从库同时作为主库时,
- 即链式复制时,
- 需要开启此参数;#默认参数为0,
- 表示OFF,
- 设置为1表示ON,
- 参数可直接带OFF或ON.log - slave - updates = 1
- #做双主时,
- 每台数据库都可能在同一个表中插入数据,
- 如果表有一个自动增长的主键,
- 那么就会在多服务器上出现主键冲突;解决方案是让每个数据库的自增主键不连续;#参数auto_increment_increment表示自增值,
- 一般有n台主库,
- 自增值就采用n;#auto_increment_offset表示起始序号,
- 一般offset不超过自增值,
- 且各主库的自增值不一样.auto_increment_increment = 2 auto_increment_offset = 1
- #使用--skip - slave - start启动,可以不立即启动从库的复制线程,方便后续配置操作 [root@master~]#service mysqld stop[root@master~]#mysqld_safe--skip - slave - start &
- #在主库上10.11.4.0网段的主机授权,从库用户repl获得REPLICATION SLAVE权限
- [root@master ~]# mysql -uroot -p
- Enter password:
- mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'10.11.4.%' IDENTIFIED BY 'repl';
- mysql> flush privileges;
- [root@master ~]# mysql -uroot -p
- Enter password:
- mysql> show master status;
- [root@master ~]# vim /etc/sysconfig/iptables
- -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
- [root@master ~]# service iptables restart
- #将主库服务器上的my.cnf文件拷贝到从库服务器
- [root@master ~]# scp /usr/local/mysql/my.cnf backup:/usr/local/mysql/
- root@backup's password:
- #修改server id值与主库服务器不同;
- #auto_increment_offset参数,各服务器的offset值应不一样
- [root@backup ~]# vim /etc/my.cnf
- [mysqld]
- server_id = 197
- auto_increment_increment = 2
- auto_increment_offset = 2
- #使用--skip-slave-start启动,可以不立即启动从库的复制线程,方便后续配置操作
- [root@backup ~]# service mysqld stop
- [root@backup ~]# mysqld_safe --skip-slave-start &
- #在主库上10.11.4.0网段的主机授权,从库用户repl获得REPLICATION SLAVE权限
- [root@backup ~]# mysql -uroot -p
- Enter password:
- mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'10.11.4.%' IDENTIFIED BY 'repl';
- mysql> flush privileges;
- [root@backup ~]# mysql -uroot -p
- Enter password:
- mysql> show master status;
- [root@backup ~]# vim /etc/sysconfig/iptables
- -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
- [root@backup ~]# service iptables restart
- #配置从库向主库提交的参数,如果参数有错误,可以重新配置;
- # master-host、master-user、master-password、master-port等也可在my.cnf文件中指定;
- #”start slave“启动复制
- [root@master ~]# mysql -uroot -p
- Enter password:
- mysql> change master to
- master_host = '10.11.4.197',
- master_user = 'repl',
- master_password = 'repl',
- master_log_file = 'mysql-bin.000009',
- master_log_pos = 1306;
- mysql> start slave;
- [root@backup ~]# mysql -uroot -p
- Enter password:
- mysql> change master to
- master_host = '10.11.4.196',
- master_user = 'repl',
- master_password = 'repl',
- master_log_file = 'mysql-bin.000003',
- master_log_pos = 2462;
- mysql> start slave;
- [root@master ~]# mysql -uroot -p
- Enter password:
- mysql> show processlist;
- [root@backup ~]# mysql -uroot -p
- Enter password:
- mysql> show processlist;
- [root@master ~]# mysql -uroot -p
- Enter password:
- mysql> show slave status\G;
- [root@backup ~]# mysql -uroot -p
- Enter password:
- mysql> show slave status\G;
- [root@master~]#mysql - uroot - p Enter password:
- mysql > create database dbtest2;
- mysql > use dbtest2;
- mysql > create table tabtest2(id int);
- mysql > insert into tabtest2() values(1),
- (2);
- [root@backup ~]# mysql -uroot -p
- Enter password:
- mysql > show databases;
- mysql > select * from dbtest2.tabtest2;
- [root@backup~]#mysql - uroot - p Enter password:
- mysql > use dbtest2;
- mysql > insert into tabtest2() values(3),
- (4);
- [root@master ~]# mysql -uroot -p
- Enter password:
- mysql> select * from dbtest2.tabtest2;
来源: http://www.linuxidc.com/Linux/2017-10/147549.htm