windows SQL Server 故障转移群集 - 确定活动节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4491411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL Server failover cluster - determine active node
提问by SilverSkin
Is there a way to programmatically determine which node in a SQL Server failover cluster is the active node? Or at least determine whether the current machine is the active node?
有没有办法以编程方式确定 SQL Server 故障转移群集中的哪个节点是活动节点?或者至少确定当前机器是否是活动节点?
I have a Windows program which runs on both physical nodes in a failover cluster, but that should operate differently depending on whether it is running on the active node. Part of the reason is that this program should not run simultaneously on the inactive and the active node.
我有一个 Windows 程序,它在故障转移集群的两个物理节点上运行,但是根据它是否在活动节点上运行,它的运行方式应该有所不同。部分原因是该程序不应在非活动节点和活动节点上同时运行。
(I've read a bit about making the program cluster aware, but that seems heavily overkill for this simple scenario.)
(我已经阅读了一些关于使程序集群感知的内容,但对于这个简单的场景来说,这似乎太过分了。)
回答by Joe Stefanelli
回答by Krzysztof Gapski
You can check like that:
你可以这样检查:
1. Check Availability Group Status:
1. 检查可用性组状态:
if (select
ars.role_desc
from sys.dm_hadr_availability_replica_states ars
inner join sys.availability_groups ag
on ars.group_id = ag.group_id
where ag.name = 'AvailabilityGroupName'
and ars.is_local = 1) = 'PRIMARY'
begin
-- this server is the primary replica, do something here
end
else
begin
-- this server is not the primary replica, (optional) do something here
end
*Remember to change AvailabilityGroupName
*记得修改AvailabilityGroupName
or
或者
2. prevent executing job on secondary:
2. 防止在辅助上执行作业:
IF?master.dbo.svf_AgReplicaState('AvailabilityGroupName')=0??raiserror?('This is not the primary replica.',2,1)
or
或者
3. check write availability on secondary:
3.检查次要的写可用性:
IF?(SELECT?CONVERT(sysname,DatabasePropertyEx(DB_NAME(),'Updateability')))?!=?'READ_ONLY'
BEGIN
-- this server is the primary replica, do something here
END
or
或者
4. for SQL2014 and newer:
4. 对于 SQL2014 及更新版本:
IF?master.dbo.fn_hadr_database_is_primary_replica('Admin')?=?1
????BEGIN?
????????-- this server is the primary replica, do something here
????END
ELSE?
????BEGIN?
????????-- this server is not the primary replica, (optional) do something here
????END