1.拉取mysql镜像
docker pull mysql:5.7
2.创建测试mysql容器
docker run -p 3307:3306 --name mysql-test -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
3.创建本地文件夹
docker cp mysql-test:/etc/mysql/mysql.cnf /mydir/mysql-master/cnf
4.删除测试容器
docker stop mysql-test
docker rm mysql-test
5.创建master节点mysql
docker run -p 3306:3306 --name mysql-master -v /mydir/mysql-master/data:/var/lib/mysql -v /mydir/mysql-master/conf/mysql.cnf:/etc/mysql/mysql.cnf -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
6.修改本地挂载配置文件
vim /mydir/mysql-master/cnf/mysql.cnf
[mysqld]
server-id=1
log-bin=mysql-bin
binlog-do-db=master
binlog-ignore-db=mysql
7.重启MySQL
docker restart mysql-master
8.使用连接工具连接master
SHOW MASTER STATUS;
9.创建从库连接的用户
CREATE USER 'admin'@'%' IDENTIFIED BY '123456'; #创建用户
GRANT ALL ON *.* TO 'admin'@'%';
10.创建从库数据库
docker run -p 3307:3306 --name mysql-slave1 -v /mydir/mysql-slave1 /data:/var/lib/mysql -v /mydir/mysql-slave1 /conf/mysql.cnf:/etc/mysql/mysql.cnf -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
11.修改从库配置
vim /mydir/mysql-slave1/cnf/mysql.cnf
[mysqld]
server-id=2
12.连接输入命令
STOP SLAVE;
CHANGE MASTER TO master_host = 'ip地址',
master_port=端口号,
master_user = '创建的主库用户名',
master_password = '创建的主库密码',
master_log_file = 'mysql-bin.000001',
master_log_pos = 154;
START SLAVE;
show slave status;
这两个为Yes 就成功了
多个从库只需要重复操作就可以了