在搭建好mysql主從之后,我們一般在從庫上通過命令
show slave status\G
來查看主從的狀態,會有很多的參數,接下來筆者就帶大家好好的了解這些參數
root@localhost (none)>show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.100 Master_User: mysync Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.001822 Read_Master_Log_Pos: 290072815 Relay_Log_File: mysqld-relay-bin.005201 Relay_Log_Pos: 256529594 Relay_Master_Log_File: mysql-bin.001821 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: 256529431 Relay_Log_Space: 709504534 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: 2923 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: 13ee75bb-99e2-11e6-be4d-b499baa80e6e Master_Info_File: /home/data/mysql/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Reading event from the relay log 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.02 sec)
參數詳解:
1. Slave_IO_State
這里顯示了當前slave I/O線程的狀態(slave連接到master的狀態)。狀態信息和使用show processlist | grep "system user"(會顯示兩條信息,一條slave I/O線程的,一條是slave SQL線程的)顯示的內容一樣。
slave I/O線程的狀態,有以下幾種:
1) waiting for master update
這是connecting to master狀態之前的狀態
2) connecting to master
I/O線程正嘗試連接到master
3) checking master version
在與master建立連接后,會出現該狀態。該狀態出現的時間非常短暫。
4) registering slave on master
在與master建立連接后,會出現該狀態。該狀態出現的時間非常短暫。
5) requesting binlog dump
在與master建立連接后,會出現該狀態。該狀態出現的時間非常短暫。在這個狀態下,I/O線程向master發送請求,請求binlog,位置從指定的binglog 名字和binglog的position位置開始。
6) waiting to reconnect after a failed binlog dump request
如果因為連接斷開,導致binglog的請求失敗,I/O線程會進入睡眠狀態。然后定期嘗試重連。嘗試重連的時間間隔,可以使用命令"change master to master_connect_trt=X;"改變。
7) reconnecting after a failed binglog dump request
I/O進程正在嘗試連接master
8) waiting for master to send event
說明,已經成功連接到master,正等待二進制日志時間的到達。如果master 空閑,這個狀態會持續很長時間。如果等待的時間超過了slave_net_timeout(單位是秒)的值,會出現連接超時。在這種狀態下,I/O線程會人為連接失敗,並開始嘗試重連
9) queueing master event to the relay log
此時,I/O線程已經讀取了一個event,並復制到了relay log 中。這樣SQL 線程可以執行此event
10) waiting to reconnect after a failed master event read
讀取時出現的錯誤(因為連接斷開)。在嘗試重連之前,I/O線程進入sleep狀態,sleep的時間是master_connect_try的值(默認是60秒)
11) reconnecting after a failed master event read
I/O線程正嘗試重連master。如果連接建立,狀態會變成"waiting for master to send event"
12) waiting for the slave sql thread to free enough relay log space
這是因為設置了relay_log_space_limit,並且relay log的大小已經整張到了最大值。I/O線程正在等待SQL線程通過刪除一些relay log,來釋放relay log的空間。
13) waiting for slave mutex on exit
I/O線程停止時會出現的狀態,出現的時間非常短。
2. Master_Host: 192.168.1.100
mysql主庫的ip地址
3. Master_User: mysync
這個是master上面的一個用戶。用來負責主從復制的用戶,創建主從復制的時候建立的(具有reolication slave權限)。
4. Master_Port: 3306
master服務器的端口 一般是3306
5. Connect_Retry: 60
連接中斷后,重新嘗試連接的時間間隔。默認值是60秒。
#與master相關的日志的信息
6. Master_Log_File: mysql-bin.001822
當前I/O線程正在讀取的主服務器二進制日志文件的名稱。
7. Read_Master_Log_Pos: 290072815
當前I/O線程正在讀取的二進制日志的位置。
#與relay log相關的信息
8. Relay_Log_File: mysqld-relay-bin.005201
當前slave SQL線程正在讀取並執行的relay log的文件名。
9. Relay_Log_Pos: 256529594
當前slave SQL線程正在讀取並執行的relay log文件中的位置;(Relay_Log_File下的Relay_Log_Pos其實一一對應着Relay_Master_Log_File的Exec_Master_Log_Pos。)
10. Relay_Master_Log_File: mysql-bin.001821
當前slave SQL線程讀取並執行的relay log的文件中多數近期事件,對應的主服務器二進制日志文件的名稱。(說白點就是我SQL線程從relay日志中讀取的正在執行的sql語句,對應主庫的sql語句記錄在主庫的哪個binlog日志中)
#slave I/O和SQL線程的狀態(重要)
11. Slave_IO_Running: Yes
I/O線程是否被啟動並成功地連接到主服務器上。
12. Slave_SQL_Running: Yes
SQL線程是否被啟動。
13. Replicate_Do_DB
Replicate_Ignore_DB
Replicate_Do_Table
Replicate_Ignore_Table
Replicate_Wild_Do_Table
Replicate_Wild_Ignore_Table
這些參數都是為了用來指明哪些庫或表在復制的時候不要同步到從庫,但是這些參數用的時候要小心,因為 當跨庫使用的時候 可能會出現問題。
一般情況下 ,限制的時候都用Replicate_Wild_Ignore_Table這個參數。
14. Last_Errno: 0
Last_Error
slave的SQL線程讀取日志參數的的錯誤數量和錯誤消息。錯誤數量為0並且消息為空字符串表示沒有錯誤。
如果Last_Error值不是空值,它也會在從屬服務器的錯誤日志中作為消息顯示。
15. Skip_Counter: 0
SQL_SLAVE_SKIP_COUNTER的值,用於設置跳過sql執行步數。
16. Exec_Master_Log_Pos: 256529431
slave SQL線程當前執行的事件,對應在master相應的二進制日志中的position。(結合Relay_Master_Log_File理解,而且在Relay_Master_Log_File這個值等於Master_Log_File值的時候,Exec_Master_Log_Pos是不可能超過Read_Master_Log_Pos的。)
17. Relay_Log_Space: 709504534
所有原有的中繼日志結合起來的總大小。
18. Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
在START SLAVE語句的UNTIL子句中指定的值。
Until_Condition具有以下值:
1) 如果沒有指定UNTIL子句,則沒有值
2) 如果從屬服務器正在讀取,直到達到主服務器的二進制日志的給定位置為止,則值為Master
3) 如果從屬服務器正在讀取,直到達到其中繼日志的給定位置為止,則值為Relay
Until_Log_File和Until_Log_Pos用於指示日志文件名和位置值。日志文件名和位置值定義了SQL線程在哪個點中止執行。
19. Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Master_SSL_Verify_Server_Cert: No
Master_SSL_Crl:
Master_SSL_Crlpath:
這些字段顯示了被從屬服務器使用加密相關的參數。這些參數用於連接主服務器。
Master_SSL_Allowed具有以下值:
1) 如果允許對主服務器進行SSL連接,則值為Yes
2) 如果不允許對主服務器進行SSL連接,則值為No
3) 如果允許SSL連接,但是從屬服務器沒有讓SSL支持被啟用,則值為Ignored。
與SSL有關的字段的值對應於–master-ca,–master-capath,–master-cert,–master-cipher和–master-key選項的值。
20. seconds_Behind_Master: 2923
這個值是時間戳的差值。是slave當前的時間戳和master記錄該事件時的時間戳的差值。
21. Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
最后一次I/O線程或者SQL線程的錯誤號和錯誤消息。
22. Replicate_Ignore_Server_Ids:
主從復制,從庫忽略的主庫服務器Id號。就是不以這些服務器Id為主庫。
23. Master_Server_Id: 1
Master_UUID: 13ee75bb-99e2-11e6-be4d-b499baa80e6e
Master_Info_File: /home/data/mysql/master.info
分別表示主庫服務器id號,主庫服務器的UUID好,還有在從庫中保存主庫服務器相關的目錄位置。
24. SQL_Delay: 0
一個非負整數,表示秒數,Slave滯后多少秒於master。
25. SQL_Remaining_Delay: NULL
當 Slave_SQL_Running_State 等待,直到MASTER_DELAY秒后,Master執行的事件,此字段包含一個整數,表示有多少秒左右的延遲。在其他時候,這個字段是NULL。
26. Slave_SQL_Running_State: Reading event from the relay log
SQL線程運行狀態:
1) Reading event from the relay log
線程已經從中繼日志讀取一個事件,可以對事件進行處理了。
2) Has read all relay log; waiting for the slave I/O thread to update it
線程已經處理了中繼日志文件中的所有事件,現在正等待I/O線程將新事件寫入中繼日志。
3) Waiting for slave mutex on exit
線程停止時發生的一個很簡單的狀態。
27. Master_Retry_Count: 86400
連接主庫失敗最多的重試次數。
28. Master_Bind:
slave從庫在多網絡接口的情況下使用,以確定用哪一個slave網絡接口連接到master。
29. Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
最后一次I/O線程或者SQL線程錯誤時的時間戳。
#GTID模式相關
30. Retrieved_Gtid_Set:
獲取到的GTID<IO線程>
Executed_Gtid_Set:
執行過的GTID<SQL線程>
Auto_Position: 0
理解與應用:
1、為什么執行stop slave; 再start slave;可以繼續主從關系呢?
其實執行stop slave;就是分別關閉了I/O線程(stop slave IO_THREAD;)和SQL線程(stop slave SQL_THREAD;),I/O線程會維護master.info信息的更新,SQL線程會維護relay-log.info信息的更新,在執行start slave;時候,會依照master.info和relay-log.info信息,繼續執行I/O線程和SQL線程。
2、怎樣從一個從庫再復制一個新的從庫呢?
步驟:
-
-
在從庫上面stop slave IO_THREAD;關閉IO線程
-
等待SQL線程執行完成,當Relay_Master_Log_File和Master_Log_File二進制日志文件名一致,且Exec_Master_Log_Pos等於Read_Master_Log_Pos值。
-
mysqldump導出所需要的庫,到新的從庫服務器,並導入。
-
主庫授權replication slave權限給新的從庫
-
新的從庫使用change master to命令,master_log_file對應Master_Log_File值,master_log_pos對應Read_Master_Log_Pos值。
-
在從庫執行start slave IO_THREAD;和在新的從庫上執行start slave;
-
轉自
本文出自 “鄭小明的技術博客” 博客,請務必保留此出處http://zhengmingjing.blog.51cto.com/1587142/19105