- 首先创建主节点
docker run -d -p 3308:3306 \
--privileged=true \
-v /Users/jackchai/Desktop/lottory_docker/learndocker/mymysql/master/log:/var/log/mysql \
-v /Users/jackchai/Desktop/lottory_docker/learndocker/mymysql/master/data:/var/lib/mysql \
-v /Users/jackchai/Desktop/lottory_docker/learndocker/mymysql/master/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
--name mysql-master \
mysql
然后给主节点创建一个my.cnf配置文件:
[mysqld]
server_id=101
binlog-ignore-db=mysql
log-bin=mall-mysql-bin
binlog_cache_size=1M
binlog_format=mixed
slave_skip_errors=1062
然后我们需要重启master实例
然后我们进入mysql-master容器实例:
docker exec -it mysql-master /bin/bash
然后我们在master上创建一个数据同步的用户(让数据同步交给可信任的用户处理):
CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
我们可以查看主节点状态:
show master status;
- 创建从节点
docker run -d -p 3309:3306 \
--privileged=true \
-v /Users/jackchai/Desktop/lottory_docker/learndocker/mymysql/slave/log:/var/log/mysql \
-v /Users/jackchai/Desktop/lottory_docker/learndocker/mymysql/slave/data:/var/lib/mysql \
-v /Users/jackchai/Desktop/lottory_docker/learndocker/mymysql/slave/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
--name mysql-slave \
mysql
同样需要创建配置文件my.cnf
[mysqld]
server_id=102
binlog-ignore-db=mysql
log-bin=mall-mysql-bin
binlog_cache_size=1M
binlog_format=mixed
slave_skip_errors=1062
relay_log=mall-mysql-relay-bin
log_slave_updates=1
read_only=1
同样需要重启mysql容器
然后我们在主节点上查看mysql主从架构的状态:
show master status;
然后我们进入从机容器中:
然后在从机中配置主从复制:
CHANGE MASTER TO
master_host='192.168.31.41',
master_port=3308,
master_user='slave',
master_password='123456',
master_log_file='mall-mysql-bin.000001',
master_log_pos=617;
master_host:主数据库ip地址
master_port:主数据库端口
master_user:主数据库创建的用于同步数据库的用户账号
master_password:用户账号对应的密码
master_log_file:指定从数据库要复制数据的日志文件,通过查看主数据库的状态,获取file参数
master_log_pos:指定从数据库哪个位置开始复制数据
master_connect_retry:连接失败重试的时间间隔,单位为秒
然后我们在从数据库汇总查看主从同步状态:
show slave status\G
在从数据库中开启主从同步:
start slave