当前位置: 首页 > news >正文

Docker 搭建MySQL主从复制-读写分离

一. 介绍

MySQL主从复制是一种常用的数据库高可用性解决方案,通过在主数据库上记录的数据变更,同步到一个或多个从数据库,实现数据的冗余备份和读写分离。在Docker环境下搭建MySQL主从复制和读写分离,不仅方便管理,还能充分发挥Docker的轻量、可移植性等特性。

二. 准备工作

在开始搭建之前,请确保你的系统已经安装好Docker和Docker Compose

三. 步骤

1. Docker安装三台mysql服务器

  • 一主二从,mysql1是主,mysql2和mysql3为从
# 安装第一台MySQL
docker run -d -e MYSQL_ROOT_PASSWORD=123456 -p 3301:3306 --name=mysql1  mysql:5.6# 安装第二台MySQL
docker run -d -e MYSQL_ROOT_PASSWORD=123456 -p 3302:3306  --name=mysql2  mysql:5.6# 安装第三台MySQL
docker run -d -e MYSQL_ROOT_PASSWORD=123456 -p 3303:3306  --name=mysql3  mysql:5.6

2. 修改三台容器配置文件(/etc/mysql/mysql.conf.d/mysqld.cnf)

  • mysql1配置文件
# Copyright (c) 2014, 2016, 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, 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
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0# 加入下方两行配置
server-id=1   #任意自然数n,只要保证每台MySQL主机不重复就可以了。
log-bin=mysql-bin   #开启二进制日志
  • myslq2配置文件
# Copyright (c) 2014, 2016, 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, 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
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0# 加入下方两行配置
server-id=2   #任意自然数n,只要保证每台MySQL主机不重复就可以了。
log-bin=mysql-bin   #开启二进制日志
  • mysql3配置文件
# Copyright (c) 2014, 2016, 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, 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
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0# 加入下方两行配置
server-id=3   #任意自然数n,只要保证每台MySQL主机不重复就可以了。
log-bin=mysql-bin   #开启二进制日志

3. 重启MySQL容器

docker restart mysql1
docker restart mysql2
docker restart mysql3

4. 配置主库

docker exec -it mysql1 /bin/bash
mysql -uroot -p123456
# 查看主库配置是否生效
SHOW VARIABLES LIKE 'server_id';

5. 为从库创建同步账户

  • root 为创建的同步用户的用户名
  • 123456为同步用户的密码
GRANT REPLICATION CLIENT,REPLICATION SLAVE ON *.* TO root@'%' IDENTIFIED BY '123456'; 
  •  验证

mysql> use mysql;

Database changed
mysql> select user,host,password from user;  
+------+-----------+-------------------------------------------+
| user | host      | password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | %         | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+------+-----------+-------------------------------------------+

6. 修改从库数据 

  • 进入从库
docker exec -it mysql2 /bin/bashmysql -uroot -p123456
  •  查看主库ip
docker inspect mysql1
{''''''"Gateway": "172.17.0.1","GlobalIPv6Address": "","GlobalIPv6PrefixLen": 0,"IPAddress": "172.17.0.6",  # 此为主库ip--设置同步所用"IPPrefixLen": 16,"IPv6Gateway": "",''''''
}
  •  查看主库同步状态
# 在主库中输入如下查看
mysql> show master status\G;
*************************** 1. row ***************************File: mysql-bin.000001  # 此为日志文件名--设置同步所用Position: 338  # 此为同步位置--设置同步所用
  • 在两个从库执行如下代码 
CHANGE MASTER TO MASTER_HOST='172.17.0.6',
MASTER_PORT=3306, 
MASTER_USER='root', 
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000001', 
MASTER_LOG_POS=338;         # Query OK, 0 rows affected, 2 warnings (0.02 sec)# CHANGE MASTER TO MASTER_HOST='172.17.0.6', #主库IP
# MASTER_PORT=3306,   #主服务器端口
# MASTER_USER='user', #主服务器用户名
# MASTER_PASSWORD='123456', #主服务器用户密码
# MASTER_LOG_FILE='mysql-bin.000001', #日志文件名,获取方法往上看
# MASTER_LOG_POS=338;   #同步位置,获取方式往上看
  •  启动从库同步
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
  • 检测同步状态 
mysql> show  slave status\G
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 172.17.0.6Master_User: rootMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000001Read_Master_Log_Pos: 338Relay_Log_File: mysqld-relay-bin.000002Relay_Log_Pos: 283Relay_Master_Log_File: mysql-bin.000001Slave_IO_Running: YesSlave_SQL_Running: Yes  Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 338Relay_Log_Space: 457Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1Master_UUID: ce3fdd70-be78-11ee-978e-0242ac110006Master_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update itMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0
1 row in set (0.00 sec)

 如果Slave_IO_Running不为Yes

请检查 MASTER_LOG_FILE 的值是否正确,就是mysql-bin.000001338这两个数据

  •  修改示例如下
stop slave;
Query OK, 0 rows affected (0.00 sec)mysql> CHANGE MASTER TO MASTER_HOST='172.17.0.6',-> MASTER_PORT=3306, -> MASTER_USER='root', -> MASTER_PASSWORD='123456',-> MASTER_LOG_FILE='mysql-bin.000001', -> MASTER_LOG_POS=338;         
Query OK, 0 rows affected, 2 warnings (0.01 sec)mysql> start alve;

 7. 检测是否完成

在主库上创建数据库及表

create database test_mysql charset=utf8;
use test_mysql;
create table user(id int primary key auto_increment);

此时从库出现数据库和数据表 

四. 注意事项

  • 配置文件中的密码、端口、数据库名称等信息,请根据实际情况进行修改。
  • 定期备份数据库以保证数据的安全性。
  • 注意MySQL版本的兼容性。

五. 总结

通过Docker搭建MySQL主从复制和读写分离,不仅简化了部署过程,还提高了系统的可维护性。合理配置主从关系和读写分离,可以优化数据库性能,提高系统的稳定性和可用性。

相关文章:

Docker 搭建MySQL主从复制-读写分离

一. 介绍 MySQL主从复制是一种常用的数据库高可用性解决方案,通过在主数据库上记录的数据变更,同步到一个或多个从数据库,实现数据的冗余备份和读写分离。在Docker环境下搭建MySQL主从复制和读写分离,不仅方便管理,还…...

[linux] which和find有什么区别?

which 和 find 都是 Unix/Linux 系统中的命令,但它们的用途和工作方式有很大的不同。 which 命令:which 命令是用来查找并显示用户可以在当前环境下执行的命令的完整路径。这些命令通常位于 PATH 环境变量中指定的目录中。例如,which python …...

使用Neo4j做技术血缘管理

目录 一、neo4j介绍 二、windows安装启动neo4j 2.1下载neo4j 2.2 解压文件 2.3 启动neo4j 三、neo4j基础操作 3.1 创建结点和关系 3.2 查询 3.3 更改 3.4 删除 四、技术血缘Demo实现 4.1 构建节点对象 4.2 构建存储对象 4.3 创建有属性关联关系 4.4 最后是图结果…...

Unity-WebGL

问题:提示gzip压缩报错解决:关闭打包的地方压缩,如下图问题:窗口未全屏解决:使用百分比画布替换固定尺寸画布 参考:新版Unity打包Webgl端进行屏幕自适应_unity webgl分辨率自适应-CSDN博客问题:…...

腾讯云部署vue+node项目

文章目录 一、安装宝塔二、vue项目部署三、node项目部署 前言: 关于项目部署,一开始也是找了很多资料,费了点时间,所以记录一下。希望能对各位有所帮助。 一、安装宝塔 1.首先在控制台,进入云服务器的终端界面 2.输入命令和密码获取权限,并且安装宝塔界面 yum install -y w…...

HBase表结构

HBase是非关系型数据库,是高可靠性、高性能、面向列、可伸缩、实时读写的分布式数据库。 HBase使用场景 大规模数据存储:如日志记录、数据库备份等。实时数据访问:如实时搜索、实时分析等。高性能读写:如高并发、低延迟的读写操…...

本人面试积累面试题更新中

本人面试积累面试题 1.事务的隔离级别 答:2024年1月30日 1.读已提交-----读取其他事务已经提交的数据 2.读未提交-----读取其他事务还未提交的数据–可能出现脏读 3.可重复读-----同一个事务多次读取同一个数据,尽可能的保证数据的一致性但是可能出现幻读 4.串行读------确保每…...

[经典面试题]169. 多数元素

题目描述 给定一个大小为 n 的数组 nums ,返回其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。 你可以假设数组是非空的,并且给定的数组总是存在多数元素。 示例 1: 输入:nums [3,2,3] 输出:3…...

Wireshark网络协议分析 - TCP协议

在我的博客阅读本文 文章目录 1. 基础2. 实战2.1. 用Go写一个简单的TCP服务器与客户端2.2. Wireshark抓包分析2.3. 限制数据包的大小——MSS与MTU2.4. 保证TCP的有序传输——Seq,Len与Ack2.5. TCP头标志位——URG,ACK,PSH,RST&…...

3 款最好的电脑硬盘数据迁移软件

您将从本页了解 3 款最好的 SSD硬盘数据迁移软件,磁盘供应商提供的软件和可靠的第三方软件。仔细阅读本文并做出您的选择。 什么是数据迁移? 数据迁移是将数据移动到其他计算机或存储设备的过程。在日常工作活动中,常见的数据迁移有三种&…...

【Java之HTML】

HTML 概念 互联网的产生:w3c的成立, ​ 互联网最开始设计的目的:看论文 ---->浏览器,HTML ​ 网络三要素:HTML HTTP URL HTML描述论文的格式 HTTP标记这个论文在网络上怎么传输 URL:指示这个论文在互联网的哪…...

支付宝支付功能解析,从零到掌握,轻松享受便捷支付

目录 一、支付宝支付功能简介 1.1 支付宝支付的概念 1.2 支付宝支付的优势 1.3 支付宝支付的适用场景 二、支付宝支付的准备工作 三、支付宝支付的接入流程 四、支付宝支付的安全性 5.1 支付宝支付的安全机制 5.2 防范支付风险的措施 5.3 支付宝支付的安全技术保障 …...

MacOS安装反编译工具JD-GUI以及解决无法打开的问题

目录 一.下载地址 二.安装 三.问题 四.解决办法 1.显示包内容 2.找到Contents/MacOS/universalJavaApplicationStub.sh 3.修改sh文件 4.保存后再次打开即可 一.下载地址 Java Decompiler 二.安装 将下载下来的 jd-gui-osx-1.6.6.tar 解压,然后将 JD-GUI.a…...

SpringBoot将第三方的jar中的bean对象自动注入到ioc容器中

新建一个模块,做自动配置 config:需要准备两个类,一个自动配置类,一个配置类 CommonAutoConfig:此类用于做自动配置类它会去读取resoutces下的META-INF.spring下的org.springframework.boot.autoconfigure.AutoConfig…...

5.变量的解构赋值 - JS

什么是解构赋值 通过类似(或相同)的构型,将已知数据的元素/属性解构并提取出来,再赋值到相应变量,可以是新建的变量,也可以是已存在的变量/属性等;最常见的是数组和对象的解构赋值,…...

tableau添加形状

目录 1.效果:1.自带的形状:2.添加形状:小结: 1.效果: 1.自带的形状: 2.添加形状: 找到tableau的安装目录,点入 默认->形状 的文件夹: 新建一个文件夹: …...

(2)(2.10) LTM telemetry

文章目录 前言 1 协议概述 2 配置 3 带FPV视频发射器的使用示例 4 使用TCM3105的FSK调制解调器示例 前言 轻量级 TeleMetry 协议 (LTM) 是一种单向通信协议(从飞行器下行的数据链路),可让你以低带宽/低波特率(通常为 2400 波…...

工具推荐系列-极客编辑器(实时在线编写md文件同步GitHub)

工具项目地址:https://github.com/geekeditor/geekeditor-desktop-releases/tree/main 工具基础配置方法:https://www.geekeditor.com/workspace1.x.html 详细同步代码仓的方法可以用下面: 如何创建GitHub仓库 及生成获取AccessToken&#xf…...

3d gaussian splatting介绍整理

3D 高斯分布是用于实时辐射场渲染的 3D 高斯分布中描述的一种光栅化技术,它允许实时渲染从小图像样本中学习到的逼真场景。 paper github 本文翻译整理自: blog: Introduction to 3D Gaussian Splatting DDPMs - Part 2 给出一些2D图片,用…...

[C#]de4dot常用命令

命令:de4dot.exe "D:\xxx.exe" 解释:运行后文件在程序集的目录下生成一个带-cleaned的新程序集。 命令:de4dot.exe file1 -f "D:\xxx.exe" -o "D:\output\xxx_cleaned.exe" 解释:-f : 指定.NET 程序…...

变量 varablie 声明- Rust 变量 let mut 声明与 C/C++ 变量声明对比分析

一、变量声明设计:let 与 mut 的哲学解析 Rust 采用 let 声明变量并通过 mut 显式标记可变性,这种设计体现了语言的核心哲学。以下是深度解析: 1.1 设计理念剖析 安全优先原则:默认不可变强制开发者明确声明意图 let x 5; …...

FastAPI 教程:从入门到实践

FastAPI 是一个现代、快速(高性能)的 Web 框架,用于构建 API,支持 Python 3.6。它基于标准 Python 类型提示,易于学习且功能强大。以下是一个完整的 FastAPI 入门教程,涵盖从环境搭建到创建并运行一个简单的…...

抖音增长新引擎:品融电商,一站式全案代运营领跑者

抖音增长新引擎:品融电商,一站式全案代运营领跑者 在抖音这个日活超7亿的流量汪洋中,品牌如何破浪前行?自建团队成本高、效果难控;碎片化运营又难成合力——这正是许多企业面临的增长困局。品融电商以「抖音全案代运营…...

质量体系的重要

质量体系是为确保产品、服务或过程质量满足规定要求,由相互关联的要素构成的有机整体。其核心内容可归纳为以下五个方面: 🏛️ 一、组织架构与职责 质量体系明确组织内各部门、岗位的职责与权限,形成层级清晰的管理网络&#xf…...

JUC笔记(上)-复习 涉及死锁 volatile synchronized CAS 原子操作

一、上下文切换 即使单核CPU也可以进行多线程执行代码,CPU会给每个线程分配CPU时间片来实现这个机制。时间片非常短,所以CPU会不断地切换线程执行,从而让我们感觉多个线程是同时执行的。时间片一般是十几毫秒(ms)。通过时间片分配算法执行。…...

代码随想录刷题day30

1、零钱兑换II 给你一个整数数组 coins 表示不同面额的硬币,另给一个整数 amount 表示总金额。 请你计算并返回可以凑成总金额的硬币组合数。如果任何硬币组合都无法凑出总金额,返回 0 。 假设每一种面额的硬币有无限个。 题目数据保证结果符合 32 位带…...

【Go语言基础【13】】函数、闭包、方法

文章目录 零、概述一、函数基础1、函数基础概念2、参数传递机制3、返回值特性3.1. 多返回值3.2. 命名返回值3.3. 错误处理 二、函数类型与高阶函数1. 函数类型定义2. 高阶函数(函数作为参数、返回值) 三、匿名函数与闭包1. 匿名函数(Lambda函…...

【Linux】Linux 系统默认的目录及作用说明

博主介绍:✌全网粉丝23W,CSDN博客专家、Java领域优质创作者,掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域✌ 技术范围:SpringBoot、SpringCloud、Vue、SSM、HTML、Nodejs、Python、MySQL、PostgreSQL、大数据、物…...

python爬虫——气象数据爬取

一、导入库与全局配置 python 运行 import json import datetime import time import requests from sqlalchemy import create_engine import csv import pandas as pd作用: 引入数据解析、网络请求、时间处理、数据库操作等所需库。requests:发送 …...

Leetcode33( 搜索旋转排序数组)

题目表述 整数数组 nums 按升序排列&#xff0c;数组中的值 互不相同 。 在传递给函数之前&#xff0c;nums 在预先未知的某个下标 k&#xff08;0 < k < nums.length&#xff09;上进行了 旋转&#xff0c;使数组变为 [nums[k], nums[k1], …, nums[n-1], nums[0], nu…...