使用select查询的mysql复制状态

时间:2011-08-10 10:04:28

标签: mysql replication status

是否可以从任何系统数据库表中获取复制状态。 使用它我可以识别复制是上升还是下降。

我需要知道系统表中的SLAVE_IO_RUNNING和SLAVE_SQL_RUNNING = YES?

玛纳斯

11 个答案:

答案 0 :(得分:12)

这是我根据Manasi的最佳答案使用的陈述。

SELECT variable_value 
FROM information_schema.global_status 
WHERE variable_name='SLAVE_RUNNING';

答案 1 :(得分:4)

我在information_schema数据库中获得了解决方案。请检查information_schema数据库中的表GLOBAL_STATUS。如果“ON”表示复制工作正常,您将看到变量“SLAVE_RUNNING”。如果它是“OFF”,那么由于任何原因复制失败,你需要检查原因? : - )

玛纳斯

答案 2 :(得分:4)

hslakhan's answer适用于MySQL 5.6,但对于 MySQL 5.7 ,从属状态变量已从information_schema移至performance_schema

Slave_IO_Running对应于:

SELECT SERVICE_STATE FROM performance_schema.replication_connection_status;

Slave_SQL_Running对应于:

SELECT SERVICE_STATE FROM performance_schema.replication_applier_status;

SHOW SLAVE STATUS输出中还有一些其他变量,其余请参见https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_show_compatibility_56_slave_status

答案 3 :(得分:2)

对此的主要陈述是SHOW SLAVE STATUS,您必须在每个从站上执行。 请参阅:http://dev.mysql.com/doc/refman/5.1/en/replication-administration-status.html

在主服务器上,您可以使用SHOW PROCESSLIST检查已连接从服务器的状态,以检查正在运行的进程列表。 对于使用--report-host选项启动并连接到主站的从站,主站上的SHOW SLAVE HOSTS语句显示有关从站的基本信息。

答案 4 :(得分:2)

从MySQL 5.6开始,您可以通过使用--master-info-repository=TABLE--relay-log-info-repository=TABLE启动服务器,将从属状态存储在表而不是文件中。

参考:http://dev.mysql.com/doc/refman/5.6/en/slave-logs.html

即便如此,我也不确定这些表格是否包含您要查找的具体值(SLAVE_IO_RUNNINGSLAVE_SQL_RUNNING)。我无法尝试这个,因为我正在运行mysql 5.1;我只是在5.6文档中搜索并找到它。

听起来您正试图以自动方式监控线程状态。由于我没有表,我计划使用shell脚本和cron作业执行此操作,如下所示:

$ mysql -u root -pXXXX -e "SHOW SLAVE STATUS\G" | grep Slave_IO_Running | awk '{ print $2 }'
$ mysql -u root -pXXXX -e "SHOW SLAVE STATUS\G" | grep Slave_SQL_Running | awk '{ print $2 }'

参考:http://www.stardothosting.com/blog/2012/02/checking-and-repairing-mysql-replication-automatically/

答案 5 :(得分:2)

此解决方案使用 awk 处理show命令输出,并在任何已处理字段中发生错误时发送邮件。在这种情况下,字段是 Slave_IO_Running Slave_SQL_Running 。  可以免费添加“显示奴隶状态”中的其他字段。输出 - Last_Error / Seconds_Behind_Master 例如或 awk 其他show命令的输出。

#!/bin/bash  
# get some slave stats  
Slave_IO_Running=`mysql -u root --password="pwd" -Bse "show slave status\G" | grep Slave_IO_Running | awk '{ print $2 }'`  
Slave_SQL_Running=`mysql -u root --password="pwd" -Bse "show slave status\G" | grep Slave_SQL_Running | awk '{ print $2 }'`  
Last_error=`mysql -u root --password="pwd" -Bse "show slave status\G" | grep Last_error | awk -F : '{ print $2 }'`  

if [ $Slave_SQL_Running == 'No' ] || [ $Slave_IO_Running == 'No' ];  
then  
    echo "Last Error:" $Last_error | mail -s "Replication error on slavedb!!!" devops@company.com  
fi  

exit 0  

答案 6 :(得分:1)

基于这个问题,我写了一个查询来回答你。 请维护版权:-)

SELECT 
    channel_name AS Channel_Name,
    smi.host AS Master_Host,
    smi.user_name AS Master_User,
    smi.port AS Master_Port,
    smi.master_log_name AS Master_Log_File,
    smi.master_log_pos AS Read_Master_Log_Pos,
    ssi.master_log_pos AS Exec_Master_Log_Pos,
    rcs.service_state AS Slave_IO_Running,
    rss.service_state AS Slave_SQL_Running,
    t.processlist_time AS Seconds_Behind_Master,
    rcs.last_error_number AS Last_IO_Errno,
    rcs.last_error_message AS Last_IO_Error,
    rss.last_error_number AS Last_SQL_Errno,
    rss.last_error_message AS Last_SQL_Error,
    tc.processlist_state AS  Slave_IO_State,
    t.processlist_state AS  Slave_SQL_Running_State

FROM
    mysql.slave_master_info smi 
        JOIN
    mysql.slave_relay_log_info ssi USING (channel_name)   
        JOIN 
    performance_schema.replication_connection_status rcs USING (channel_name)
        LEFT JOIN
    performance_schema.replication_applier_status_by_worker rss USING (channel_name)
        LEFT JOIN
    performance_schema.threads t ON (rss.thread_id = t.thread_id)
        LEFT JOIN
    performance_schema.threads tc ON (rcs.thread_id = tc.thread_id)
\G

最好的问候, Renan Benedicto Pereira(BR MySQL DBA)

答案 7 :(得分:0)

我不太确定tbh有什么大惊小怪的。 “显示从站状态”是一个查询。您可以从任何现代编程语言中执行该查询,然后只需选择要使用的列名即可?

例如在PHP中,我使用:

    $row = $stmt->fetch();
    print "Slave_IO_Running: " . $row['Slave_IO_Running'] . "\n";

从$ row中的“显示从属状态”获得结果之后。

答案 8 :(得分:0)

您也可以在master上运行它。

SELECT * FROM information_schema.PROCESSLIST AS p WHERE p.COMMAND = 'Binlog Dump';

答案 9 :(得分:0)

是否存在可以产生此信息的选择查询?

我做了很多搜索工作,非常讨厌运行show slave status;后滚动。

我喜欢@renan的回答。该答案集中在show slave status;common_schema以及其他替代方法上。

简单但不可自定义(而不是SELECT):

show slave status;


如果您有权访问common_schema,并且主要希望比master落后几秒钟:

SELECT SECONDS_BEHIND_MASTER, 
       slave_status.* 
FROM common_schema.slave_status;

如果您只想要最后一条错误消息,则忽略主时钟后面的秒数:

SELECT LAST_ERROR_MESSAGE,
       LAST_ERROR_TIMESTAMP,
       replication_applier_status_by_worker.*
FROM performance_schema.replication_applier_status_by_worker

我都使用(两者):

这将显示与SECONDS_BEHIND_MASTER在同一行上的最新错误。如果该值不为零,则可能正在运行大型查询,或者您可能在show slave status;命令中发现错误,也可能在performance_schema中发现了该错误。

SELECT LAST_ERROR_MESSAGE,
       LAST_ERROR_TIMESTAMP,
       SECONDS_BEHIND_TABLE.*
FROM performance_schema.replication_applier_status_by_worker
         left join (select SECONDS_BEHIND_MASTER,
                           null                 'RUNNING_INDICATORS ->',
                           SLAVE_RUNNING,
                           SLAVE_IO_RUNNING,
                           SLAVE_SQL_RUNNING,
                           'show slave status;' FOR_MORE
                    from common_schema.slave_status) SECONDS_BEHIND_TABLE on TRUE;

最后:如果您无权访问common_schema,但希望您这样做:

请注意::这完全是Shlomi Noach的工作,来自他的open source library,我已将其建模为一个查询。这应该与SELECT * FROM common_schema.slave_status;

的输出相同
SELECT SUM(IF(is_io_thread, TIME, NULL))                                      AS Slave_Connected_time,
       SUM(is_io_thread) IS TRUE                                              AS Slave_IO_Running,
       SUM(is_sql_thread OR (is_system AND NOT is_io_thread)) IS TRUE         AS Slave_SQL_Running,
       (SUM(is_system) = 2) IS TRUE                                           AS Slave_Running,
       SUM(IF(is_sql_thread OR (is_system AND NOT is_io_thread), TIME, NULL)) AS Seconds_Behind_Master
FROM (
         SELECT PROCESSLIST.*,
                USER = 'system user'                                                     AS is_system,
                (USER = 'system user' AND state_type = 'replication_io_thread') IS TRUE  AS is_io_thread,
                (USER = 'system user' AND state_type = 'replication_sql_thread') IS TRUE AS is_sql_thread,
                COMMAND = 'Binlog Dump'                                                  AS is_slave
         FROM INFORMATION_SCHEMA.PROCESSLIST
                  LEFT JOIN (
             -- Replication SQL thread states
             select 'Waiting for the next event in relay log' state, 'replication_sql_thread' state_type
             union
             select 'Reading event from the relay log' state, 'replication_sql_thread' state_type
             union
             select 'Making temp file' state, 'replication_sql_thread' state_type
             union
             select 'Slave has read all relay log; waiting for the slave I/O thread to update it' state, 'replication_sql_thread' state_type
             union
             select 'Waiting until MASTER_DELAY seconds after master executed event' state, 'replication_sql_thread' state_type
             union
             select 'Has read all relay log; waiting for the slave I/O thread to update it' state, 'replication_sql_thread' state_type
             union
             -- Replication I/O thread states
             select 'Waiting for an event from Coordinator' state, 'replication_io_thread' state_type
             union
             select 'Waiting for master update' state, 'replication_io_thread' state_type
             union
             select 'Connecting to master ' state, 'replication_io_thread' state_type
             union
             select 'Checking master version' state, 'replication_io_thread' state_type
             union
             select 'Registering slave on master' state, 'replication_io_thread' state_type
             union
             select 'Requesting binlog dump' state, 'replication_io_thread' state_type
             union
             select 'Waiting to reconnect after a failed binlog dump request' state, 'replication_io_thread' state_type
             union
             select 'Reconnecting after a failed binlog dump request' state, 'replication_io_thread' state_type
             union
             select 'Waiting for master to send event' state, 'replication_io_thread' state_type
             union
             select 'Queueing master event to the relay log' state, 'replication_io_thread' state_type
             union
             select 'Waiting to reconnect after a failed master event read' state, 'replication_io_thread' state_type
             union
             select 'Reconnecting after a failed master event read' state, 'replication_io_thread' state_type
             union
             select 'Waiting for the slave SQL thread to free enough relay log space' state, 'replication_io_thread' state_type
         ) known_states ON (known_states.state LIKE CONCAT(PROCESSLIST.STATE, '%'))
         WHERE USER = 'system user'
            OR COMMAND = 'Binlog Dump'
     ) common_schema_slave_status;

来源:

  • 经过大量的搜索,终于在common_schema.slave_status上找到了这个很棒的文档
  • 该项目是开源的,可以访问here,在这里我主要使用_known_thread_states的定义(它是库中的一个表)来重新构成我们这些可以访问他的作品的人在common_schema.slave_status中使用,这是库中的视图。
  • performance schema replication tables上的文档。 (我在performance_schema.replication_*中查询了所有表,直到发现one都显示出我正在寻找的特定错误,您可以在上面的链接中看到,只需向下滚动一会儿即可。)

最好,斯宾塞

答案 10 :(得分:-1)

afaik,没有选择(如information_schema)

检查从属复制状态

show slave status;

参考 - http://dev.mysql.com/doc/refman/5.0/en/show-slave-status.html