Docker搭建MySQL主从复制
docker搭建mysql主从复制
-
主从服务器上分别安装docker
1.1 docker 要求 centos 系统的内核版本高于 3.10
[root@localhost ~]# uname -r 3.10.0-693.el7.x86_64
1.2 确保 yum 包更新到最新。
[root@localhost ~]# sudo yum update loaded plugins: fastestmirror, langpacks loading mirror speeds from cached hostfile * base: mirrors.cqu.edu.cn * extras: mirrors.cqu.edu.cn * updates: mirrors.cqu.edu.cn base | 3.6 kb 00:00:00 docker-ce-stable | 3.5 kb 00:00:00 extras | 2.9 kb 00:00:00 updates | 2.9 kb 00:00:00
1.3 安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的
[root@localhost ~]# sudo yum install -y yum-utils device-mapper-persistent-data lvm2 loaded plugins: fastestmirror, langpacks loading mirror speeds from cached hostfile * base: mirrors.cqu.edu.cn * extras: mirrors.cqu.edu.cn * updates: mirrors.cqu.edu.cn package yum-utils-1.1.31-52.el7.noarch already installed and latest version package device-mapper-persistent-data-0.8.5-1.el7.x86_64 already installed and latest version package 7:lvm2-2.02.185-2.el7_7.2.x86_64 already installed and latest version nothing to do
1.4 设置yum源
[root@localhost ~]# sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo loaded plugins: fastestmirror, langpacks adding repo from: https://download.docker.com/linux/centos/docker-ce.repo grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo repo saved to /etc/yum.repos.d/docker-ce.repo
1.5 安装docker
[root@localhost ~]# sudo yum install docker-ce loaded plugins: fastestmirror, langpacks loading mirror speeds from cached hostfile * base: mirrors.cqu.edu.cn * extras: mirrors.cqu.edu.cn * updates: mirrors.cqu.edu.cn package 3:docker-ce-19.03.8-3.el7.x86_64 already installed and latest version nothing to do
1.6 启动docker并设置docker自动启动
[root@localhost ~]# sudo systemctl start docker [root@localhost ~]# sudo systemctl enable docker
1.7 检测docker是否安装成功
[root@localhost ~]# docker version client: docker engine - community version: 19.03.8 api version: 1.40 go version: go1.12.17 git commit: afacb8b built: wed mar 11 01:27:04 2020 os/arch: linux/amd64 experimental: false server: docker engine - community engine: version: 19.03.8 api version: 1.40 (minimum version 1.12) go version: go1.12.17 git commit: afacb8b built: wed mar 11 01:25:42 2020 os/arch: linux/amd64 experimental: false containerd: version: 1.2.13 gitcommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429 runc: version: 1.0.0-rc10 gitcommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd docker-init: version: 0.18.0 gitcommit: fec3683
-
docker更换国内阿里仓库,并下载安装mysql
2.1 docker更换国内阿里仓库
sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'eof' { "registry-mirrors": ["https://rdwyjupq.mirror.aliyuncs.com"] } eof sudo systemctl daemon-reload sudo systemctl restart docker
2.2 安装mysql
2.2.1 下载mysql
[root@localhost ~]# docker pull mysql using default tag: latest latest: pulling from library/mysql c499e6d256d6: pull complete 22c4cdf4ea75: pull complete 6ff5091a5a30: pull complete 2fd3d1af9403: pull complete 0d9d26127d1d: pull complete 54a67d4e7579: pull complete fe989230d866: pull complete 3a808704d40c: pull complete 826517d07519: pull complete 69cd125db928: pull complete b5c43b8c2879: pull complete 1811572b5ea5: pull complete digest: sha256:b69d0b62d02ee1eba8c7aeb32eba1bb678b6cfa4ccfb211a5d7931c7755dc4a8 status: downloaded newer image for mysql:latest docker.io/library/mysql:latest
2.2.2 查看mysql镜像
[root@localhost ~]# docker images repository tag image id created size mysql latest 9228ee8bac7a 4 days ago 547mb
2.2.3 创建mysql的挂载文件目录(日志、数据、配置)
[root@localhost ~]# mkdir -p /root/mysql/data /root/mysql/logs /root/mysql/conf
2.2.4 先启动容器(为了复制配置文件)(这种做法显得多余,但是docker不是很精通的我没想到更好的办法,请各位大神指导)
[root@localhost conf]# docker run -d -p 3306:3306 --name mysql -e mysql_root_password="123456" mysql 05d161e69f3b2e3e4ebaa50f822934599a31e7ade330e1440a68aec9e404f7ae
2.2.5 复制配置文件
[root@localhost conf]# docker cp mysql:/etc/mysql/my.cnf /root/mysql/conf/
2.2.6 停止并删容器
[root@localhost conf]# docker stop mysql mysql [root@localhost conf]# docker rm mysql mysql
2.2.7 重新启动容器(加上挂载)
[root@localhost conf]# docker run --name mysql -p 3306:3306 -e mysql_root_password=123456 --mount type=bind,src=/root/mysql/conf/my.cnf,dst=/etc/mysql/my.cnf --mount ty pe=bind,src=/root/mysql/data,dst=/var/lib/mysql --mount type=bind,src=/root/mysql/logs,dst=/logs --restart=on-failure:3 -d mysql64824c7e84ceb3513b65a375c55f2c53bc653f95b119008296122b13ff632ba1
2.2.8 进入mysql查询现有字符集,结果都是utf8mb4
show variables like '%char%';
2.2.9 在挂载的配置文件更改字符集
character-set-server=utf8 default-character-set=utf8 default-character-set=utf8
2.2.10 重启docker容器,并查看字符集已经更改,证明挂载的配置文件有效
-
搭建主从复制数据库
3.1 配置主master库
3.1.1 进入主库挂载配置文件my.cnf,加入以下配置并重启docker容器
[mysqld] ## 同一局域网内注意要唯一 server-id=100 ## 开启二进制日志功能,可以随便取(关键) log-bin=mysql-bin
3.1.2 在主master库执行语句
create user 'slave'@'%' identified by '123456'; grant replication slave, replication client on *.* to 'slave'@'%';
为防止后面出现报错[authentication plugin 'caching_sha2_password' cannot be loaded],执行下面语句
alter user 'slave'@'%' identified by 'password' password expire never; #修改加密规则 alter user 'slave'@'%' identified with mysql_native_password by 'password'; #更新一下用户的密码 flush privileges; alter user 'slave'@'%' identified by '123456';
最后执行语句获取file, position
show master status;
3.2 配置从slave库
3.2.1 进入从库挂载配置文件my.cnf,加入以下配置并重启docker容器
[mysqld] ## 设置server_id,注意要唯一 server-id=101 ## 开启二进制日志功能,以备slave作为其它slave的master时使用 log-bin=mysql-slave-bin ## relay_log配置中继日志 relay_log=edu-mysql-relay-bin
3.2.2 链接主从库,在从库执行语句
change master to master_host='192.168.47.128', master_user='slave', master_password='123456', master_port=3306, master_log_file='mysql-bin.000001', master_log_pos= 5938, master_connect_retry=30;
master_host :master的地址
master_port:master的端口号,指的是容器的端口号
master_user:用于数据同步的用户
master_password:用于同步的用户的密码
master_log_file:指定 slave 从哪个日志文件开始复制数据,即上文中提到的 file 字段的值
master_log_pos:从哪个 position 开始读,即上文中提到的 position 字段的值
master_connect_retry:如果连接失败,重试的时间间隔,单位是秒,默认是60秒
3.2.3 查看主从同步状态
show slave status ;
正常情况下,slaveiorunning 和 slavesqlrunning 都是no,因为我们还没有开启主从复制过程。
3.2.4 开启主从复制
start slave;
重新查看主从同步状态, 这时slaveiorunning 和 slavesqlrunning 都是yes,如果你的slaveiorunning 和 slavesqlrunning 其中一个是connecting或者no,就证明配置有错,查看last_io_error报错的详细信息
通过下面命令,删除已经配置的主从链接信息,重新进行配置
stop slave; reset master;
3.2.5 测试主从复制,最简单的方式是在主库建立个新库,这时,从库会自动同步
上一篇: 5.ASP.NET Core 项目文件
下一篇: java面试基础篇-List