参考地址:https://www.cnblogs.com/jinjiangongzuoshi/p/9299275.html

1.下载镜像

docker pull mysql:5.7.19

2.建立配用文件

--mysql
  --master
    --data
    --conf
      --my.cnf
  --slave
    --data
    --conf
      --my.cnf

3.master配置文件

 

[mysqld]
server_id = 1

log-bin= mysql-bin

read-only=0

replicate-ignore-db=mysql

replicate-ignore-db=sys

replicate-ignore-db=information_schema

replicate-ignore-db=performance_schema

!includedir /etc/mysql/conf.d/

!includedir /etc/mysql/mysql.conf.d/

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

4.slave配置文件

 

[mysqld]
server_id = 2

log-bin= mysql-bin

read-only=1

replicate-ignore-db=mysql

replicate-ignore-db=sys

replicate-ignore-db=information_schema

replicate-ignore-db=performance_schema

 

!includedir /etc/mysql/conf.d/

!includedir /etc/mysql/mysql.conf.d/

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

5. 启动服务

主服务

docker run --name mastermysql  -d -p 3307:3306 -eMYSQL_ROOT_PASSWORD=123456 ~/docker/mysql-master/data:/var/lib/mysql -v ~/docker/mysql-master/conf/my.cnf:/etc/mysql/my.cnf  3e3878acd190 

从服务

docker run --name slavemysql  -d -p 3308:3306 -eMYSQL_ROOT_PASSWORD=123456 -v  ~/docker/mysql-slave/data:/var/lib/mysql -v ~/docker/mysql-slave/conf/my.cnf:/etc/mysql/my.cnf  imageid

6.进入容器

主服务  

  docker exec -it mastermysql bash

 

  mysql -u root -p

  //查看状态,记住File、Position的值,在Slave中将用到

  show master status/G;

从服务 //进入slaver容器

  docker exec -it slavemysql bash

//启动mysql命令,刚在创建窗口时我们把密码设置为:root
mysql -u root -p
//设置主库链接master_log_file(主服务File)master_log_pos(主服务Position)
change master to master_host='172.17.0.2',master_user='slave',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=0,master_port=3306; //启动从库同步 start slave; //查看状态 show slave status\G;
//启动从库同步
start slave;
show slave status\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

mysql --help |grep 'my.cnf'

 

 

chmod 664 /etc/my.cnf