1. 程式人生 > >基於docker的mysql8的主從複製

基於docker的mysql8的主從複製

基於docker的mysql8的主從複製

建立mysql的docker映象

  • 構建docker映象,其中資料卷配置內容在下面,結構目錄如下

    version: '3.7'
    services: 
        db:
          # images 8.x
          image: mysql
          restart: always
          environment: 
            MYSQL_ROOT_PASSWORD: 456123
          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
          ports: 
            - 3309:3306
          volumes: 
            - ./data:/var/lib/mysql
            - ./my.cnf:/etc/my.cnf
    

配置mysql的主庫

  • 更新配置檔案

    # 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
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0
    
    # Custom config should go here
    # [必須]啟用二進位制日誌
    log-bin=mysql-bin 
    # [必須]伺服器唯一ID,預設是1  1~255
    server-id=1 
    sql_mode=STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
    
  • 配置操作

    1. 啟動mysql

      docker-compose up -d 
    2. 更新配置

      • 檢視容器

        docker ps
        

        • 進入mysql互動

           docker exec -it 518a92715f6f /bin/bash
          
        • 登入mysql

           mysql -u root -p
          

        • 配置

          #在主庫上建立同步使用者並授權
          CREATE USER 'replicate'@'112.74.41.236' IDENTIFIED BY '123456';
          GRANT REPLICATION SLAVE ON *.* TO 'replicate'@'112.74.41.236';
          FLUSH PRIVILEGES;
          #最後增加遠端訪問使用者 並賦予所有許可權,遠端訪問測試用
          CREATE USER antsdouble IDENTIFIED BY '123456';
          GRANT ALL ON *.* TO 'antsdouble'@'%';
          #用navicate12及以上可以不用修復修復遠端登入報報 caching_sha2_password異常
          # mysql8 用新的驅動  driver-class-name: com.mysql.cj.jdbc.Driver
          ALTER USER 'antsdouble'@'%' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER;
          ALTER USER 'antsdouble'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
          FLUSH PRIVILEGES;
          #查詢master的狀態,此處File,Position資料在配置從庫時用到
          show master status;

  • 相關說明

    1. server-id是唯一的,主從不能相同,server-id為1表示mysq資料庫為主資料庫,server-id為2表示mysql的資料庫為從資料庫

    2. 為何必須掛載到/etc/my.cnf 檔案上,而不是/etc/mysql/my.cnf 檔案上?因為mysql預設配置檔案位置在/etc/mysql/my.cnf,掛在方式無法改變容器中檔案內容,my.conf內容不會改變,my.cnf中沒有我們自定義的配置內容,啟動mysql容器會報錯

      lower_case_table_names:忽略表名、列名等資料結構的大小寫(注意:不是每行記錄內容的大小寫!)。
      
      
      log-bin:開啟二進位制記錄。這是為了主從複製而做的設定。本文使用RBR(Row-Based Replication)模式。
      
      slow_query_log=1:開啟慢查詢日誌。如果某一條SQL執行的時間超過long_query_time設定的秒數,那麼就記錄下來。記錄檔案路徑可以使用show variables;命令,在變數名是slow_query_log_file下查詢到具體的日誌檔案路徑。
      
      long_query_time=1:單位是秒。指如果某一條SQL語句執行時間超過1秒,就記錄下來。必須開啟慢查詢日誌了以後,此變數才能使用。
      
      log_error:開啟錯誤日誌。show variables like 'log_error'; 就可以查詢到日誌檔案的路徑。mysql的docker官方映象如果設定別的取值會導致容器無法正常啟動。

配置mysql的從庫

  • 更新配置檔案

    # 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
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0
    
    # Custom config should go here
    # [必須]啟用二進位制日誌
    log-bin=mysql-bin 
    # [必須]伺服器唯一ID,預設是1
    server-id=1 
    sql_mode=STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
  • 配置操作

    1. 進入互動模式,方法同主庫

    2. 配置從庫

      change master to master_host='112.74.41.236',master_port=3309,master_user='replicate',master_password='123456',master_log_file='binlog.000006',master_log_pos=3862;
      • 說明

        master_port:Master的埠號,指的是容器的埠號
        
        master_user:用於資料同步的使用者
        
        master_password:用於同步的使用者的密碼
        
        master_log_file:指定 Slave 從哪個日誌檔案開始複製資料,即上文中提到的 File 欄位的值
        
        master_log_pos:從哪個 Position 開始讀,即上文中提到的 Position 欄位的值
        
        master_connect_retry:如果連線失敗,重試的時間間隔,單位是秒,預設是60秒
    3. 檢視

      show slave status\G
      # 查詢slave的狀態,Slave_IO_Running及Slave_SQL_Running程序必須正常執行,即YES狀態,否則都是錯誤的狀態 錯誤可以在2標記處檢視到原因,也可以通過
      show slave status;

測試功能

  • 主庫新增一條數
  • 在從庫檢視

常見問題

  • 可能產生的原因

    網路不通
    
    檢查ip,埠
    
    密碼不對
    
    檢查是否建立用於同步的使用者和使用者密碼是否正確
    
    pos不對
    
    檢查Master的 Position
  • Could not execute Update_rows event on table oa.bui_bill_sum; Can't find record in 'bui_bill_sum', Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND; the event's master log mysql-bin.000138, end_log_pos 160051747

    1. 解決,臨時

      進入sql 執行
      STOP SLAVE; 
      SET GLOBAL sql_slave_skip_counter =1; #表示跳過一步錯誤,後面的數字可變 
      START SLAVE;  
    2. 永久

      在my.cnf中配置
      slave-skip-errors = 1032 就會跳過所有的1032的錯誤,多個用逗號分隔
  • mysql並沒有從my.cnf檔案中更新server_id,既然這樣就只能手動修改了

set global server_id=2; #此處的數值和my.cnf裡設定的一樣就行
slave start;