将其中一个对等节点配置为可用性组的一部分

从 SQL Server 2019 (15.x) CU 13 开始,属于 SQL Server Always On 可用性组的数据库可以作为对等节点参与点对点事务复制拓扑。 本文介绍如何配置此方案。

本示例中的脚本使用 T-SQL 存储过程。

角色和名称

本部分介绍加入本文复制拓扑的各种元素的角色和名称。

Peer1

  • Node1:可用性组 MyAG 中的副本 1。
  • Node2:MyAG 可用性组中的第 2 个副本。
  • MyAG:你在 Node1 和 Node2 上创建的可用性组名称。
  • MyAGListenerName:可用性组侦听器的名称。
  • Dist1:远程分发服务器实例的名称。
  • MyDBName:数据库的名称。
  • P2P_MyDBName:发布的名称。

Peer2

  • Node3:一台托管默认 SQL Server 实例的独立服务器。
  • Dist2:远程分发服务器实例的名称。
  • MyDBName:数据库的名称。
  • P2P_MyDBName:发布名称。

先决条件

  • 在单独的物理服务器或虚拟服务器上托管可用性组的两个 SQL Server 实例。 可用性组包含一个对等数据库。

  • 一个 SQL Server 实例托管另一个对等数据库。

  • 托管分发服务器数据库的两个 SQL Server 实例。

  • 所有服务器实例都需要支持的版本 - 企业版或开发人员版。

  • 所有服务器实例都需要支持的版本 - SQL Server 2019 (15.x) CU13 或更高版本。

  • 所有实例之间的网络连接和带宽充足。

  • 在所有 SQL Server 实例上安装 SQL Server 复制。

    若要查看是否在任何实例上安装了复制,请运行以下查询:

    USE master;   
    GO   
    DECLARE @installed int;   
    EXEC @installed = sys.sp_MS_replication_installed;   
    SELECT @installed; 
    

    注意

    若要避免分发数据库出现单一故障点,请为每个对等数据库使用远程分发服务器。

    对于演示或测试环境,你可以在单个实例上配置分布式数据库。

配置分发服务器和远程发布服务器 (Peer1)

  1. 运行 sp_adddistributor 以配置 Dist1 上的分发。 使用 @password = 指定远程发布服务器连接到分发服务器时所使用的密码。 配置远程分发服务器时,请在每个远程发布服务器上使用此密码。

    USE master;  
    GO  
    EXEC sys.sp_adddistributor  
     @distributor = 'Dist1',  
     @password = '<Strong password for distributor>';  
    
  2. 在分发器上创建分发数据库。

    USE master;
    GO  
    EXEC sys.sp_adddistributiondb  
     @database = 'distribution',  
     @security_mode = 1;  
    
  3. 将 Node1 和 Node2 配置为远程发布服务器。

    @security_mode 确定复制代理如何连接到当前主要副本。

    • 1 = Windows 身份验证。
    • 0 = SQL Server 身份验证。 需要 @login 和 @password。 指定的登录名和密码必须在每个次要副本上均有效。

    注意

    如果任何修改后的复制智能体运行在除分发服务器之外的计算机上,请对到主服务器的连接使用 Windows 身份验证,这要求副本主机计算机之间的通信使用 Kerberos 身份验证。 使用SQL Server登录,连接当前主节点不需要Kerberos认证。

    USE master;  
    GO  
    EXEC sys.sp_adddistpublisher  
     @publisher = 'Node1',  
     @distribution_db = 'distribution',  
     @working_directory = '\\MyReplShare\WorkingDir',  
     @security_mode = 1
    USE master;  
    GO  
    EXEC sys.sp_adddistpublisher  
     @publisher = 'Node2',  
     @distribution_db = 'distribution',  
     @working_directory = '\\MyReplShare\WorkingDir',  
     @security_mode = 1
    

在原始发布者(Node1)上配置发布者

  1. 配置远程分发的原始发布服务器(Node1)。 为 @password 指定与在分发服务器上运行 sp_adddistributor 以配置分发时所使用的相同值。

    exec sys.sp_adddistributor  
    @distributor = 'Dist1',  
    @password = '<Password used when running sp_adddistributor on distributor server>' 
    
  2. 为数据库启用复制功能。

    USE master;  
    GO  
    EXEC sys.sp_replicationdboption  
     @dbname = 'MyDBName',  
     @optname = 'publish',  
     @value = 'true';  
    

将次要副本主机配置为复制发布服务器 (Node2)

在每个辅助副本主机上配置分发功能。 为 sp_adddistributor 指定与您在分销商处运行 @password 以设置分发时所使用的值相同的值。

EXEC sys.sp_adddistributor  
   @distributor = 'Dist1',  
   @password = '<Password used when running sp_adddistributor on distributor server>' 

使数据库成为可用性组的一部分并创建侦听器 (Peer1)

  1. 在目标主副本上,创建可用性组,并将该数据库作为成员数据库。

  2. 为可用性组创建 DNS 侦听器。 复制代理通过侦听器连接到当前主副本。 以下示例会创建一个名为 MyAGListername 的侦听器。

    ALTER AVAILABILITY GROUP 'MyAG'
    ADD LISTENER 'MyAGListenerName' (WITH IP (('<ip address>', '<subnet mask>') [, PORT = <listener_port>]));   
    

    注意

    在前述文字中,方括号内[ ... ]的信息为可选。 用它来指定TCP端口的非默认值。 不要包含括号。

将原始发布服务器重定向到 AG 侦听器名称 (Peer1)

在 Peer1 的分发服务器上,将原始发布服务器重定向到 AG 侦听器名称。

USE distribution;   
GO   
EXEC sys.sp_redirect_publisher    
@original_publisher = 'Node1',   
@publisher_db = 'MyDBName',   
@redirected_publisher = 'MyAGListenerName,<port>';   

注意

在前面的脚本中,,<port> 是可选的。 只有在使用非默认端口时才需要。 不要把角括号 <>加进去。

在原始发布者 - Node1 上创建点对点发布(Peer1)

以下脚本为 Peer1 创建发布。

exec master..sp_replicationdboption  @dbname=  'MyDBName'    
        ,@optname=  'publish'    
        ,@value=  'true'   

GO 

DECLARE @publisher_security_mode smallint = 1 
EXEC [MyDBName].dbo.sp_addlogreader_agent @publisher_security_mode = @publisher_security_mode 

GO 

DECLARE @allow_dts nvarchar(5) = N'false' 
DECLARE @allow_pull nvarchar(5) = N'true' 
DECLARE @allow_push nvarchar(5) = N'true' 
DECLARE @description nvarchar(255) = N'Peer-to-Peer publication of database MyDBName from Node1' 
DECLARE @enabled_for_p2p nvarchar(5) = N'true' 
DECLARE @independent_agent nvarchar(5) = N'true' 
DECLARE @p2p_conflictdetection nvarchar(5) = N'true' 
DECLARE @p2p_originator_id int = 100 
DECLARE @publication nvarchar(256) = N'P2P_MyDBName' 
DECLARE @repl_freq nvarchar(10) = N'continuous' 
DECLARE @restricted nvarchar(10) = N'false' 
DECLARE @status nvarchar(8) = N'active' 
DECLARE @sync_method nvarchar(40) = N'NATIVE' 
EXEC [MyDBName].dbo.sp_addpublication @allow_dts = @allow_dts, @allow_pull = @allow_pull, @allow_push = @allow_push, @description = @description, @enabled_for_p2p = @enabled_for_p2p,  
@independent_agent = @independent_agent, @p2p_conflictdetection = @p2p_conflictdetection, @p2p_originator_id = @p2p_originator_id, @publication = @publication, @repl_freq = @repl_freq, @restricted = @restricted,  
@status = @status, @sync_method = @sync_method 
go 

DECLARE @article nvarchar(256) = N'tbl0' 
DECLARE @description nvarchar(255) = N'Article for dbo.tbl0' 
DECLARE @destination_table nvarchar(256) = N'tbl0' 
DECLARE @publication nvarchar(256) = N'P2P_MyDBName' 
DECLARE @source_object nvarchar(256) = N'tbl0' 
DECLARE @source_owner nvarchar(256) = N'dbo' 
DECLARE @type nvarchar(256) = N'logbased' 
EXEC [MyDBName].dbo.sp_addarticle @article = @article, @description = @description, @destination_table = @destination_table, @publication = @publication, @source_object = @source_object, @source_owner = @source_owner, @type = @type 
go 

DECLARE @article nvarchar(256) = N'tbl1' 
DECLARE @description nvarchar(255) = N'Article for dbo.tbl1' 
DECLARE @destination_table nvarchar(256) = N'tbl1' 
DECLARE @publication nvarchar(256) = N'P2P_MyDBName' 
DECLARE @source_object nvarchar(256) = N'tbl1' 
DECLARE @source_owner nvarchar(256) = N'dbo' 
DECLARE @type nvarchar(256) = N'logbased' 
EXEC [MyDBName].dbo.sp_addarticle @article = @article, @description = @description, @destination_table = @destination_table, @publication = @publication, @source_object = @source_object,  
@source_owner = @source_owner, @type = @type 
GO  

使点对点发布与可用性组(Peer1)兼容

在原始发布服务器 (Node1) 上,运行以下脚本以使发布与可用性组兼容:

USE MyDBName
GO
DECLARE @publication sysname = N'P2P_MyDBName' 
DECLARE @property sysname = N'redirected_publisher' 
DECLARE @value sysname = N'MyAGListenerName,<port>' 
EXEC MyDBName..sp_changepublication @publication = @publication, @property = @property, @value = @value 
GO 

注意

在前面的脚本中,,<port> 是可选的。 只有在使用非默认端口时才需要。 不要把角括号 <>加进去。

完成上述步骤后,可用性组就已准备好参与对等拓扑。 接下来的步骤将配置一个独立的 SQL Server 实例(Peer2)以使其参与其中。

配置分发服务器和远程发布服务器 (Peer2)

  1. 在分发服务器上配置分发功能。

    USE master;   
    GO   
    EXEC sys.sp_adddistributor   
     @distributor = 'Dist2',   
     @password = '**Strong password for distributor**';   
    
  2. 在分发器上创建分发数据库。

    USE master;   
    GO   
    EXEC sys.sp_adddistributiondb   
     @database = 'distribution',   
     @security_mode = 1;     
    
  3. 在分发商 Dist2 上配置 Node3 作为远程发布者。

    USE master;   
    GO   
    EXEC sys.sp_adddistpublisher   
     @publisher = 'Node3',   
     @distribution_db = 'distribution',   
     @working_directory = '\\MyReplShare\WorkingDir2',   
     @security_mode = 1 
    

配置发布服务器 (Peer2)

  1. 在 Node3 上,配置远程分发。

    exec sys.sp_adddistributor  
    @distributor = 'Dist2',  
    @password = '<Password used when running sp_adddistributor on distributor server>' 
    
  2. 在 Node3 上,启用数据库复制功能。

USE master;  
GO  
EXEC sys.sp_replicationdboption  
    @dbname = 'MyDBName',  
    @optname = 'publish',  
    @value = 'true';  

创建对等发布 (Peer2)

在 Node3 上,执行以下命令创建点对点发布。

exec master..sp_replicationdboption  @dbname=  'MyDBName'   
        ,@optname=  'publish'   
        ,@value=  'true'  
go
DECLARE @publisher_security_mode smallint = 1
EXEC [MyDBName].dbo.sp_addlogreader_agent @publisher_security_mode = @publisher_security_mode
go

-- Make sure that the value for @p2p_originator_id is different from Peer1.
DECLARE @allow_dts nvarchar(5) = N'false'
DECLARE @allow_pull nvarchar(5) = N'true'
DECLARE @allow_push nvarchar(5) = N'true'
DECLARE @description nvarchar(255) = N'Peer-to-Peer publication of database MyDBName from Node3'
DECLARE @enabled_for_p2p nvarchar(5) = N'true'
DECLARE @independent_agent nvarchar(5) = N'true'
DECLARE @p2p_conflictdetection nvarchar(5) = N'true'
DECLARE @p2p_originator_id int = 1
DECLARE @publication nvarchar(256) = N'P2P_MyDBName'
DECLARE @repl_freq nvarchar(10) = N'continuous'
DECLARE @restricted nvarchar(10) = N'false'
DECLARE @status nvarchar(8) = N'active'
DECLARE @sync_method nvarchar(40) = N'NATIVE'
EXEC [MyDBName].dbo.sp_addpublication @allow_dts = @allow_dts, @allow_pull = @allow_pull, @allow_push = @allow_push, @description = @description, @enabled_for_p2p = @enabled_for_p2p, @independent_agent = @independent_agent, @p2p_conflictdetection = @p2p_conflictdetection, @p2p_originator_id = @p2p_originator_id, @publication = @publication, @repl_freq = @repl_freq, @restricted = @restricted, @status = @status, @sync_method = @sync_method
GO

DECLARE @article nvarchar(256) = N'tbl0'
DECLARE @description nvarchar(255) = N'Article for dbo.tbl0'
DECLARE @destination_table nvarchar(256) = N'tbl0'
DECLARE @publication nvarchar(256) = N'P2P_MyDBName'
DECLARE @source_object nvarchar(256) = N'tbl0'
DECLARE @source_owner nvarchar(256) = N'dbo'
DECLARE @type nvarchar(256) = N'logbased'
EXEC [MyDBName].dbo.sp_addarticle @article = @article, @description = @description, @destination_table = @destination_table, @publication = @publication, @source_object = @source_object, @source_owner = @source_owner, @type = @type
GO

DECLARE @article nvarchar(256) = N'tbl1'
DECLARE @description nvarchar(255) = N'Article for dbo.tbl1'
DECLARE @destination_table nvarchar(256) = N'tbl1'
DECLARE @publication nvarchar(256) = N'P2P_MyDBName'
DECLARE @source_object nvarchar(256) = N'tbl1'
DECLARE @source_owner nvarchar(256) = N'dbo'
DECLARE @type nvarchar(256) = N'logbased'
EXEC [MyDBName].dbo.sp_addarticle @article = @article, @description = @description, @destination_table = @destination_table, @publication = @publication, @source_object = @source_object, 
@source_owner = @source_owner, @type = @type
GO

创建从 Peer1 到 Peer2 的推送订阅

此步骤将创建从可用性组到 SQL Server 独立实例的推送订阅。

在 Node1 上执行以下脚本。 这一步假设 节点1 正在运行主副本。

EXEC [MyDBName].dbo.sp_addsubscription 
   @publication = N'P2P_MyDBName'
 , @subscriber = N'Node3'
 , @destination_db = N'MyDBName'
 , @subscription_type = N'push'
 , @sync_type = N'replication support only'
GO

EXEC [MyDBName].dbo.sp_addpushsubscription_agent 
   @publication = N'P2P_MyDBName'
 , @subscriber = N'Node3'
 , @subscriber_db = N'MyDBName'
 , @job_login = null
 , @job_password = null
 , @subscriber_security_mode = 1
 , @frequency_type = 64
 , @frequency_interval = 1
 , @frequency_relative_interval = 1
 , @frequency_recurrence_factor = 0
 , @frequency_subday = 4
 , @frequency_subday_interval = 5
 , @active_start_time_of_day = 0
 , @active_end_time_of_day = 235959
 , @active_start_date = 0
 , @active_end_date = 0
 , @dts_package_location = N'Distributor'
GO

从Peer2到可用性组侦听器创建推送订阅

若要创建从 Peer2 到可用性组侦听器的推送订阅,请在 Node3 上运行以下命令。

重要

以下脚本指定了用户的可用性组监听者名称。

@subscriber = N'MyAGListenerName,<port>'
EXEC [MyDBName].dbo.sp_addsubscription 
   @publication = N'P2P_MyDBName'
 , @subscriber = N'MyAGListenerName,<port>'
 , @destination_db = N'MyDBName'
 , @subscription_type = N'push'
 , @sync_type = N'replication support only'
GO

EXEC [MyDBName].dbo.sp_addpushsubscription_agent 
   @publication = N'P2P_MyDBName'
 , @subscriber = N'MyAGListenerName,<port>'
 , @subscriber_db = N'MyDBName'
 , @job_login = null
 , @job_password = null
 , @subscriber_security_mode = 1
 , @frequency_type = 64
 , @frequency_interval = 1
 , @frequency_relative_interval = 1
 , @frequency_recurrence_factor = 0
 , @frequency_subday = 4
 , @frequency_subday_interval = 5
 , @active_start_time_of_day = 0
 , @active_end_time_of_day = 235959
 , @active_start_date = 0
 , @active_end_date = 0
 , @dts_package_location = N'Distributor'
GO

配置链接服务器

在每个辅助副本主机上,确保发布的推送订阅服务器显示为链接服务器。

EXEC sys.sp_addlinkedserver   
    @server = 'MySubscriber';

下一步