天天看點

Mysql Docker 主從配置Mysql Docker 主從配置

Mysql Docker 主從配置

作業系統:Windows10 使用的MySQL8.0的docker鏡像

總覽

前置環境說明

    首先在系統中起了一個MySQL8.0的docker鏡像,并在其中插入了初始資料,容器名就叫mysql(視為主庫),從庫在主從分離前還沒有啟動

    記錄檔如下:

# 拉取MySQL8.0版本鏡像并啟動,監聽在3306端口,設定root使用者密碼為root
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -e MYSQL_ROOT_HOST=% -d mysql:latest
           

    其中使用了腳本,自動生成了表和插入資料,下面有腳本連結,可進行點選檢視:

  • 資料庫表初始化腳本
  • 資料庫資料插入初始化腳本

資料導入導出

    這裡不使用腳本對從庫進行初始化,而是從主庫中導出資料,導入到從庫中

    注意:如果主庫有在使用,需要停掉相關的使用程式,或者對主庫進行加鎖

    記錄檔如下:

# 首先從主庫中備份資料到本地(在sh中提前進入準備好的目錄)
docker exec mysql /usr/bin/mysqldump -u root --password=root test > backup.sql

# 運作一個新的mysql,作為從庫
docker run --name mysql_bk1 -p 3309:3306 -e MYSQL_ROOT_PASSWORD=root -e MYSQL_ROOT_HOST=% -d mysql:latest

# 新開的資料啟動可能需要點時間,請耐心等候,使用下面的指令可以看到mysql的運作日志,運作到新的日志内容就說明準備就緒了,可以進行使用
docker logs -f mysql_bk1
# /usr/sbin/mysqld: ready for connections. Version: '8.0.22'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.

# 進入到新開的資料庫中,建立資料庫test
docker exec -ti mysql_bk1 mysql -u root -p
create database test;

# Ctrl-D退出,執行下面的指令導入資料到新開的mysql中,這個需要花費好幾分鐘,請耐心等待;當然也可以docker cp backup.sql檔案到容器中,然後連上資料庫使用source backup.sql
cat backup.sql | docker exec -i mysql_bk1 /usr/bin/mysql -u root --password=root test
           

主庫設定

    首先在本地編寫配置檔案,這裡使用的配置檔案如下,根據情況進行修改即可:

# 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

# Custom config should go here
!includedir /etc/mysql/conf.d/

#主從設定
#主資料庫端ID号
server_id = 1           
 #開啟二進制日志                  
log-bin = mysql-bin    
#需要複制的資料庫名,如果複制多個資料庫,重複設定這個選項即可                  
binlog-do-db = test  
#将從伺服器從主伺服器收到的更新記入到從伺服器自己的二進制日志檔案中                 
log-slave-updates                        
#控制binlog的寫入頻率。每執行多少次事務寫入一次(這個參數性能消耗很大,但可減小MySQL崩潰造成的損失) 
sync_binlog = 1                    
#這個參數一般用在主主同步中,用來錯開自增值, 防止鍵值沖突
auto_increment_offset = 1           
#這個參數一般用在主主同步中,用來錯開自增值, 防止鍵值沖突
auto_increment_increment = 1            
#二進制日志自動删除的天數,預設值為0,表示“沒有自動删除”,啟動時和二進制日志循環時可能删除  
expire_logs_days = 7                    
#将函數複制到slave  
log_bin_trust_function_creators = 1    
           

    記錄檔如下:

# 将主控端本地的配置檔案複制替換MySQL的配置檔案,并重新開機mysql
docker cp .\mysql_master.conf mysql:/etc/mysql/my.cnf
docker restart mysql

# 進入mysql中,檢視master的狀态,注意下面的File和Position,在從庫設定中需要使用
docker exec -ti mysql mysql -u root -p
show master status\G;

*************************** 1. row ***************************
             File: mysql-bin.000001
         Position: 156
     Binlog_Do_DB: test
 Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
           

從庫設定

    首先在本地編寫配置檔案,這裡使用的配置檔案連結如下,根據情況進行修改即可:

# 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

# Custom config should go here
!includedir /etc/mysql/conf.d/

# 主從設定
# 從資料庫端ID号
server_id = 2           
# 開啟二進制日志                  
log-bin = mysql-bin    
# 需要複制的資料庫名,如果複制多個資料庫,重複設定這個選項即可                  
binlog-do-db = test  
# 将從伺服器從主伺服器收到的更新記入到從伺服器自己的二進制日志檔案中                 
log-slave-updates                        
# 控制binlog的寫入頻率。每執行多少次事務寫入一次(這個參數性能消耗很大,但可減小MySQL崩潰造成的損失) 
sync_binlog = 0                    
#log buffer将每秒一次地寫入log file中,并且log file的flush(刷到磁盤)操作同時進行。該模式下在事務送出的時候,不會主動觸發寫入磁盤的操作
innodb_flush_log_at_trx_commit = 0
# MySQL主從複制的時候,當Master和Slave之間的網絡中斷,但是Master和Slave無法察覺的情況下(比如防火牆或者路由問題)。
# Slave會等待slave_net_timeout設定的秒數後,才能認為網絡出現故障,然後才會重連并且追趕這段時間主庫的資料
slave-net-timeout = 60                    
log_bin_trust_function_creators = 1
           

    記錄檔如下:

# 将主控端本地的配置檔案複制替換MySQL的配置檔案,并重新開機mysql
docker cp .\mysql_slave1.conf mysql_bk1:/etc/mysql/my.cnf
docker restart mysql_bk1

# 需要安全證書,執行下面的指令獲驗證書,并進入MySQL彙中
docker exec -ti mysql_bk1 mysql --ssl-mode=DISABLED -h 192.168.101.104 -uroot -proot --get-server-public-key
# 設定主庫連結,master_log_file/master_log_pos設定從上面主庫的File和Position,設定完成後啟動
# 這裡的IP設定可以使用主控端IP,也可以使用docker容器的ip(docker inspect [容器id]|grep IPA 檢視 IP)
# MASTER_HOST:主資料庫的主機ip
# MASTER_PORT:主資料庫的端口,不設定則預設是3306
# MASTER_USER:主資料庫被授予同步複制權限的使用者名
# MASTER_PASSWORD:對應的使用者密碼
# MASTER_LOG_FILE:在主資料庫執行指令show master status 查詢到的二進制日志檔案名稱
# MASTER_LOG_POS:在主資料庫執行指令show master status 查詢到的位置 Position的值
change master to master_host='192.168.101.104',master_user='root',master_password='root',master_log_file='mysql-bin.000001',master_log_pos=156;
start slave;

# 檢視,大緻如下,沒有錯誤即可
show slave status\G;

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.101.104
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 859
               Relay_Log_File: 28bc4fb44f99-relay-bin.000002
                Relay_Log_Pos: 324
        Relay_Master_Log_File: mysql-bin.000001
             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: 859
              Relay_Log_Space: 540
              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: f6b20c68-23d1-11eb-a497-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:
1 row in set, 1 warning (0.01 sec)
           

    另外一個從庫,配置和上面的指令完全一樣,隻是将mysql_bk1換成mysql_bk2即可

主從測試

    進入到主庫中,插入一條資料,在從庫中查詢可以看到即可

    記錄檔如下:

docker exec -ti mysql mysql -u root -p
insert into stores (name, description) VALUES ("name103", "description103");

docker exec -ti mysql mysql_bk1 -u root -p
select * from stores;
           

參考連結

  • Mysql 主從複制搭建-極簡版
  • MySQL8.0主從複制的配置
  • MySQL主從配置詳解