基于docker-compsoe.yml配置mysql8.0主从 (需要进入主从容器执行命令)

基于docker-compsoe.yml配置mysql主从 (需要进入主从容器执行命令)

  1. 主my.cnf文件

    [mysqld]
    user=mysql                     # MySQL启动用户
    default-storage-engine=INNODB  # 创建新表时将使用的默认存储引擎
    character-set-server=utf8mb4      # 设置mysql服务端默认字符集
    pid-file        = /var/run/mysqld/mysqld.pid  # pid文件所在目录
    socket          = /var/run/mysqld/mysqld.sock # 用于本地连接的socket套接字
    datadir         = /var/lib/mysql              # 数据文件存放的目录
    #log-error      = /var/log/mysql/error.log
    #bind-address   = 127.0.0.1                   # MySQL绑定IP
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0
    sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION # 定义mysql应该支持的sql语法,数据校验等!
    # 允许最大连接数
    max_connections=200
    
    # ================= ↓↓↓ mysql主从同步配置start ↓↓↓ =================
    
    # 同一局域网内注意要唯一
    server-id=3306
    # 开启二进制日志功能 & 日志位置存放位置`/var/lib/mysql`
    #log-bin=mysql-bin
    log-bin=/var/lib/mysql/mysql-bin
    # binlog格式
    # 1. STATEMENT:基于SQL语句的模式,binlog 数据量小,但是某些语句和函数在复制过程可能导致数据不一致甚至出错;
    # 2. MIXED:混合模式,根据语句来选用是 STATEMENT 还是 ROW 模式;
    # 3. ROW:基于行的模式,记录的是行的完整变化。安全,但 binlog 会比其他两种模式大很多;
    binlog_format=ROW
    # FULL:binlog记录每一行的完整变更 MINIMAL:只记录影响后的行
    binlog_row_image=FULL
    # 日志文件大小
    # max_binlog_size=1G
    max_binlog_size=100M
    # 定义清除过期日志的时间(这里设置为7天)
    expire_logs_days=7
    
    # ================= ↑↑↑ mysql主从同步配置end ↑↑↑ =================
    
    [mysql]
    default-character-set=utf8mb4
    
    [client]
    default-character-set=utf8mb4  # 设置mysql客户端默认字符集
    
    
    
  2. 从my.cnf文件

    
    [mysqld]
    user=mysql                     # MySQL启动用户
    default-storage-engine=INNODB  # 创建新表时将使用的默认存储引擎
    character-set-server=utf8mb4      # 设置mysql服务端默认字符集
    pid-file        = /var/run/mysqld/mysqld.pid  # pid文件所在目录
    socket          = /var/run/mysqld/mysqld.sock # 用于本地连接的socket套接字
    datadir         = /var/lib/mysql              # 数据文件存放的目录
    #log-error      = /var/log/mysql/error.log
    #bind-address   = 127.0.0.1                   # MySQL绑定IP
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0
    sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION # 定义mysql应该支持的sql语法,数据校验等!
    
    # 允许最大连接数
    max_connections=200
    
    # ================= ↓↓↓ mysql主从同步配置start ↓↓↓ =================
    
    # 同一局域网内注意要唯一,从库只需要设置 server_id 即可
    server-id=3307
    
    # ================= ↑↑↑ mysql主从同步配置end ↑↑↑ =================
    
    [mysql]
    default-character-set=utf8mb4
    
    [client]
    default-character-set=utf8mb4  # 设置mysql客户端默认字符集
    
    
    
  3. 主要的yml配置

    version: '3'
    
    # 网桥 -> 方便相互通讯
    networks:
      mysql:
        driver: bridge
    
    services:
      mysql-master:
        image: mysql  #镜像
        container_name: mysql-master                                 # 容器名为'mysql-master'
        restart: unless-stopped                                       # 指定容器退出后的重启策略为始终重启,但是不考虑在Docker守护进程启动时就已经停止了的容器
        volumes:                                                      # 数据卷挂载路径设置,将本机目录映射到容器目录
          - "./mysql/master/my.cnf:/etc/mysql/my.cnf"
          - "./mysql/master/data:/var/lib/mysql"
          #      - "./mysql/master/conf.d:/etc/mysql/conf.d"
          - "./mysql/master/log/mysql/error.log:/var/log/mysql/error.log"
          - "./mysql/master/mysql-files:/var/lib/mysql-files"
        environment:                        # 设置环境变量,相当于docker run命令中的-e
          TZ: Asia/Shanghai
          LANG: en_US.UTF-8
          MYSQL_ROOT_PASSWORD: "123456"         # 设置root用户密码
        ports:                              # 映射端口
          - "3306:3306"
        networks:
          - mysql
    
      mysql-slave:
        image: mysql  #镜像
        container_name: mysql-slave                                  # 容器名为'mysql-slave'
        restart: unless-stopped                                       # 指定容器退出后的重启策略为始终重启,但是不考虑在Docker守护进程启动时就已经停止了的容器
        volumes:                                                      # 数据卷挂载路径设置,将本机目录映射到容器目录
          - "./mysql/slave/my.cnf:/etc/mysql/my.cnf"
          - "./mysql/slave/data:/var/lib/mysql"
          #      - "./mysql/slave/conf.d:/etc/mysql/conf.d"
          - "./mysql/slave/log/mysql/error.log:/var/log/mysql/error.log"
          - "./mysql/slave/mysql-files:/var/lib/mysql-files"
        environment:                        # 设置环境变量,相当于docker run命令中的-e
          TZ: Asia/Shanghai
          LANG: en_US.UTF-8
          MYSQL_ROOT_PASSWORD: "123456"     # 设置root用户密码
        ports:                              # 映射端口
          - "3307:3306"
        depends_on:
          - mysql-master
        networks:
          - mysql
    
    
  4. 主容器的操作

    1. 进入主mysql容器 3306端口的
      docker exec -it 容器id /bin/bash
    2. 登录mysql
      mysql -uroot -proot
    3. 创建主从复制的账号 - 创建用户slave,密码123456
      CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
    4. 授权主从复制
      GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
    5. 刷新权限
      FLUSH PRIVILEGES;
    6. 查看状态
      show master status;
      +------------------+----------+--------------+------------------+-------------------+
      | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
      +------------------+----------+--------------+------------------+-------------------+
      | mysql-bin.000003 |      839 |              |                  |                   |
      +------------------+----------+--------------+------------------+-------------------+
      
    7. 记住File 和 Position下面的值的值
  5. 配置从库

    1. 进入主mysql容器 3307端口的
      docker exec -it 容器id /bin/bash

    2. 登录mysql
      mysql -uroot -proot

    3. 执行主从授权命令

      change master to master_host='我们3306的id或者域名',master_port=3306, master_user='slave', master_password='123456', master_log_file='mysql-bin.000003', master_log_pos= 839, get_master_public_key=1,master_connect_retry=30;
      

      master_log_file='mysql-bin.000003',和master_log_pos= 839就是我们刚刚记住的值

    4. 开启主从同步
      start slave;

    5. 查询结果
      show slave status \G

    6. 代表成功的标识

      Slave_IO_Running: Yes
      Slave_SQL_Running: Yes
      

此时这样就成功了!!!

你可能感兴趣的:(MySQL,docker,mysql主从,docker-compose)