MySQL 8.0 Replication主从集群Docker-compose搭建

本文详细介绍了如何使用Docker-compose搭建MySQL 8.0的主从复制集群,包括所需软件版本、配置文件制作、容器创建与运行、同步用户的创建与授权、从库的主库信息配置、同步启动与状态检查。特别提醒,主库必须使用有权限的用户进行操作,且在手动对齐同步点前需确保数据一致。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.需要的软件及版本

MySQL镜像版本 8.0

Docker版本 19.03.9

Docker-compose版本 1.25.5

2.准备配置文件

2.1 docker-compose.yml文件

稍后的命令

docker-compose  up -d

会根据此文件创建docker网络,并根据其中的参数创建名为mysql_master和mysql_slave的两个容器

version: '2'
services:
  mysql-master:
   image: docker.io/mysql
   networks:
       jznet:
         ipv4_address: 172.18.4.24
   volumes:
     - /root/data/mysql-master:/var/lib/mysql
     - /home/xinyue/master.cnf:/etc/mysql/my.cnf
   ports:
     - "3306:3306"
   environment:
     - MYSQL_DATABASE=root
     - MYSQL_ROOT_PASSWORD=123456
  mysql-slave:
   image: docker.io/mysql
   networks:
       jznet:
         ipv4_address: 172.18.4.25
   volumes:
     - /root/data/mysql-slave:/var/lib/mysql
     - /home/xinyue/slave.cnf:/etc/mysql/my.cnf
   ports:
     - "3307:3306"
   environment:
     - MYSQL_DATABASE=root
     - MYSQL_ROOT_PASSWORD=123456
networks:
  jznet:
    driver: bridge
    ipam:
      driver: default
      config:
      - subnet: 172.18.4.0/26

2.2 Master主库配置文件

Master的配置文件,保存在/home/xinyue/目录下,名称为master.cnf

# 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=1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# Custom config should go here
!includedir /etc/mysql/conf.d/

2.3 Slave主库配置文件

Slave的配置文件,保存在/home/xinyue/目录下,名称为slave.cnf

# 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,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值