在使用MySQL 5.5时,我们可能会收到生产环境中从节点发出MySQL数据复制已经停止运行的警告。这个警告在我们平时工作时很少会发生。
SHOW SLAVE STATUS
遇到这样的情况,我们首先要做的就是观察服务器的当前数据复制状态。我们会获得如下SQL命令:
slave> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.48
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.001220
Read_Master_Log_Pos: 3453586
Relay_Log_File: relay-log.003586
Relay_Log_Pos: 3452185
Relay_Master_Log_File: mysql-bin.001220
Slave_IO_Running: Yes
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 1062
Last_Error: Error 'Duplicate entry '42-2011-04-16 00:00:00'for key 'user_id'' on query. Default database: 'book'. Query: 'INSERT INTOproduct_comment(product_id,user_id,comment_dt, comment) VALUES (20,42,'2011-04-16 00:00:00','I found this very useful with product Y')'
Skip_Counter: 0
Exec_Master_Log_Pos: 3452040
Relay_Log_Space: 3453930
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: 1062
Last_SQL_Error: Error 'Duplicate entry '42-2011-04-16 00:00:00'for key 'user_id'' on query. Default database: 'book'. Query: 'INSERT INTO product_comment(product_id,user_id,comment_dt,comment) VALUES (20,42,'2011-04-16 00:00:00','I found this very useful with product Y')'
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
下面的几个指标指出了上述输出中的数据复制问题:
SQL线程没有运行,这个结论可以从下列输出看出:
Slave_SQL_Running=No
数据复制滞后量未知,这个结论可以从下列输出看出:
Seconds_Behind_Master=NULL
在Last_Errno和Last_Error中被标识出来的错误信息。
在突发状况下发生错误报告时,将问题修正以确保数据可以被从服务器使用,这比找出导致这个问题的原因更重要。
注意
这个输出的数据列没有根据重要性来排序。按照MySQL产品的约定,会将新的数据列放到列表的最后。例如,Replicate_Ignore_Server_Ids和Master_Server_Id就是MySQL中的新列。在 MySQL 5.1中,单独的错误数字和描述会被添加到I/O和SQL线程列表的最后,而不是在已存在的错误列中。
提示
很多警报系统会使用一些规则来决定错误的条件。在下面这个例子中,警报会被下面的规则触发:Seconds_Behind_Master = NULL OR Seconds_Behind_Master > 30。将SHOW SLAVE STATUS命令的输出合并到邮件警报,是进一步改善诊断的方法。这种方法能够节省时间,而且不需要重新连接服务器。