Docker搭建MySQL主从复制

2019-06-13  本文已影响0人  糊涂蟲

环境准备

OS:CentOS7.5
Docker:18.06.1-ce
MySQL镜像:5.7
在服务器上安装docker,运行两个MySQL容器,实现主从复制

安装Docker

详见Docker部署

安装MySQL

拉取镜像

[root@doc ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
fc7181108d40: Pull complete 
787a24c80112: Pull complete 
a08cb039d3cd: Pull complete 
4f7d35eb5394: Pull complete 
5aa21f895d95: Pull complete 
a742e211b7a2: Pull complete 
0163805ad937: Pull complete 
62d0ebcbfc71: Pull complete 
559856d01c93: Downloading  45.67MB/83.75MB
c849d5f46e83: Download complete 
f114c210789a: Download complete 

运行master节点

[root@doc ~]# docker run -d --name master -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -v /root/data/mysql/master:/var/lib/mysql mysql:5.7
fea899e5c07c39ea251abe97ceb21aac71a138c8a4009bc1170eff9ed5df8b79
[root@doc ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
fea899e5c07c        mysql:5.7           "docker-entrypoint.s…"   4 seconds ago       Up 3 seconds        0.0.0.0:3306->3306/tcp, 33060/tcp   master

使用navicat连接,创建主容器的复制账号

GRANT REPLICATION SLAVE ON *.* to 'backup'@'%' identified by 'backup';
show grants for 'backup'@'%';

拷贝容器的配置文件到宿主机

[root@doc ~]# docker cp master:/etc/mysql/mysql.conf.d/mysqld.cnf /root/conf/mysql/master/

在配置文件[mysqld]节点最后添加

log-bin=mysql-bin
server-id=1
# log-bin=mysql-bin 使用binary logging,mysql-bin是log文件名的前缀
# server-id=1 唯一服务器ID,非0整数,不能和其他服务器的server-id重复

将修改后的文件覆盖docker中master的配置文件,并重启生效

[root@doc master]# docker cp /root/conf/mysql/master/mysqld.cnf master:/etc/mysql/mysql.conf.d/mysqld.cnf
[root@doc master]# docker restart master
master
[root@doc master]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
c852713a7701        mysql:5.7           "docker-entrypoint.s…"   About an hour ago   Up 2 seconds        0.0.0.0:3306->3306/tcp, 33060/tcp   master

运行slave节点

[root@doc ~]# docker run -d --name slave -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 -v /root/data/mysql/slave:/var/lib/mysql mysql:5.7
e1f4cd6cdf4b091cd730c800d2802632b708f6a7aedc35af185d5f0e8ae408da
[root@doc ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
e1f4cd6cdf4b        mysql:5.7           "docker-entrypoint.s…"   4 seconds ago       Up 3 seconds        33060/tcp, 0.0.0.0:3307->3306/tcp   slave
c852713a7701        mysql:5.7           "docker-entrypoint.s…"   About an hour ago   Up 8 minutes        0.0.0.0:3306->3306/tcp, 33060/tcp   master
[root@doc ~]# docker cp slave:/etc/mysql/mysql.conf.d/mysqld.cnf /root/conf/mysql/slave/
[root@doc slave]# vi mysqld.cnf 
最后添加
log-bin=mysql-bin
server-id=2
覆盖到docker中的slave容器
[root@doc ~]# docker cp /root/conf/mysql/slave/mysqld.cnf slave:/etc/mysql/mysql.conf.d/mysqld.cnf
[root@doc ~]# docker restart slave
slave
[root@doc ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
e1f4cd6cdf4b        mysql:5.7           "docker-entrypoint.s…"   13 minutes ago      Up 3 seconds        33060/tcp, 0.0.0.0:3307->3306/tcp   slave
c852713a7701        mysql:5.7           "docker-entrypoint.s…"   About an hour ago   Up 21 minutes       0.0.0.0:3306->3306/tcp, 33060/tcp   master
CHANGE MASTER TO 
MASTER_HOST='39.100.43.154',  #填Navicat连接配置中的ip应该就可以
MASTER_PORT=3306, #master容器端口
MASTER_USER='backup',  #同步账号的用户名
MASTER_PASSWORD='backup';  #同步账号的密码
START SLAVE;

检查配置是否成功

show slave status;
image.png

看到上图所示,表示主从复制已成功,测试功能
现在master容器新建数据库


image.png

然后到slave容器中刷新下,即可看到新建的库


image.png
上一篇下一篇

猜你喜欢

热点阅读