2018/09/13《塗抹MySQL》【MySQL復制特性】學習筆記(六)
推薦一首歌
- 《可不可以》張紫豪
好吧,隨便從排行榜上找了一首
讀
第十一章《MySQL的復制特性》
總結
1:復制(Replication) 應用場景?
- 提高性能 (通過主從特性,把寫操作全部放在 Master 端,讀操作放在 Slave 端,提高整體的 寫入/查詢 效率)
- 保存數據 (即使 Master 掛掉,從數據庫還保存著數據)
- 數據分析 (將數據挖掘等耗時吃資源的事,放到 Slave 去做)
2:MySQL 的復制過程是什麽樣子的?
- MySQL 的復制過程默認是異步的,也就是說,Master 和 Slave 是不需要實時連接的,甚至 Slave 可以很長時間都不連接 Master ,需要同步時,在連接 Master(一般沒人這麽做)
- 但是面對 讀寫實時性要求很高的場景,明顯就不合適了。
- 在 MySQL5.0 Google 的 MySQL 團隊提交了一個補丁,可以實現半同步的功能。
- 在 MySQL5.5 之後,官方就自帶了半同步功能
4:配置主從
- 修改 Master 數據庫
- log-bin [必須] 二進制日誌
- server-id [必須] 唯一標識
- 建立專用的復制用戶 [授予權限]
- grant replication slave on *.* to ‘用戶名‘@‘允許主機‘ identified by ‘密碼‘;
- 查詢 Master 主機 bin-log 日誌位置,和開始起始位置
mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | bin-log 日誌位置 | 起始位置 | | | | +------------------+----------+--------------+------------------+-------------------+ 1row in set (0.00 sec)
- 配置 slave 機器
mysql> change master to -> master_host=‘主機IP‘, -> master_port=‘主機端口‘, -> master_user=‘用戶名‘, -> master_password=‘密碼‘, -> master_log_file=‘master主機bin-log日誌位置‘, -> master_log_pos=‘master主機位置‘; Query OK, 0 rows affected, 2 warnings (0.00 sec)
- 配置成功後,查看下 slave 狀態
mysql> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.27.27 Master_User: genee Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 688 Relay_Log_File: 3c02e653a082-relay-bin.000002 Relay_Log_Pos: 496 Relay_Master_Log_File: mysql-bin.000002 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: 688 Relay_Log_Space: 710 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: bb3e01f3-b739-11e8-bc2d-02b2c2845dd0 Master_Info_File: /var/lib/mysql/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: 1 row in set (0.00 sec) ERROR: No query specified
- Last_IO_Error 如果這個字段沒有信息的話,說明是成功的。
5:管理常用命令 ( slave )
- Slave_IO_State 顯示 slave 當前的狀態
- Slave_IO_Running 顯示 I/O 狀態是否正常運行,正常情況下應該在正常運行,除非手動停止或者出現錯誤。
- Last_IO_Errno / Last_SQL_Errno 正常情況下應該輸出空值,在出錯的時候回提示錯誤信息。
- Seconds_Behind_Master 顯示當前 slave 和 master 的主從延遲,註意這裏的主從延遲指的並不是時間上的延遲
- 其實是 slave 接受到的 master 節點日誌,和已經應用到日誌的節點數目。
- 這麽說不好理解,舉個例子來說,比如 I/O 接受到的日誌寫入位置為 3306 ,而 SQL 線程才應用到 1106,中間差了 200 ,Seconds_Behind_Master 的值就會是 200。
- 但是,此事 master 可能已經寫到了 7200 ,所以說,這個值並不是完全精確的代表主從延遲。
- 這個參數,最好是 0,當然,並不意味著為0了就代表主從的完全同步,還時需要通過其他參數來判斷。
- Master_Log_File / Read_Master_Log_Pos 顯示當前讀取 master 的二進制文件位置 / 讀取位置
- Relay_Log_File / Relay_Log_Pos 顯示當前當前 slave 正在使用處理的中繼文件位置 / 讀取位置
- Relay_Master_Log_File / Exec_Master_Log_Pos 顯示當前當前 slave 正在使用二進制文件位置 / 讀取位置
6: Master 管理 slave 狀態
- 在本地搭建了兩個 slave 從機,作為試驗
mysql> show processlist \G; *************************** 1. row *************************** Id: 14 User: root Host: localhost db: demo Command: Query Time: 0 State: starting Info: show processlist *************************** 2. row *************************** Id: 21 User: genee Host: 172.17.42.3:60222 db: NULL Command: Binlog Dump Time: 21932 State: Master has sent all binlog to slave; waiting for more updates Info: NULL *************************** 3. row *************************** Id: 23 User: genee Host: 172.17.42.4:43398 db: NULL Command: Binlog Dump Time: 116 State: Master has sent all binlog to slave; waiting for more updates Info: NULL 3 rows in set (0.00 sec)
- 通過 show processlist 可以在 Master 主機上獲取全部的 Slave 從機信息,包括他們的連接狀態等
- 但是如果我們只想獲取 slave 的節點消息時,直接可以使用 show slave hosts 來獲取全部的狀態信息。
mysql> show slave hosts; +-----------+------+------+-----------+--------------------------------------+ | Server_id | Host | Port | Master_id | Slave_UUID | +-----------+------+------+-----------+--------------------------------------+ | 9999 | | 3306 | 1 | a34fad42-b7ca-11e8-a457-0242ac112a04 | | 11 | | 3006 | 1 | 898e0435-b753-11e8-99c5-0242ac112a03 | +-----------+------+------+-----------+--------------------------------------+ 2 rows in set (0.00 sec)
7:復制特性的原理?
- 復制的核心,就是基於 Master 節點對數據庫的各項變更機制。
- Slave 只要連接到 Master 就會不斷的去請求 Master 的二進制文件,拿到二進制文件中,解析之後再本地處理。(這個過程中 Slave 是主導地位拉取日誌)
7:MySQL的復制模式?(目前只做了解,之後如果有機會的話會詳細說)
- 基於語句復制 (SBR) 基於實際執行的SQL語句復制方案。[S]tatement [B]ase [R]eplication
- 基於記錄復制 (RBR) 基於修改列的復制模式。[R]ow [B]ase [R]eplication
- 混合復制模式 (MBR) 。 [M]ixed [B]ase [R]eplication
- 可以通過 查看 binlog_format 參數查看
mysql> show global variables like ‘binlog_format‘; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | binlog_format | ROW | +---------------+-------+ 1 row in set (0.00 sec)
- statment 基於語句復制
- row 基於行復制
- mixed 混合復制
8:什麽是中繼日誌?
- 從文件類型上來看,中繼日誌(Relay Log) 和二進制日誌及其相似,區別是邏輯上的。
- 二進制日誌用於保存自身節點產生的數據。
- 中繼日誌則是保存接受其他節點的事件。
- 可以在 my.cnf 中指定中繼日誌的位置和索引位置(不要隨意更改 日誌位置/和主機名稱 -> 會導致文件位置和索引位置不符合報錯)
- relay-log=‘中繼日誌位置’
- relay-log-index=‘中繼日誌索引位置’
- MySQL 會在下列情況下 創建新的中繼日誌文件,並更新其索引
- 啟動 Slave 節點時候。
- 日誌文件達到最大值
- 由 max_relay_log_size 和 控制。(如果沒有 max_relay_log_size 則指定 max_binlog_size 來確定大小)
9:部署聯級 Slave 增強性能
- 總的來說,如果只有 Master 為全部的 Slave 提供節點服務,那麽本身就會增加 Master 的負載
- 所以,我們也可以讓 Slave 承擔起中轉節點的作用。
- 但是 Slave 只有中繼日誌,沒有二進制日誌怎麽辦
- 設置 log-slave-updates 參數就好啦
-
10:復制環境中的主從切換
- 在 MySQL 中 並沒有很嚴格的定義 Master 和 Slave 定義。
- 也就是說,在 MySQL 的復制環境中,隨時都可以通過 change master to 來改變。隨意性非常強
- 主從的切換也就是如此,也是用過改變 Master 的狀態來改變
2018/09/13《塗抹MySQL》【MySQL復制特性】學習筆記(六)