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.000001和338这两个数据
- 修改示例如下
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…...
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 程序…...
双倍效率:在快马平台中融合chatgpt实现智能代码生成与即时调试
最近在开发过程中,我发现了一个能显著提升效率的工作方式:将ChatGPT的智能生成能力与InsCode(快马)平台的即时调试环境结合起来。这种组合让我在代码编写、问题排查和逻辑优化上都节省了大量时间,今天就来分享一下具体的使用体验。 自然语言…...
实战应用:基于快马平台构建支持实时协作的团队版pencil设计工具
今天想和大家分享一个实战项目:基于InsCode(快马)平台构建团队协作版pencil设计工具的经历。这个工具最终成为了我们产品团队的需求沟通神器,特别适合中小团队快速搭建轻量级设计协作环境。 为什么需要这个工具 我们团队经常遇到设计稿反复修改、版本混乱…...
新手福音:在快马平台上零配置完成你的第一个openclaw交互实验
作为一个刚接触AI的新手,想要在本地电脑上跑通openclaw这样的多模态模型,光是环境配置就能劝退一大波人。最近我在InsCode(快马)平台上发现了一个超友好的入门项目,完全不需要折腾环境,打开浏览器就能直接体验openclaw的核心功能。…...
用噪音打破听觉恐怖谷:RTE 开发者社区发布 RealNoise™ TTS:全球首个原生合成动态声场的语音大模型
在过去的几年里,语音 AI 行业的内卷方向始终如一:更高的采样率、更低的延迟、更纯净的音质。我们不断训练模型去剔除哪怕最微小的背景杂音,追求实验室级别的完美信噪比(SNR)。 然而,当我们在真实的实时互动…...
通义千问大模型+Flask:打造智能PDF批量解析与问答系统
1. 为什么需要智能PDF解析与问答系统 每天都有海量的PDF文档在各个行业流转,从合同协议到财务报表,从学术论文到产品手册。传统的人工阅读和提取方式效率低下,容易出错。我曾经帮一家律师事务所处理过上千份合同,光是找出所有涉及…...
【计算机组成原理】——磁盘性能三要素:容量、寻址与传输的实战解析
1. 磁盘性能三要素:从理论到实战 刚接触计算机组成原理时,我对磁盘性能的理解仅限于"越大越好"。直到有次帮朋友选配NAS存储,面对商家宣传的"7200转高速盘"、"128MB缓存"等参数时,才发现自己完全不…...
RecyclerListView测试终极指南:单元测试与集成测试完整解决方案
RecyclerListView测试终极指南:单元测试与集成测试完整解决方案 【免费下载链接】recyclerlistview High performance listview for React Native and web! 项目地址: https://gitcode.com/gh_mirrors/re/recyclerlistview RecyclerListView是一个专为React …...
颠覆式AI视觉自动化:3大突破重新定义UI测试与跨平台交互
颠覆式AI视觉自动化:3大突破重新定义UI测试与跨平台交互 【免费下载链接】midscene AI-powered, vision-driven UI automation for every platform. 项目地址: https://gitcode.com/GitHub_Trending/mid/midscene 在数字化转型加速的今天,UI自动化…...
QMK Toolbox终极指南:从零开始掌握键盘固件刷写的完整教程
QMK Toolbox终极指南:从零开始掌握键盘固件刷写的完整教程 【免费下载链接】qmk_toolbox A Toolbox companion for QMK Firmware 项目地址: https://gitcode.com/gh_mirrors/qm/qmk_toolbox QMK Toolbox是机械键盘爱好者的必备神器,这款开源工具集…...
为什么你的C盘空间总是不够用?可能是Windows驱动文件在悄悄“发胖“
为什么你的C盘空间总是不够用?可能是Windows驱动文件在悄悄"发胖" 【免费下载链接】DriverStoreExplorer Driver Store Explorer 项目地址: https://gitcode.com/gh_mirrors/dr/DriverStoreExplorer 想象一下这样的场景:你的电脑C盘明明…...

