是否可以从任何系统数据库表中获取复制状态 ?
使用它我可以识别复制是向上还是向下。
我需要知道SLAVE_IO_RUNNING
和SLAVE_SQL_RUNNING = YES
是否来自一个系统表。
这是我根据Manasi的顶级答案所使用的陈述。
SELECT variable_value
FROM information_schema.global_status
WHERE variable_name='SLAVE_RUNNING';
hslakhan的答案适用于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了解其余内容。
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)
注意:这不会工作,除非master_info_repository = TABLE
和relay_log_info_repository=TABLE
是启用的,FILE选项是默认的,将不工作
这个解决方案使用awk来处理show命令输出,并在处理的任何字段中出现错误时发送邮件。在本例中,字段为Slave_IO_Running和Slave_SQL_Running。填充free以添加'show slave status'输出中的其他字段-例如Last_Error/Seconds_Behind_Master或awk其他show命令的输出。
#!/bin/bash
# get some slave stats
Slave_IO_Running=`mysql -u root --password="pwd" -Bse "show slave statusG" | grep Slave_IO_Running | awk '{ print $2 }'`
Slave_SQL_Running=`mysql -u root --password="pwd" -Bse "show slave statusG" | grep Slave_SQL_Running | awk '{ print $2 }'`
Last_error=`mysql -u root --password="pwd" -Bse "show slave statusG" | 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
我在information_schema数据库中得到了解决方案。请检查information_schema数据库中的GLOBAL_STATUS表。如果它是"ON",您将看到一个变量"SLAVE_RUNNING",这意味着复制工作正常。如果它是"OFF",那么复制由于某种原因失败了,您需要检查原因。: -)
Manasi
是否存在可以产生此信息的选择查询?
我做了很多搜索这个答案,我真的很讨厌滚动后运行show slave status;
。
show slave status;
, common_schema
,以及不可用的替代方案。
简单,但不能自定义(也不是SELECT
):
show slave status;
如果您访问common_schema
并且主要希望比master慢几秒:
SELECT SECONDS_BEHIND_MASTER,
slave_status.*
FROM common_schema.slave_status;
如果你只想要最后一个错误消息,忽略master后的秒数:
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的工作,来自他的开源库,我已经将其塑造成一个查询。这应该与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;
来源:
- 很多谷歌搜索,我终于在
- 他的项目是开源的,可以在这里访问,我主要使用_known_thread_states的定义,这是库中的一个表,来重新组合我们这些可以访问他的工作的人可以在
common_schema.slave_status
中使用,这是库中的一个视图。 - 关于性能模式复制表的文档。(我查询
performance_schema.replication_*
中的所有表,直到我找到一个显示我正在寻找的特定错误的表,您可以在上面的链接中看到,只需向下滚动一会儿。)
common_schema.slave_status
上找到了这个很棒的文档Best, Spencer
主要语句是SHOW SLAVE STATUS,您必须在每个SLAVE上执行该语句。参见:http://dev.mysql.com/doc/refman/5.1/en/replication-administration-status.html
在主服务器上,您可以使用SHOW PROCESSLIST检查连接的从服务器的状态,以检查正在运行的进程列表。对于使用——report-host选项启动并连接到主服务器的从服务器,主服务器上的SHOW SLAVE HOSTS语句将显示有关从服务器的基本信息。
从MySQL 5.6开始,您可以通过使用--master-info-repository=TABLE
和--relay-log-info-repository=TABLE
启动服务器来将slave状态存储在表中而不是文件中。
参考:http://dev.mysql.com/doc/refman/5.6/en/slave-logs.html
即使这样,我不确定表是否会包含您正在寻找的特定值(SLAVE_IO_RUNNING
和SLAVE_SQL_RUNNING
)。我不能尝试这个,因为我运行mysql 5.1;我只是在搜索,并在5.6文档中找到它。
听起来你在试图以一种自动化的方式监控线程状态。由于我没有表,所以我计划使用shell脚本和cron作业来完成此操作,如下所示:
$ mysql -u root -pXXXX -e "SHOW SLAVE STATUSG" | grep Slave_IO_Running | awk '{ print $2 }'
$ mysql -u root -pXXXX -e "SHOW SLAVE STATUSG" | grep Slave_SQL_Running | awk '{ print $2 }'
参考:http://www.stardothosting.com/blog/2012/02/checking-and-repairing-mysql-replication-automatically/
我不太确定这有什么大惊小怪的。show slave status是查询。您可以使用任何现代编程语言执行该查询,然后只需选择希望使用的列名,对吗?
例如,在PHP中,我使用: $row = $stmt->fetch();
print "Slave_IO_Running: " . $row['Slave_IO_Running'] . "n";
在$row中获取'show slave status'的结果后。
您也可以在master上运行此命令。
SELECT * FROM information_schema.PROCESSLIST AS p WHERE p.COMMAND = 'Binlog Dump';
在MySQL 5.7中,我们得到如下信息:
select
ras.SERVICE_STATE as sql_status,
rcs.SERVICE_STATE as io_status,
t.PROCESSLIST_TIME as slave_lag
from
performance_schema.replication_applier_status ras
join replication_connection_status rcs
left join performance_schema.threads t on
t.NAME = 'thread/sql/slave_sql'
left join information_schema.processlist pl on
pl.id = t.PROCESSLIST_ID;
很遗憾,没有选择(如information_schema)
检查从复制状态
show slave status;
参考——http://dev.mysql.com/doc/refman/5.0/en/show-slave-status.html