基於docker的mysql8的主從複製

基於docker的mysql8的主從複製

創建mysql的docker鏡像

  • 構建docker鏡像,其中數據卷配置內容在下面,結構目錄如下

    mysql

    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
        

        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;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章