SQL Server故障转移群集 - 确定活动节点

时间:2010-12-20 16:08:23

标签: sql-server windows sql-server-2008 cluster-computing failover

有没有办法以编程方式确定SQL Server故障转移群集中的哪个节点是活动节点?或者至少确定当前机器是否是活动节点?

我有一个Windows程序,它在故障转移群集中的两个物理节点上运行,但根据它是否在主动节点上运行,它应该以不同的方式运行。 部分原因是该程序不应同时在非活动节点和活动节点上运行

(我已经阅读了一些关于让程序集群知晓的内容,但对于这个简单的场景来说,这似乎有点过分了。)

2 个答案:

答案 0 :(得分:9)

从SQL Server:

Select ServerProperty('ComputerNamePhysicalNetBIOS')

您也可以通过Microsoft {.SourceServer.Management.Smo命名空间访问它,如图here所示。

答案 1 :(得分:0)

您可以像这样检查:

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

*记住要更改AvailabilityGroupName

2。阻止在辅助节点上执行作业:

IF master.dbo.svf_AgReplicaState('AvailabilityGroupName')=0  raiserror ('This is not the primary replica.',2,1) 

3。检查辅助节点上的写可用性:

IF (SELECT CONVERT(sysname,DatabasePropertyEx(DB_NAME(),'Updateability'))) != 'READ_ONLY'
BEGIN

-- this server is the primary replica, do something here

END 

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