docker mysql 主從配置

qiuqiumade發表於2021-01-27
前言

之前配置過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 協議》,轉載必須註明作者和本文連結
別問我八十年代的哪首歌

相關文章