我们来搭建两对Mysql主从复制;
前面我们搭建过一对,
用的是:
docker run -p 3306:3306 --name master -d -v /home/mysql/mysql.conf.d/:/etc/mysql/mysql.conf.d/ -v /home/mysql/log/:/var/log --net extnetwork --ip 172.20.0.2 -e MYSQL_ROOT_PASSWORD=123456 镜像ID
docker run -p 3307:3306 --name slave -d -v /home/mysql2/mysql.conf.d/:/etc/mysql/mysql.conf.d/ -v /home/mysql2/log/:/var/log --net extnetwork --ip 172.20.0.3 -e MYSQL_ROOT_PASSWORD=123456 镜像ID
再启动两个
docker run -p 3308:3306 --name master2 -d -v /home/mysql3/mysql.conf.d/:/etc/mysql/mysql.conf.d/ -v /home/mysql3/log/:/var/log --net extnetwork --ip 172.20.0.5 -e MYSQL_ROOT_PASSWORD=123456 镜像ID
docker run -p 3309:3306 --name slave2 -d -v /home/mysql4/mysql.conf.d/:/etc/mysql/mysql.conf.d/ -v /home/mysql4/log/:/var/log --net extnetwork --ip 172.20.0.6 -e MYSQL_ROOT_PASSWORD=123456 镜像ID
m1,m2我们要加配置:涉及到数据插入id不冲突,以及数据同步;
# 在作为从数据库的时候,有写入操作也要更新二进制日志文件
log-slave-updates
#表示自增长字段每次递增的量,指自增字段的起始值,其默认值是1,取值范围是1 .. 65535
auto-increment-increment=2
# 表示自增长字段从哪个数开始,指字段一次递增多少,他的取值范围是1 .. 65535
auto-increment-offset=1
m2 的 server-id=5
s2的 server-id=6
配置OK后,我们重新搭界主从复制;
m1 删除user里的原先slave用户,重新权;
CREATE USER 'slave'@'172.20.0.%' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'172.20.0.%';
FLUSH PRIVILEGES;
RESET MASTER
s1 配置主从复制;
CHANGE MASTER TO MASTER_HOST='172.20.0.2', MASTER_USER='slave', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=154;
START SLAVE
SHOW SLAVE STATUS 测试;
m2 创建slave用户,授权
CREATE USER 'slave'@'172.20.0.%' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'172.20.0.%';
FLUSH PRIVILEGES;
RESET MASTER
s2 配置主从复制;
CHANGE MASTER TO MASTER_HOST='172.20.0.5', MASTER_USER='slave', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=154;
START SLAVE
SHOW SLAVE STATUS 测试;
下一篇:m1和m2配置互为主从复制