使用docker进行两台虚拟机Mysql的主从复制(最详细,最简单)

一、首先使用docker进行mysql镜像以及容器的创建。(两台虚拟机都要创建)

1.拉取mysql镜像

docker pull mysql:8.0.20

2.运行容器命令

#两台虚拟机都要运行
cd /home
mkdir mysql
cd mysql
mkdir conf
mkdir data

#在第一台虚拟机上运行
docker run -p 8888:3306 --name mysql-master -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:8.0.20

#在第二台虚拟机上运行
docker run -p 8889:3306 --name mysql-slave -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:8.0.20

二、对主服务器的MySQL进行设置启用二进制日志

#进入mysql容器内部
docker exec -it mysql-master /bin/bash

#查看my.cnf文件
cd /etc/mysql
cat my.cnf

接下来需要修改容器内容文件:

#退出容器,在外部创建my.cnf文件
vi my.cnf

#修改my.cnf文件:
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

#
# The MySQL  Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[mysqld]


pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-bin=mysql-bin     #[必须]启用二进制日志
server-id=100         #[必须]服务器唯一ID

# Custom config should go here
!includedir /etc/mysql/conf.d/

在my.cnf文件写入内容后,将文件复制到容器内部:

#将创建好的my.cnf文件复制到容器内
docker cp my.cnf mysql-master:/etc/mysql/

#重启mysql容器
docker restart mysql-master

三、在主服务器(第一台虚拟器)中进行创建角色并授权

#进入容器
docker exec -it mysql-master /bin/bash

#进入mysql中
mysql -uroot -p123456

#执行以下两条语句
CREATE USER 'xiaoming'@'%' IDENTIFIED BY 'wangqinmin';
GRANT REPLICATION SLAVE ON *.* TO 'xiaoming'@'%';
ALTER USER 'xiaoming'@'%' IDENTIFIED WITH 'mysql_native_password' BY 'wangqinmin';
FLUSH PRIVILEGES;

#查看主库状态
show master status;

4.对从服务器(第二台虚拟机)进行操作

#进入mysql容器内部
docker exec -it mysql-slave /bin/bash

#查看my.cnf文件
cd /etc/mysql
cat my.cnf

接下来需要修改容器内容文件:

#退出容器,在外部创建my.cnf文件
vi my.cnf

#修改my.cnf文件:
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

#
# The MySQL  Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[mysqld]


pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

server-id=101         #[必须]服务器唯一ID

# Custom config should go here
!includedir /etc/mysql/conf.d/

在my.cnf文件写入内容后,将文件复制到容器内部:

#将创建好的my.cnf文件复制到容器内
docker cp my.cnf mysql-slave:/etc/mysql/

#重启mysql容器
docker restart mysql-slave

之后,在进入容器内部进行执行:

master_host的内容是第一台虚拟机的ip地址,端口号就是你之前创建容器的端口号,如果之前和我一样,那就是8888,无需修改。

#再次进入容器
docker exec -it mysql-slave /bin/bash

#进入mysql中
mysql -uroot -p123456

#执行语句
change master to master_host= '192.168.91.1' ,master_user= 'xiaoming' ,master_password= 'wangqinmin' , master_log_file= 'mysql-bin.00001' ,master_log_pos=698,get_master_public_key=1;
CHANGE MASTER TO
  MASTER_HOST='192.168.91.1',
  MASTER_PORT=8888,
  MASTER_USER='xiaoming',
  MASTER_PASSWORD='wangqinmin',
  MASTER_LOG_FILE='mysql-bin.000001',
  MASTER_LOG_POS=1150;

#执行语句
start slave;

#查看从库状态
show slave statusG;

看到这个红色框中内容都为Yes,表示主从复制已经成功。

可以去navicat进行测试,