如何查找mysql DB是slave?

发布于 2024-10-28 18:06:13 字数 39 浏览 5 评论 0原文

如何通过查询查找mysql DB是从属而不使用“显示从属状态”?

How to find mysql DB is slave with out using "show slave status" by query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

祁梦 2024-11-04 18:06:13

这里有 3 个选项,您必须检测复制是否正在运行

选项 #1:检查状态变量“Slave_running”

使用 MySQL 5.1/5.5

select variable_value from information_schema.global_status
where variable_name = 'Slave_running';

使用 MySQL 5.0 及后

SHOW VARIABLES LIKE 'Slave_running';

选项 #2:检查进程列表

使用 MySQL 5.1+/5.5

select COUNT(1) SlaveThreads
from information_schema.processlist
where user = 'system user';
  • 如果 SlaveThreads = 2,复制正在运行
  • 如果 SlaveThreads = 1,复制被中断
  • 如果 SlaveThreads = 0,复制被停止或禁用

使用 MySQL 5.0 及之后版本

SHOW PROCESSLIST;

查找具有“系统”的 2 个 DB 连接用户列中的“用户”。

选项 #3:检查 master.info 是否存在

如果在数据库服务器上设置了复制,请查找 master.info。默认情况下,master.info 通常位于 /var/lib/mysql 或定义 datadir 的任何位置。

只需多次运行“cat master.info”(对于 Windows 社区,请输入 master.info)。如果日志位置发生移动,则复制已开启。如果日志位置没有移动,则可能意味着复制已损坏(SQL 线程中的 SQL 错误)、已停止(由于 STOP SLAVE;)或已禁用(通过运行 CHANGE MASTER TO MASTER_HOST='';)。

Here are 3 options you have to detect if Replication is running

OPTION #1 : Check Status Variable 'Slave_running'

Using MySQL 5.1/5.5

select variable_value from information_schema.global_status
where variable_name = 'Slave_running';

Using MySQL 5.0 and back

SHOW VARIABLES LIKE 'Slave_running';

OPTION #2 : Check the Process List

Using MySQL 5.1+/5.5

select COUNT(1) SlaveThreads
from information_schema.processlist
where user = 'system user';
  • If SlaveThreads = 2, Replication is Running
  • If SlaveThreads = 1, Replication is Broken
  • If SlaveThreads = 0, Replication is Stopped or Disabled

Using MySQL 5.0 and back

SHOW PROCESSLIST;

Look for 2 DB Conenctions thaty have 'system user' in the user column.

OPTION #3 : Check for presence of master.info

If replication is setup on a DB Server, look for master.info. By default, master.info is usually in /var/lib/mysql or wherever datadir is defined.

Simply run 'cat master.info' multiple times (For Windows community, type master.info). If the log position is moving, replication is on. If the log position is not moving, it could mean that replication is either broken (SQL Error in SQL Thread), stopped (due to STOP SLAVE;), or disabled (by running CHANGE MASTER TO MASTER_HOST='';).

回心转意 2024-11-04 18:06:13

根据 MySQL 文档 - 检查复制状态

Slave_IO_Running:I/O是否运行
用于读取 master 二进制文件的线程
日志正在运行。通常情况下,你想要
这是“是”,除非您还没有
开始复制或已明确
使用 STOP SLAVE 停止它。

Slave_SQL_Running:是否运行SQL
用于执行事件的线程
中继日志正在运行。与 I/O 一样
线程,通常应该是“是”。

According to MySQL doc - Checking Replication Status:

Slave_IO_Running: Whether the I/O
thread for reading the master's binary
log is running. Normally, you want
this to be Yes unless you have not yet
started replication or have explicitly
stopped it with STOP SLAVE.

Slave_SQL_Running: Whether the SQL
thread for executing events in the
relay log is running. As with the I/O
thread, this should normally be Yes.

冬天的雪花 2024-11-04 18:06:13

在 MySQL 5.7 之前,您可以通过执行以下查询来检查“slave_running”变量:SHOW GLOBAL STATUS LIKE 'slave_running';

自 MySQL 5.7 起,slave_running 已被删除,上面的查询返回一个空集
您可以启用“show_compatibility_56”来获取该值,但“show_compatibility_56”已被弃用,并将很快被删除。其原因是 MySQL 正在放弃 information_schema GLOBAL_STATUS 和 SESSION_STATUS 表,转而使用 Performance_schema。

在 SHOW SLAVE STATUS 之外获取 MySQL 5.7 中运行的从属状态的正确方法是使用新的基于复制的 Performance_schema 表。

您可以执行以下查询来获取复制服务的状态:
从 Performance_schema.replication_connection_status 选择 SERVICE_STATE;

Prior to MySQL 5.7, you can check the 'slave_running' variable by executing the following query:

 SHOW GLOBAL STATUS LIKE 'slave_running';

Since MySQL 5.7, the slave_running has been removed and the above query returns an empty set
You can enable "show_compatibility_56" to get the value but “show_compatibility_56” is deprecated and will be removed soon. The reason for this is because MySQL is moving away from the information_schema GLOBAL_STATUS and SESSION_STATUS tables in preference for performance_schema.

The correct way to get the status of the slave running in MySQL 5.7 outside of SHOW SLAVE STATUS is to use the new replication-based performance_schema tables.

You can execute the following query to get the status of the replication service:

SELECT SERVICE_STATE FROM performance_schema.replication_connection_status;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文