show slave status判断主从同步状态

xiaoxiao2021-02-28  62

show slave status\G 会显示主从同步状态

例如

MySQL> show slave status\G *************************** 1. row ***************************                Slave_IO_State: Waiting for master to send event                   Master_Host: XXX.XXX.XXX.XXX                   Master_User: XXX                   Master_Port: 3306                 Connect_Retry: 60               Master_Log_File: mysql-bin.000145           Read_Master_Log_Pos: 391789243                Relay_Log_File: Report-relay-bin.000228                 Relay_Log_Pos: 480230500         Relay_Master_Log_File: mysql-bin.000144              Slave_IO_Running: Yes             Slave_SQL_Running: No               Replicate_Do_DB: dbXXX           Replicate_Ignore_DB:            Replicate_Do_Table:        Replicate_Ignore_Table:       Replicate_Wild_Do_Table:   Replicate_Wild_Ignore_Table:                    Last_Errno: 1205                    Last_Error: Slave SQL thread retried transaction 10 time(s) i                                                                                        n vain, giving up. Consider raising the value of the slave_transaction_retries v                                                                                        ariable.                  Skip_Counter: 0           Exec_Master_Log_Pos: 480230337               Relay_Log_Space: 1784477152               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: NULL Master_SSL_Verify_Server_Cert: No                 Last_IO_Errno: 0                 Last_IO_Error:                Last_SQL_Errno: 1205                Last_SQL_Error: Slave SQL thread retried transaction 10 time(s) i                                                                                        n vain, giving up. Consider raising the value of the slave_transaction_retries v                                                                                        ariable.   Replicate_Ignore_Server_Ids:              Master_Server_Id: 1                   Master_UUID: 07b51a82-dcd0-11e4-a812-00163e020999              Master_Info_File: /mnt/erp/programdata/data/master.info                     SQL_Delay: 0           SQL_Remaining_Delay: NULL       Slave_SQL_Running_State:            Master_Retry_Count: 86400                   Master_Bind:       Last_IO_Error_Timestamp:      Last_SQL_Error_Timestamp: 150815 01:42:34                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: Yes   Slave_SQL_Running: No

Yes表示正常,No表示异常

  Slave_IO线程负责把主库的bin日志(Master_Log)内容,抄写到从库的中继日志上(Relay_Log)。

  Slave_SQL线程负责把中继日志上的语句在从库上执行一遍。

Slave_IO线程相对比较简单,一般不容易出错,如果Slave_IO_Running显示为No,多为网络连接不上,权限不够等环境问题。

相对的Slave_SQL线程就比较容易出错,例如人为手动的在从库插入一条数据,造成主从不一致。但此时两个线程的状态任然是正常的,

等到主库也插入一条同样的数据时,通知从库做相同操作,从库会出现主键重复的错误。此时Slave_SQL_Running的状态会变为No,

而Last_SQL_Error,Last_SQL_Error_Timestamp会记录错误的原因和发生时间。

Slave_SQL线程会停止后续的SQL语句执行,因为它意识到往后执行会导致错误修复的难度增加。

但Slave_IO线程会继续抄log的工作。所以中继log依然是最新的。

这时候Master_Log_File: mysql-bin.000145 和  Relay_Master_Log_File: mysql-bin.000144之间就产生了偏差。

例如上面的情况,主库的日志已经写到了 mysql-bin.000145,而从库依然停留在 mysql-bin.000144等待人工修复错误。

那么当错误修复后,只需用stop slave和start slave重启下同步。Slave_SQL线程会重新的尝试工作。

如果没有问题,那么Slave_SQL的状态会变回Yes。但此时主从并没有完全同步,需要一点时间。

如何判断主从完全同步呢?

首先Master_Log_File和Relay_Master_Log_File所指向的文件必须一致。

其次Relay_Log_Pos和Exec_Master_Log_Pos的为止也要一致才行。

此时,Slave_SQL_Running_State:显示为wait 意思是中继日志的sql语句已经全部执行完毕,我没事作了

转载请注明原文地址: https://www.6miu.com/read-33813.html

最新回复(0)