1.建立数据库master
docker run --name master --restart always -p 3308:3306 -v /root/docker/volumes/etc/master:/etc/mysql -v /root/docker/volumes/var/lib/master:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=riCXT8zM -d mysql:latest
2.复制master的配置文件到docker
docker cp master:/etc/my.cnf /root/docker/volumes/etc/master/my.cnf
3.卸载并安装master
docker stop master
docker rm master
docker run --name master -p 3308:3306 -v /root/docker/volumes/etc/master/my.cnf:/etc/my.cnf -v /root/docker/volumes/etc/master/my.cnf.d:/etc/mysql/my.cnf.d -v /root/docker/volumes/etc/master/conf.d:/etc/mysql/conf.d -v /root/docker/volumes/var/lib/master:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=riCXT8zM -d mysql:latest
1.建立数据库slave01
docker run --name slave01 --restart always -p 3309:3306 -v /root/docker/volumes/etc/slave01:/etc/mysql -v /root/docker/volumes/var/lib/slave01:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=riDYQ8zM -d mysql:latest
2.复制slave01的配置文件到容器
docker cp slave01:/etc/my.cnf /root/docker/volumes/etc/slave01/my.cnf
3.卸载并安装slave01
docker stop slave01
docker rm slave01
docker run --name slave01 -p 3309:3306 -v /root/docker/volumes/etc/slave01/my.cnf:/etc/my.cnf -v /root/docker/volumes/etc/slave01/my.cnf.d:/etc/mysql/my.cnf.d -v /root/docker/volumes/etc/slave01/conf.d:/etc/mysql/conf.d -v /root/docker/volumes/var/lib/slave01:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=riDYQ8zM -d mysql:latest
建立从机slave02与slave01类似!!!
1.建立数据库slave02
docker run --name slave02 --restart always -p 3310:3306 -v /root/docker/volumes/etc/slave02:/etc/mysql -v /root/docker/volumes/var/lib/slave02:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=riEZG8zM -d mysql:latest
2.复制slave02的配置文件到容器
docker cp slave02:/etc/my.cnf /root/docker/volumes/etc/slave02/my.cnf
3.卸载并安装slave01
docekr stop slave02 #停止容器slave02
docker rm slave02 #移除容器slave02
docker run --name slave02 -p 3310:3306 -v /root/docker/volumes/etc/slave02/my.cnf:/etc/my.cnf -v /root/docker/volumes/etc/slave02/my.cnf.d:/etc/mysql/my.cnf.d -v /root/docker/volumes/etc/slave02/conf.d:/etc/mysql/conf.d -v /root/docker/volumes/var/lib/slave02:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=riEZG8zM -d mysql:latest
1.在三个数据库分别执行
show variables like 'server_id';
可以看到都是以下一样的结果:
2.配置my.cnf配置文件
主机master配置日志
# start~
datadir=/var/lib/mysql
# 是否开启mysql日志 0:关闭(默认值) 1:开启
general-log=1
# mysql 日志的存放位置
general_log_file="/var/lib/mysql/logs/master.log"
slow-query-log=1
slow_query_log_file="/var/lib/mysql/logs/master-slow.log"
long_query_time=10
# Error Logging.
log-error="/var/lib/mysql/logs/master.err"
# end~
slave01:
server_id=2
slave02:
server_id=3
执行完后执行在服务器终端执行以下代码:
docker restart slave01
docker restart slave02
mysql命令行
#重新查一下server_id
show variables like 'server_id';
slave01:
docker exec -it slave01 /bin/bash
cat /etc/my.cnf
sed -i '$a server_id=2' /etc/my.cnf
docker restart slave01
mysql> show variables like 'server_id';
slave02:
docker exec -it slave01 /bin/bash
cat /etc/my.cnf
sed -i '$a server_id=2' /etc/my.cnf
docker restart slave01
mysql> show variables like 'server_id';
mysql命令行:
#重新查一下server_id
show variables like 'server_id';
如果server_id分别为1,2,3,则成功!!!
3.先查看主机master数据库的日志
mysql> show master status;
4.在从机slave01,slave02分别在sql命令行执行以下代码
change master to
master_port=3308,(主机端口号)
master_user='root',
master_password='riCXT8zM',(主机密码)
master_host='172.20.16.116',(填自己ip)
master_log_file='binlog.000003',(按照日志填)
master_log_pos=157;(按照日志填)
5.开启主从服务(slave01,slave02的终端执行)
mysql> start slave;
查看是否配置成功
mysql> show slave status \G;
由于在navicat上执行可视化不怎么好看,难以看出什么东西,我们从服务器进入MySQL进行输入
拓展补充:
执行show slave status \G;显示以下:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
则成功
MySQL主从复制(一主两从)读写分离架构 安全规范 重启步骤:
Linux> docker stop slave01;
Linux> docker stop salve02;
Linux> docker stop master;
Linux> docker start master;
Linux> docker start slave01;
Linux> docker start slave02;
进入容器后台与数据终端的基本命令:
Linux> docker exec -it slave01 bash
Linux> docker exec -it slave01 /bin/bash
mysql> mysql -uroot(用户) -priDYQ8zM(密码)
Linux> docker exec -it slave02 /bin/bash
mysql> mysql -uroot(用户) -priEZG8zM(密码)