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

发布于 2024-10-08 15:38:45 字数 250 浏览 0 评论 0原文

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

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

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

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?

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.

(I've read a bit about making the program cluster aware, but that seems heavily overkill for this simple scenario.)

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

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

发布评论

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

评论(2

憧憬巴黎街头的黎明 2024-10-15 15:38:45

从 SQL Server:

Select ServerProperty('ComputerNamePhysicalNetBIOS')

您还可以通过 Microsoft.SqlServer.Management.Smo 命名空间访问它,如下所示 此处

From SQL Server:

Select ServerProperty('ComputerNamePhysicalNetBIOS')

You can also access it through the Microsoft.SqlServer.Management.Smo Namespace as shown here.

停滞 2024-10-15 15:38:45

您可以这样检查:

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 

You can check like that:

1. Check Availability Group Status:

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

or

2. prevent executing job on secondary:

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

or

3. check write availability on secondary:

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:

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