Docker 部署 MySQL 主从

资源清单

主机 IP
master <部署mysql-master> 10.0.0.1
slave <部署mysql-slave> 10.0.0.2
软件 版本
Docker 20.10.12
MySQL镜像 5.7.35

一、Docker 安装

1. 使用国内 yum

# yum install -y yum-utils device-mapper-persistent-data lvm2
# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

2. 卸载旧版本的 docker

## 如果主机上已经有docker存在且不是想要安装的版本,需要先进行卸载。
# yum remove -y docker \
              docker-client \
              docker-client-latest \
              docker-common \
              docker-latest \
              docker-latest-logrotate \
              docker-logrotate \
              docker-selinux \
              docker-engine-selinux \
              docker-engine \
              container*

3. 安装 Docker20.10 版本

# yum -y install docker-ce-20.10.12-3.el7 docker-ce-cli-20.10.12-3.el7

4. 设置镜像加速

# mkdir /etc/docker
# vi /etc/docker/daemon.json

{
  "registry-mirrors": ["https://xxxxxxxxx.mirror.aliyuncs.com"]
}

5. 启动 docker

# systemctl start docker
# systemctl enable docker
# systemctl status docker

二、MySQL部署

主节点部署

1. 新建目录

# mkdir /data/mysql/{conf,data} -pv

2. 启动 MySQL 服务

# docker run -d \
--name mysql-master \
-p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=root@1234 \
mysql:5.7.35

3. 复制容器内配置文件,持久化存储配置

# docker cp mysql-master:/etc/mysql/mysql.conf.d/mysqld.cnf /data/mysql/conf/

4. 添加主从配置等信息

# vim /data/mysql/conf/mysqld.cnf

# Copyright (c) 2014, 2021, Oracle and/or its affiliates.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation.  The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# 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, version 2.0, 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
log-error       = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address   = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# <------- 新增配置 ------->
log-bin=/var/lib/mysql/mysql-bin
server-id=1

character-set-server=utf8mb4
slow-query-log-file=/logs/slow-query.log
long_query_time=1

5. 删除容器,重启服务,设置配置和数据持久化

# docker run -d \
--name mysql-master \
-p 3306:3306 \
-v /data/mysql/conf:/etc/mysql/mysql.conf.d \
-v /data/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=root@1234 \
mysql:5.7.35

6. 新增主从同步账户

## 进入容器链接上mysql并新增test用户用于从库同步
# docker exec -it mysql-master bash

# mysql -uroot -proot@1234

## mysql 命令
## 链接主库配置
## 配置test用户和权限(密码也为test)
> grant replication slave on *.* to 'test'@'%' identified by 'test';
> flush privileges;

## 查看主从状态
## 记住File和Position的值,在从库配置中会使用。
> show master status\G;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 |     4969 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

从节点部署

1. 新建目录

# mkdir /data/mysql/{conf,data} -pv

2. 启动 MySQL 服务

## 需要挂载 -v /data/mysql/data:/var/lib/mysql 把当前初始化好的数据库mysql库等信息同步到宿主机磁盘

# docker run -d \
--name mysql-slave \
-p 3306:3306 \
-v /data/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=root@1234 \
mysql:5.7.35

3. 使用主服务器 mysqld.conf 配置文件并修改为从节点配置

# vim /data/mysql/conf/mysqld.cnf

# <------- 新增配置 ------->
log-bin=mysql-slave-bin
relay_log=mysql-relay-bin
server-id=2

character-set-server=utf8mb4
slow-query-log-file=/logs/slow-query.log
long_query_time=1

4. 删除容器,重启服务,设置配置和数据持久化

# docker run -d \
--name mysql-slave \
-p 3306:3306 \
-v /data/mysql/conf:/etc/mysql/mysql.conf.d \
-v /data/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=root@1234 \
mysql:5.7.35

5. 配置主从同步

## 进入容器并链接主库
# docker exec mysql-slave -it /bin/bash

# mysql -uroot -proot@1234

## mysql 命令
## 链接主库配置
> change master to master_host='10.0.0.1', master_user='test', master_password='test', master_port=3306, master_log_file='mysql-bin.000005', master_log_pos=4969, master_connect_retry=30;

## 启动从库
> start slave;

## 查看从库状态
> show slave status\G;
posted @ 2022-04-28 16:43  evescn  阅读(250)  评论(0编辑  收藏  举报