操作步骤
下载镜像
目前我测试最新的8.0.27是不能测试成功的,不知道原因出在哪里,保守一点使用5.7的版本。
实现效果:
1 2 3 root@knight:/docker# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql latest 3218b38490ce 19 months ago 516MB
生成主数据库容器
1 2 3 4 5 6 7 8 docker run -d -p 3307:3306 \ --privileged=true \ -v /docker/mysql-master/log:/var/log/mysql \ -v /docker/mysql-master/data:/var/lib/mysql \ -v /docker/mysql-master/conf:/etc/mysql/conf.d \ -e MYSQL_ROOT_PASSWORD=admin \ --name mysql-master \ mysql:latest
具体的参数详解,可以查看这篇文章
实现效果:
1 2 3 root@knight:/docker# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a1f6f6e03029 mysql:latest "docker-entrypoint.s…" 6 seconds ago Up 5 seconds 33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp mysql-master
添加配置文件
进入/docker/mysql-master/conf
目录,编辑配置文件my.cnf
,插入以下内容:
折叠代码 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=com-mysql-bin binlog_cache_size=1M binlog_format=mixed expire_logs_days=7 slave_skip_errors=1062
重启容器
1 docker restart mysql-master
进入容器
1 docker exec -it mysql-master /bin/bash
授权用户
进入数据库,添加授权用户。
1 2 CREATE USER 'slave' @'%' IDENTIFIED BY '123456' ;GRANT REPLICATION SLAVE, REPLICATION CLIENT ON * .* TO 'slave' @'%' ;
实现效果:
1 2 3 4 5 6 7 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> CREATE USER 'slave' @'%' IDENTIFIED BY '123456' ; Query OK, 0 rows affected (0.03 sec) mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON * .* TO 'slave' @'%' ; Query OK, 0 rows affected (0.02 sec)
查看主数据库的主状态
1 2 3 4 5 6 7 mysql> show master status; + | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | + | com- mysql- bin.000001 | 156 | | mysql | | + 1 row in set (0.00 sec)
创建从数据库容器
1 2 3 4 5 6 7 8 docker run -d -p 3308:3306 \ --privileged=true \ -v /docker/mysql-slave/log:/var/log/mysql \ -v /docker/mysql-slave/data:/var/lib/mysql \ -v /docker/mysql-slave/conf:/etc/mysql/conf.d \ -e MYSQL_ROOT_PASSWORD=admin \ --name mysql-slave \ mysql:latest
实现效果:
1 2 3 4 root@knight:/docker# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 06a84db29686 mysql:latest "docker-entrypoint.s…" 3 seconds ago Up 2 seconds 33060/tcp, 0.0.0.0:3308->3306/tcp, :::3308->3306/tcp mysql-slave a1f6f6e03029 mysql:latest "docker-entrypoint.s…" About a minute ago Up About a minute 33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp mysql-master
添加配置文件
进入/docker/mysql-slave/conf
目录,编辑配置文件my.cnf
,插入以下内容:
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=com-mysql-slave1-bin binlog_cache_size=1M binlog_format=mixed expire_logs_days=7 slave_skip_errors=1062 relay_log=com-mysql-relay-bin log_slave_updates=1 read_only=1
重启容器
1 docker restart mysql-slave
在从数据库中配置主从复制
进入容器
1 docker exec -it mysql-slave /bin/bash
进入数据库
开启复制功能
1 change master to master_host= '192.168.1.42' , master_user= 'slave' , master_password= '123456' , master_port= 3307 , master_log_file= 'com-mysql-bin.000001' , master_log_pos= 156 , master_connect_retry= 30 ;
上面的宿主机ip需要根据实际情况修改。
主从复制参数说明:
master_host:主数据库的IP地址;
master_port:主数据库的运行端口;
master_user:在主数据库创建的用于同步数据的用户账号;
master_password:在主数据库创建的用于同步数据的用户密码;
master_log_file:指定从数据库要复制数据的日志文件,通过查看主数据的状态,获取File参数;
master_log_pos:指定从数据库从哪个位置开始复制数据,通过查看主数据的状态,获取Position参数;
master_connect_retry:连接失败重试的时间间隔,单位为秒。
在从数据库中开启主从同步
查看从数据库状态
折叠代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 mysql> show slave status \G * * * * * * * * * * * * * * * * * * * * * * * * * * * 1. row * * * * * * * * * * * * * * * * * * * * * * * * * * * Slave_IO_State: Connecting to source Master_Host: 192.168 .1 .42 Master_User: slave Master_Port: 3307 Connect_Retry: 30 Master_Log_File: com- mysql- bin.000001 Read_Master_Log_Pos: 156 Relay_Log_File: com- mysql- relay- bin.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: com- mysql- bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB:
主从复制测试
主库新建库、新建表、插入数据:
1 2 3 4 5 create database db108;use db108; create table t1 (id int ,name varchar (20 ));insert into t1 values (1 ,'liuxp' );select * from t1;
从库查看库、查看记录,看主从同步是否成功:
1 2 3 show databases;use db108; select * from t1;