MySQL數據庫複製技術 Part 2 : 主從複製

1 MySQL數據庫主從複製

        MySQL數據庫主從複製技術是將數據庫更新的binlog日誌發送至從庫服務器,從庫數據庫線程讀取日誌中的SQL語句並應用到MySQL數據庫中,進而實現主從複製。

1.1 企業應用場景

  • 從庫作爲主庫的實時備份
  • 主從數據庫實現讀寫分離,從庫實現負載均衡
  • 應用重要等級劃分,一對一配置從庫

1.2 主從複製關鍵點

  • 主從複製是異步的邏輯SQL複製
  • 主庫binlog線程,從庫I/O線程和SQL線程
  • MySQL 5.6,從庫的SQL線程可以有多個
  • 主庫、從庫均需要開啓binlog
  • 多從庫時,server-id不能相同
  • binlog記錄數據庫內容更改的SQL語句

2 MySQL數據庫主從複製配置

2.1 基礎環境

角色 hostname eth0(業務) eth1(主從同步)
master node1 10.11.12.1 172.16.33.1
slave node2 10.11.12.2 172.16.33.2

2.2 主庫配置

--- 開啓binlog,配置server-id

[mysqld]

server_id = 1
log_bin = /app/mysql5.7/logs/ocean-bin

--- 覈驗配置文件

egerp "server_id | log_bin" /etc/my.cnf

--- 重啓數據庫

/etc/init.d/mysqld restart

--- 覈驗數據庫參數

mysql -e "show variables like 'log_bin';"

mysql -e "show variables like 'server_id';"

--- 主庫創建數據同步的賬號

grant replication slave on *.* to 'rep'@'10.11.12.%' identified by 'root1234';

flush privileges;

--- 覈驗賬號

SELECT user,host FROM mysql.user;

SHOW GRANTS FOR rep@'10.11.12.%';

--- 鎖表只讀(會話窗口勿關閉)

flush table with read lock;

show variables like '%timeout%';

--- 查看主庫狀態

show master status;

--- 備份主庫數據

mysqldump -A -B | gzip > /bak/ocean_$(date +%F).sql.gz

2.3 從庫配置 

--- 開啓binlog,配置server-id

[mysqld]

server_id = 2
#log_bin = /app/mysql5.7/logs/ocean-bin

--- 覈驗配置文件

egerp "server_id | log_bin" /etc/my.cnf

--- 重啓數據庫

/etc/init.d/mysqld restart

--- 覈驗數據庫參數

mysql -e "show variables like 'log_bin';"

mysql -e "show variables like 'server_id';"

--- 備庫恢復數據

zcat /bak/ocean_2019-04-01.sql.gz | mysql

--- 配置主從參數

CHANGE MASTER TO MASTER_HOST='10.11.12.1',MASTER_PORT=3306,MASTER_USER='rep',MASTER_PASSWORD='root1234',MASTER_LOG_FILE='ocean-bin.000006',MASTER_LOG_POS=668;

--- 覈驗master.info中的配置信息

more /app/mysql5.7/data/master.info

--- 啓動從庫,開啓複製

START SLAVE;

--- 查看主從複製狀態

show slave status\G;

mysql -e "show slave status\G;"

--- 查看主從複製關鍵參數

mysql -e "show slave status\G;" | egrep "IO_Running | SQL_Running | _Behind_Master"
  • Slave_IO_Running:I/O線程讀取binlog日誌,寫入從庫的中繼日誌(relay-log)
  • Slave_SQL_Running:SQL線程讀取中繼日誌的數據,轉換成SQL語句應用到從庫
  • Seconds_Behind_Master:從庫的延遲時間(秒爲單位)

2.4 小結

  1. 數據庫操作系統環境一致,關閉selinux、iptables
  2. 配置my.cnf,主庫配置binlog、從庫註釋binlog,server-id不能相同
  3. 主庫創建同步賬戶,授權replication slave
  4. 整庫鎖表(flush table with read lock),查看binlog位置
  5. 開啓新session備份數據
  6. 主庫解鎖(unlock table)
  7. 從庫恢復數據
  8. 主庫查看binlog位置(show master status\G;),從庫配置參數(change master to ...)
  9. 從庫開啓複製(start slave;)
  10. 從庫檢查複製狀態(show slave status\G;)

附錄

        查看MySQL線程

show processlist\G;

        主庫I/O線程 

I/O線程 說明
Sending binlog event to slave 讀取binlog,發送至從服務器
Finished reading one binlog;switching to next binlog 完成binlog數據讀取,正在打開下一個binlog
Has sent all binlog to slave;waiting for binlog to be updated 完成binlog更新數據的讀取和發送,等待binlog更新數據
Waiting to finalize termination 線程停止

        從庫I/O線程

I/O線程 說明
Connecting to master 嘗試連接主服務器
Checking master version 成功連接主庫的臨時狀態
Registering slave on master
Requesting binlog dump 發出請求,索取binlog
Waiting to reconnect after a failed binlog dump request binlog轉儲失敗,線程進入睡眠狀態
Reconnecting after a failed binlog dump request 嘗試重連主庫,索取binlog
Waiting for master to send event 線程連接主庫,等待binlog數據
Queueing master event to the relay log 讀取binlog更新,寫入relay-log
Waiting to reconnect after a failed master event read 連接中斷,線程嘗試重連
Reconnecting after a failed master event read 嘗試重連主庫,等待binlog數據

        從庫SQL線程

SQL線程 說明
Reading event from the relay log 讀取relay-log,處理事務
Has read all relay log;waiting for the slave I/O thread to update it 處理所有事務,等待I/O線程寫入數據庫
Waiting for slave mutex on exit 線程停止

 

 

 

相關鏈接

MySQL數據庫複製技術 Part 1 : 複製技術介紹

MySQL數據庫複製技術 Part 3 : 級聯複製

MySQL數據庫複製技術 Part 4 : 雙主複製

MySQL數據庫複製技術 Part 5 : 半同步複製

MySQL數據庫複製技術 Part 6 : GTID複製 

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