查看主庫運行狀態
-- 查看主庫運行狀態
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000012
Position: 439767167
Binlog_Do_DB: xxx_db
Binlog_Ignore_DB: information_schema,mysql
Executed_Gtid_Set:
1 row in set (0.00 sec)
查看從庫運行狀態
-- 查看從庫運行狀態
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.10.0.2
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000012
Read_Master_Log_Pos: 439767167
Relay_Log_File: xxxx-relay-bin.000018
Relay_Log_Pos: 33321
Relay_Master_Log_File: mysql-bin.000012
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: xxx_db
Replicate_Ignore_DB: mysql,information_schema
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: 439767167
Relay_Log_Space: 34651
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: 84cc6274-2241-11e6-86b1-00161e0c136d
Master_Info_File: /data/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
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
1 row in set (0.00 sec)
其實就是主要看 Slave_IO_Running 和 Slave_SQL_Running 兩個線程的狀態。
-- 負責把主庫bin日誌(Master_Log)內容投遞到從庫的中繼日誌上(Relay_Log)
Slave_IO_Running: Yes
-- 負責把中繼日誌上的語句在從庫上執行一遍
Slave_SQL_Running: Yes
-- Yes:表示正常, No:表示異常
Slave_IO線程相對比較簡單,一般不容易出錯。如果顯示爲No,則有可能以下原因導致:
* 網絡問題
* 權限問題,例如在配置slave同步時因爲slave訪問master沒有權限導致
* master上的binlog文件誤刪或者其他問題導致的master庫突然停止更新binlog日誌。解決方案是找到同步的點和binlog文件,重新change master
相對的Slave_SQL線程就比較容易出錯。例如人爲的在從庫插入一條數據,造成的主從庫不一致。但此時兩個線程的狀態仍然是正常的,主庫插入數據時,到從庫同步時,從庫會出現主鍵重複的錯誤。此時Slave_SQL_Running的狀態變爲No
而Last_SQL_Error, Last_SQL_Error_Timestamp會記錄錯誤的原因和發生的時間
Slave_SQL_Running線程報錯之後,會停止後續的SQL執行,因爲向後執行會導致錯誤修復的難度增加
錯誤修復
-- 先停止slave
stop slave;
-- 跳過執行語句數量
-- 此時需要查看錯誤日誌去修復報錯信息
set global sql_slave_skip_counter=1;
-- 開始slave
start slave;
-- 然後再檢查一下 slave status
如何判斷完全同步
* Master_Log_File 和 Relay_Master_Log_File 所指向的文件必須一致
* Relay_Log_Pos 和 Exec_Master_Log_Pos 位置也要一致才行
* Slave_SQL_Running_State: 顯示爲wait 中繼日誌的sql語句已經全部執行完畢