0
点赞
收藏
分享

微信扫一扫

docker部署mysql主从同步

念川LNSC 2023-06-30 阅读 62

1. 规划

主机

ip

master

10.0.0.1

slave

10.0.0.1

2. 查看镜像,下载镜像,导入镜像的命令

docker  images

docker pull mysql:5.7

docker load -i mysql.tar

3. 启动镜像服务

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

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

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

5. 添加主从配置的相关文件

# 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

log_slave_updates

6. 删除容器,重启服务,设置配置和数据持久化,删除原有的mysql服务

docker stop $(docker ps -a | awk '{print $1}')

docker rm  $(docker ps -a | awk '{print $1}')

 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.17

7. 新增主从同步账户

## 进入容器链接上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)


8.从节点部署

新建目录:

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

9.启动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.17

10.使用主服务器 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

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

# 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.17


12.配置主从同步

## 进入容器并链接主库

 # 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;


出现IQ线程SQL无报错就可以。


新增的服务器重复执行,第8 9 10 11 12步的操作就可以。


举报

相关推荐

0 条评论