时间:2021-07-01 10:21:17 帮助过:5人阅读
// 创建目录,数据和配置不至于丢失 mkdir -p /usr/local/docker_app/mysql/master/conf.d/ mkdir -p /usr/local/docker_app/mysql/slave/conf.d/
/usr/local/docker_app/mysql/master/conf.d/my.cnf
vi /usr/local/docker_app/mysql/master/conf.d/my.cnf
修改文件配置
[mysqld] log-bin=master-bin server-id=1
/usr/local/docker_app/mysql/slave/conf.d/my.cnf
vi /usr/local/docker_app/mysql/slave/conf.d/my.cnf
修改文件配置
[mysqld] log-bin=slave-bin server-id=2
5.启动master服务
docker run -d --name mysql-master -p 3306:3306 -v /usr/local/docker_app/mysql/master/conf.d/data:/var/lib/mysql -v /usr/local/docker_app/mysql/master/conf.d:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
docker run -d --name mysql-slave -p 3307:3306 -v /usr/local/docker_app/mysql/slave/conf.d/data:/var/lib/mysql -v /usr/local/docker_app/mysql/slave/conf.d:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
# 进入容器shell
$ docker exec -it mysql-master /bin/sh
mysql -uroot -p // mysql shell // 用户名:root // 密码:root GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘%‘ IDENTIFIED BY ‘root‘ WITH GRANT OPTION;
进入容器修改slave的读写权限
# 进入容器shell
$ docker exec -it mysql-slave/bin/sh
//进入mysql mysql -uroot -p // mysql shell // 用户名:root // 密码:root GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘%‘ IDENTIFIED BY ‘root‘ WITH GRANT OPTION;8.进入mysql-master查看master状态
// mysql shell show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | master-bin.000003 | 154 | | | | +------------------+----------+--------------+------------------+-------------------+
docker inspect --format=‘{{.NetworkSettings.IPAddress}}‘ mysql-master
// mysql shell change master to master_host=‘172.17.0.2‘, master_user=‘root‘,master_password=‘root‘, master_log_file=‘master-bin.000003‘,master_log_pos=154;
// mysql shell // 启用slave start slave;
查看是否成功
show slave status \G;
以下两个都为yes就代表成功了
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
//说明主从成功
show databases;
create database test;
docker搭建mysql读写分离
标签:ofo doc 容器 实例 mic 使用 setting use ipa