Docker快速构建MySQL主从复制

本文详细介绍了如何配置MySQL服务器的主从复制,包括编辑my.cnf配置文件,开启3306端口,设置服务器ID,以及在Navicat中进行主从连接、停止从机、启动从机等操作。通过这些步骤,可以实现数据库的高可用性和数据同步。
摘要由CSDN通过智能技术生成

1、前提条件

  • 已打开防火墙 3306 3307 3308 端口
  • 已获取 my.cnf 配置文件

2、配置文件

在这里插入图片描述

# Copyright (c) 2014, 2015, 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 Community Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[client]
port		= 3306
socket		= /var/run/mysqld/mysqld.sock

[mysqld_safe]
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
nice		= 0

[mysqld]
skip-host-cache
skip-name-resolve
user		= mysql
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
port		= 3306
basedir		= /usr
datadir		= /var/lib/mysql
tmpdir		= /tmp
lc-messages-dir	= /usr/share/mysql
explicit_defaults_for_timestamp

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address	= 127.0.0.1

#log-error	= /var/log/mysql/error.log

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/

log-bin=mysql-bin
server-id=1 
# 修改最后的值即可

3、命令

在这里插入图片描述

docker run --name 集装箱名 -p 端口:映射端口 -e MYSQL_ROOT_PASSWORD=密码-d MySQL版本
docker cp 配置好的文件位置 集装箱名:对应位置
# 默认为 /etc/my.cnf
docker restart 各集装箱名(空格隔开)

4、操作Navicat

  • 所需命令
stop slave;
# 停止从机
CHANGE MASTER TO MASTER_HOST='对应IP', MASTER_PORT=端口, MASTER_USER='用户', MASTER_PASSWORD='密码',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=154; 
# 连接
# 后两点需要查看 master 信息
START SLAVE;
# 启动从机
show slave status;
# 查看从机状态
show master status;
# 查看主机状态
  • 主机信息
    在这里插入图片描述
  • 查看从机连接状态
    在这里插入图片描述

5、操作主机后刷新从机查看

在这里插入图片描述

PS

本大可不必发表本文,but 博主的配置被自个瞎搞坏了,左摸右摸没办法了抱着死马当活马医的心态,结果一下可以了
附上裂开瞬间
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值