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

docker-compos mysql5.7主从配置

docker-compos mysql5.7主从配置

docker-compose目录结构

配置文件

master/my.cnf

[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]
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
secure-file-priv=/var/lib/mysql-files
lc-messages-dir = /usr/share/mysql
explicit_defaults_for_timestamp

log-bin = mysql-bin
server-id = 1

slave/my.cn

[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]
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
secure-file-priv=/var/lib/mysql-files
lc-messages-dir = /usr/share/mysql
explicit_defaults_for_timestamp

log-bin = mysql-bin
server-id = 2

docker-compose.yml文件

version: '3.2'
services:
  mysql-master:
    image: "mysql:5.7"
    container_name: mysql-master
    restart: always
    privileged: true
    environment:
      MYSQL_ROOT_PASSWORD: 123456  #主库root用户的密码
    command:
      --default-authentication-plugin=mysql_native_password
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --explicit_defaults_for_timestamp=true
      --lower_case_table_names=1
      --max_allowed_packet=128M;
    ports:
      - 3306:3306  #映射宿主机端口:容器端口
    volumes:  #宿主机配置目录或文件挂载到容器
      - ./master/my.cnf:/etc/mysql/my.cnf
      - ./master/mysql-files:/var/lib/mysql-files
      - ./master/data:/var/lib/mysql
      - ./master/log:/var/log/
    networks:
      mysql-network:
        ipv4_address: 10.2.0.2
  mysql-slave:
    image: "mysql:5.7"
    container_name: mysql-slave
    restart: always
    privileged: true
    environment:
      MYSQL_ROOT_PASSWORD: 123456  #从库root用户的密码
    command:
      --default-authentication-plugin=mysql_native_password
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --explicit_defaults_for_timestamp=true
      --lower_case_table_names=1
      --max_allowed_packet=128M;
    ports:
      - 3307:3306  #映射宿主机端口:容器端口
    volumes:  #宿主机配置目录或文件挂载到容器
      - ./slave/my.cnf:/etc/mysql/my.cnf
      - ./slave/mysql-files:/var/lib/mysql-files
      - ./slave/data:/var/lib/mysql
      - ./slave/log:/var/log/
    networks:
      mysql-network:
        ipv4_address: 10.2.0.3
networks:  #定义容器连接网络
  mysql-network:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 10.2.0.0/24

运行

在配置文件目录下使用docker compose up -d运行

主节点的mysql配置:

进入主节点docker容器

docker exec -it mysql-master bash
mysql -uroot -p123456

#创建账号用于从机连接主机

GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' identified by '123456';

权限立刻生效

flush privileges;

查看binglog信息

show master status \G;

从节点的mysql配置:

进入从节点

docker exec -it mysql-slave bash
mysql -uroot -p123456

配置连接

下面几个参数需要与主节点的信息对应

CHANGE MASTER TO MASTER_HOST='10.2.0.2', MASTER_USER='repl', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS=589;

启动服务

start slave;

查看状态

show slave status \G;


http://www.kler.cn/a/460743.html

相关文章:

  • flutter 专题二十四 Flutter 响应式状态管理框架GetX
  • 【数据结构】双向循环链表的使用
  • python制作打字小游戏
  • GDPU 数据库原理 期末复习
  • 【Java设计模式-1】单例模式,Java世界的“独苗”
  • “AI人工智能软件开发公司:创新技术,引领未来
  • Python入门:9.递归函数和高阶函数
  • 2020最新整理版SpringBoot 面试题
  • 【C++】2029:【例4.15】水仙花数
  • Python列表推导常见问题解析:高效编程的陷阱与避坑指南
  • DeepSeek V3“报错家门”:我是ChatGPT
  • 【brew安装失败】DNS 查询 raw.githubusercontent.com 返回的是 0.0.0.0
  • 电子电气架构 --- 汽车电子电器设计概述
  • 用Pyside6 和sqlite3 重写了《电脑装配单》 加入切换主题 样式
  • 构建一个rust生产应用读书笔记7-确认邮件3
  • 【信息系统项目管理师】高分论文:论信息系统项目的沟通管理(不动产登记系统)
  • Python世界:人生苦短,我用Python
  • 一文讲清楚CSS3新特性
  • Hessian 矩阵与函数的凸性
  • 网络渗透测试实验二:网络嗅探与身份认证
  • 从零到一:构建高效、安全的电商数据API接口
  • Leetcode 从中序与后序遍历序列构造二叉树
  • Rocky Linux 下安装Liboffice
  • 计算机网络 (17)点对点协议PPP
  • Android音频效果处理:基于`android.media.audiofx`包的原理、架构与实现
  • WPF中的Microsoft XAML Behaviors包功能详解