- 拉取镜像
docker pull mysql:5.7
- 新建主服务器容器实例3307
docker run -p 3307:3306 --privileged=true --name mysql-master \
-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
- 宿主机conf下新建my.cnf
vim my.cnf
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[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
init_connect=SET collation_connection = utf8_unicode_ci
init_connect=SET NAMES utf8
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
- 重启mysql-master
docker restart mysql-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'@'%';
- 新建从服务器容器实例3308
docker run -p 3308:3306 --privileged=true --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
- 同主一样编辑配置文件并重启slave容器
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
server_id=102
binlog-ignore-db=mysql
log-bin=mall-mysql-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
init_connect=SET collation_connection = utf8_unicode_ci
init_connect=SET NAMES utf8
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
docker restart mysql-slave
- 从主数据库查看主从同步状态
show master status;
- 从数据库配置主从复制
change master to master_host='10.0.12.2',master_user='slave',master_password='123456',master_port=3307,master_log_file='mall-mysql-bin.000001',master_log_pos=617,master_connect_retry=30;
- 从数据库查看主从同步状态
show slave status \G;
- 从数据库开启主从同步
start slave;
yes表明同步已开启;
- 主从复制测试
主库插入数据: 从库可查询到:
|