docker mysql 主从配置

前言

之前配置过windows的主从,现在来复习一下docker 的主从配置

主从原理

master库 ——》curd操作——-》记录到binlog日志中——-》通过IO线程——-》存储到ready log(中继日志)——》sql线程—–》写入到slave库

配置
  1. master 配置

    # master 配置
    server-id=1             # 服务ID,主库一般设置为1
    log-bin=binlog          # 文件格式
    binlog_format=mixed        # 日志格式,stament,row,mixed
  2. slave 配置

    # slave 配置
    server-id=2                # 服务ID
    log-bin=binlog
    binlog_format=mixed
  3. master 创建拥有权限的用户,用来主备处理

    # 创建用户
    CREATE USER 'backup'@'%' IDENTIFIED BY '123456';
    # 授予权限
    GRANT ALL PRIVILEGES ON *.* TO 'backup'@'%' WITH GRANT OPTION;
    # 更新权限
    flush privileges;
  4. 查看主库的日志状态

    # master 主库状态
    show master status
    +---------------+----------+--------------+------------------+-------------------+
    | File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    +---------------+----------+--------------+------------------+-------------------+
    | binlog.000002 |      156 |              |                  |                   |
    +---------------+----------+--------------+------------------+-------------------+
    
  5. 主备连接

    # docker master主库 ip地址
    change master to master_host='172.17.0.2',
    master_user='backup',
    master_password='123456',
    master_port=3306,
    master_log_file='binlog.000002',
    master_log_pos=156;
    
    # 开启slave
    start slave
  6. 查看备库状态

    show slave status\G
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 172.17.0.2
                      Master_User: backup
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: binlog.000012
              Read_Master_Log_Pos: 156
                   Relay_Log_File: 5dd396d0bfec-relay-bin.000003
                    Relay_Log_Pos: 365
            Relay_Master_Log_File: binlog.000012
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
                  Replicate_Do_DB:
              Replicate_Ignore_DB:
               Replicate_Do_Table:
           Replicate_Ignore_Table:
          Replicate_Wild_Do_Table:
      Replicate_Wild_Ignore_Table:
                       Last_Errno: 0
                       Last_Error:
                     Skip_Counter: 0
              Exec_Master_Log_Pos: 156
                  Relay_Log_Space: 746
                  Until_Condition: None
                   Until_Log_File:
                    Until_Log_Pos: 0
               Master_SSL_Allowed: No
               Master_SSL_CA_File:
               Master_SSL_CA_Path:
                  Master_SSL_Cert:
                Master_SSL_Cipher:
                   Master_SSL_Key:
            Seconds_Behind_Master: 0
    Master_SSL_Verify_Server_Cert: No
                    Last_IO_Errno: 0
                    Last_IO_Error:
                   Last_SQL_Errno: 0
                   Last_SQL_Error:
      Replicate_Ignore_Server_Ids:
                 Master_Server_Id: 1
                      Master_UUID: 3bc79f30-5616-11eb-9070-0242ac110002
                 Master_Info_File: mysql.slave_master_info
                        SQL_Delay: 0
              SQL_Remaining_Delay: NULL
          Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
               Master_Retry_Count: 86400
                      Master_Bind:
          Last_IO_Error_Timestamp:
         Last_SQL_Error_Timestamp:
                   Master_SSL_Crl:
               Master_SSL_Crlpath:
               Retrieved_Gtid_Set:
                Executed_Gtid_Set:
                    Auto_Position: 0
             Replicate_Rewrite_DB:
                     Channel_Name:
               Master_TLS_Version:
           Master_public_key_path:
            Get_master_public_key: 0
                Network_Namespace:
    
      # 重要参数
      Slave_IO_Running: Yes
      Slave_SQL_Running: Yes
      # 只有当这2个参数都为yes,才是真正联通
  7. 测试流程

    # master 创建 库
    create database t1;
    create table test(id int(11) not null, name varchar(20) not null);
    # 这些创建完之后,从库也会有
本作品采用《CC 协议》,转载必须注明作者和本文链接
别问我八十年代的哪首歌
邓sir
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!