首页 > 数据库 >docker部署mysql主从同步

docker部署mysql主从同步

时间:2023-06-30 12:03:42浏览次数:47  
标签:log ## master mysql docker data 主从

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步的操作就可以。


标签:log,##,master,mysql,docker,data,主从
From: https://blog.51cto.com/u_14529165/6589551

相关文章

  • docker部署mongodb副本集
    1.规格ip地址端口192.168.1.10127017192.168.1.10227017192.168.1.103270172.部署操作2.1 以下操作分别在三台不同的ECS服务器上操作[root@ecs-ff50-73e8-0002~]#mkdir/path/to-p[root@ecs-ff50-73e8-0002~]#cd/path/to/[root@ecs-ff50-73e8-0002to]#ls[root@ecs-ff50-73......
  • Docker部署redis集群
    1.规划ip地址端口数据存储位置192.168.103.587000/var/lib/redis/70007003/var/lib/redis/7003192.168.103.417001/var/lib/redis/70017004/var/lib/redis/7004192.168.103.1247002/var/lib/redis/70027005/var/lib/redis/70052.创建数据存储目录,及配置文件,导入镜像文件2.1创建好用......
  • Dockerfile构建镜像
    https://blog.csdn.net/qq_69278945/article/details/127461465先编写镜像构建文件DockerfilevimDockerfileFROMnginx:1.20RUNecho'开心活泼的果冻'>/usr/share/nginx/html/index.html构建命令dockerbuild-tmy_nginx.build:构建命令-t:指定镜像名my_nginx:镜......
  • Docker-环境安装
    安装yum-utils:yuminstall-yyum-utilsdevice-mapper-persistent-datalvm2  为yum源添加docker仓库位置: yum-config-manager--add-repohttps://download.docker.com/linux/centos/docker-ce.repo   安装docker: yuminstalldocker-ce......
  • MySQL内存使用率高且不释放问题排查与总结
    一、内存使用率高且不释放问题排查生产环境MySQL5.7数据库告警内存使用率95%。排查MySQL内存占用问题的思路方法可以参考叶老师这篇文章:https://mp.weixin.qq.com/s/VneUUnprxzRGAyQNaKi-7g。TOP命令查看MySQL进程的RES指标,发现内存使用了10.6G,而数据库的innodb_buffer_pool_si......
  • docker
    https://blog.csdn.net/weixin_42249893/article/details/122598512?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-5-122598512-blog-108204378.235^v38^pc_relevant_sort_base1&spm=1001.2101.3001.4242.4&utm_rel......
  • gitlab-docker部署最终版
    下载gitlab镜像dockerpullgitlab/gitlab-ce:latest 运行mkdirgitlabgitlab/etcgitlab/loggitlab/optdockerrun-id-p3000:80-p9922:22-v/root/gitlab/etc:/etc/gitlab-v/root/gitlab/log:/var/log/gitlab-v/root/gitlab/opt:/var/opt/gitlab--restart......
  • MySQL-增删改语句
    数据库增删改语句原创 Lyle_Tu Linux分布式主任 2023-06-2622:28 发表于福建收录于合集#数据库7个#语句2个#sql5个#服务器15个 插入数据使用INSERT语句可向指定表中插入数据。INSERT语法的基本结构如下:INSERTINTO<table_name>(column_name1,column......
  • mysql-alter语句
    T-SQLAlter语句原创 Lyle_Tu Linux分布式主任 2023-06-2008:21 发表于福建收录于合集#sql5个#linux13个#数据库7个#服务器15个 ALTERTABLE语句用于在已有的表中添加、修改或删除列。 修改数据库常用语句语法格式:ALTERDATABASEdatabase_name{AD......
  • 12 | 为什么我的MySQL会“抖”一下?
    一下内容出自《MySQL实战45讲》12|为什么我的MySQL会“抖”一下?一条SQL语句,正常执行的时候特别快,但是有时也不知道怎么回事,它就会变得特别慢,并且这样的场景很难复现,它不只随机,而且持续时间还很短。看上去,这就像是数据库“抖”了一下。这个时候,MySQL可能是在刷脏页(fl......