docker实现mysql主从复制 主db端口3307 从db端口3308
主mysql设置 1、启动mysql 1 2 3 4 5 6 docker run - p 3307 :3306 - v / mydata/ mysql- master/ log:/ var/ log/ mysql - v / mydata/ mysql- master/ data:/ var/ lib/ mysql - v / mydata/ mysql- master/ conf:/ etc/ mysql - e MYSQL_ROOT_PASSWORD= root - d mysql:5.7
2、进入/mydata/mysql-master/conf目录下新建my.cnf my.conf文件内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 [mysqld] # server_id=101 # binlog-ignore-db=mysql # log-bin=mall-mysql-bin # binlog_cache_size=1M # binlog_format=mixed # expire_logs_days=7 # # slave_skip_errors=1062
3、重启mysql,进入mysql进行配置 1 docker restart mysql-master
进入mysql进行配置 1 2 docker exec -it mysql-master /bin/bash mysql -uroot -proot
创建从数据库用户\ 1 2 REATE USER 'slave' @'%' IDENTIFIED BY '123456' ; GRANT REPLICATION SLAVE, REPLICATION CLIENT ON * .* TO 'slave' @'%' ;
从数据库配置 1、启动mysql 1 2 3 4 5 6 docker run -p 3308:3306 --name mysql-slave -v /mydata/mysql-slave/log:/var/log/mysql -v /mydata/mysql-slave/data:/var/lib/mysql -v /mydata/mysql-slave/conf:/etc/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
2、进入/mydata/mysql-slave/conf目录下新建my.cnf my.conf文件内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 [mysqld] # server_id=102 # binlog-ignore-db=mysql # log-bin=mall-mysql-slave1-bin # binlog_cache_size=1M # binlog_format=mixed # expire_logs_days=7 # # slave_skip_errors=1062 # relay_log=mall-mysql-relay-bin # log_slave_updates=1 # read_only=1
3、重启mysql,进入mysql进行配置 1 2 3 4 5 docker restart mysql-slave docker exec -it mysql-slave /bin/bash mysql -uroot -proot CREATE USER 'slave'@'%' IDENTIFIED BY '123456'; GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
在从数据库中配置主从复制 查看pos从哪开始 在master数据库查看position,获取到pos
查看本机ip(master的ip)
回到slave数据库
在slave配置主从复制 1 change master to master_host= ' 192.168.127.129' , master_user= 'slave' , master_password= '123456' , master_port= 3307 , master_log_file= 'mall-mysql-bin.000001' , master_log_pos= 154 , master_connect_retry= 30 ;
master_host(刚才查看的宿主机ip);master_log_pos(刚才在master数据库查看的pos)
在slave查看主从同步状态
此时连接状态为no 开启slave,查看主从同步状态.
1 2 start slave;show slave status \G;
此时连接为yes,即同步成功
4、建表查看 master建表,slave查看 成功