docker搭建主从复制mysql
步骤:
---启动两个master和slave容器
---修改配置master和slave的my.cnf
---master添加从服务器要要复制的用户并授权
---slave添加主服务器相关信息,并开启slave
1.启动两个容器
1 2 3 | docker run -d --name mysql_master -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root mysql docker run -d --name mysql_slave -p 3307:3306 -e MYSQL_ROOT_PASSWORD=root mysql |
2.修改两个配置文件
master
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | # Copyright (c) 2017, 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 as published by # the Free Software Foundation; version 2 of the License. # # 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 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 secure- file -priv= NULL log-bin=mysql-bin server_id=1 # Custom config should go here !includedir /etc/mysql/conf .d/ |
slave
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | # Copyright (c) 2017, 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 as published by # the Free Software Foundation; version 2 of the License. # # 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 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 secure- file -priv= NULL server_id=233 relay_log=mysql-relay-bin # Custom config should go here !includedir /etc/mysql/conf .d/ |
3.添加授权用户,记录master状态
1 2 3 4 5 6 7 8 9 10 11 | create user slave '@' % ' identified by ' 123456'; grant replication slave on *.* to 'slave' @ '%' ; mysql> show master status; +---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+-------------------+ | binlog.000002 | 1249 | | | | +---------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) |
4.添加主服务器信息,开启slave模式
1 2 3 | change master to master_host= '172.17.0.2' , master_user= 'slave' , master_password= '123456' , master_port=3306, master_log_file= 'binlog.000002' , master_log_pos=1249, master_connect_retry=30; start slave; |
主服务器创建数据库验证
报错
1.可能会有下面的报错,解决方法:https://blog.csdn.net/sunbocong/article/details/81634296
The slave I/O thread stops because master and slave have equal MySQL server UUIDs
2.以前有过slave复制会开启失败,解决方法如下:
stop slave;
reset slave;
然后再重复上面的操作就可以了
参考文章:
https://juejin.im/post/5b0f91ab518825153749b1db
https://www.jianshu.com/p/ab20e835a73f
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人