1. 程式人生 > 實用技巧 >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;

參考連結