mysql-双主环境

搭建一个主从

添加链接描述

添加一台新的服务

修改原来的master中的my.cnf

# endable binlog
log_bin=mysql-bin
server-id=1
sync-binlog=1

# 哪些库同步不步不设置就全步
binlog-ignore-db=information_schema
binlog-ignore-db=mysql
binlog-ignore-db=performance_schema
binlog-ignore-db=sys
# 同步的库
# binlog-db-db=test
# 开启relay_log
relay_log=mysql-relay-bin
log_slave_updates=1
#1,3,5,7ID
auto_increment_offset=1
auto_increment_increment=2


mysql-双主环境_第1张图片
重启原master
在这里插入图片描述

新的master中修改my.cnf

# endable binlog
log_bin=mysql-bin
server-id=4
sync-binlog=1
# 哪些库同步不步不设置就全步
binlog-ignore-db=information_schema
binlog-ignore-db=mysql
binlog-ignore-db=performance_schema
binlog-ignore-db=sys
# 同步的库
# binlog-db-db=test
# 开启relay_log
 relay_log=mysql-relay-bin
 log_slave_updates=1
# #1,3,5,7ID
auto_increment_offset=2
auto_increment_increment=2

重启

授权

grant replication slave on *.* to 'root'@'%' identified by 'root';

grant all privileges ON *.* to 'root'@'%' identified by 'root';
# 刷新权限
flush privileges;

mysql-双主环境_第2张图片

原主库上

change master to master_host='192.168.181.131',master_port=3306,master_user='root',master_password='root',master_log_file='mysql-bin.000005',master_log_pos=884;

start slave;

show slave status \G;

mysql-双主环境_第3张图片
master_host 为第二台master的ip

第二台master 131

change master to master_host='192.168.181.128',master_port=3306,master_user='root',master_password='root',master_log_file='mysql-bin.000003',master_log_pos=154;
start slave;

show slave status \G;

mysql-双主环境_第4张图片

在master1 ,128上建立一个库

mysql-双主环境_第5张图片

create database mymaster1;

mysql-双主环境_第6张图片

  • 创建一张表
create table test1(id int primary key auto_increment,name varchar(20)) engine=innodb charset=utf8;
insert into test1(name) values('a');
insert into test1(name) values('c');
  • 在master2上
    发现mymaster1已经有了
insert into test1(name) values('b');
  • master1
    mysql-双主环境_第7张图片
  • master2
    mysql-双主环境_第8张图片

你可能感兴趣的:(MySQL)