sys.sp_adddistributor(Transact-SQL)

适用于:SQL ServerAzureSQL Managed Instance

在 sys.servers 表中创建一个条目(如果没有),将服务器条目标记为分发服务器,并存储属性信息。 此存储过程在数据库的分发服务器上 master 执行,以注册服务器并将其标记为分发服务器。 对于远程分发服务器,它还在发布服务器上从 master 数据库执行以注册远程分发服务器。

Transact-SQL 语法约定

语法

sys.sp_adddistributor
    [ @distributor = ] N'distributor'
    [ , [ @heartbeat_interval = ] heartbeat_interval ]
    [ , [ @password = ] N'password' ]
    [ , [ @from_scripting = ] from_scripting ]
    [ , [ @security_mode = ] security_mode ]
    [ , [ @encrypt_distributor_connection = ] N'encrypt_distributor_connection' ]
    [ , [ @trust_distributor_certificate = ] N'trust_distributor_certificate' ]
    [ , [ @host_name_in_distributor_certificate = ] N'host_name_in_distributor_certificate' ]
    [ , [ @multi_subnet_failover = ] multi_subnet_failover ]
[ ; ]

参数

[ @distributor = ] N'distributor'

分发服务器名称。 @distributor 是 sysname,没有默认值。 只有在设置远程分发服务器时才使用此参数。 它为表中的分发服务器属性 msdb..MSdistributor 添加条目。

注意

可以将服务器名称指定为 <Hostname>,<PortNumber> 默认实例或 <Hostname>\<InstanceName>,<PortNumber> 命名实例。 使用自定义端口在 Linux 或 Windows 上部署 SQL Server 时指定连接的端口号,并禁用浏览器服务。 远程分发服务器的自定义端口号的使用适用于 SQL Server 2019 (15.x) 及更高版本。

[ @heartbeat_interval = ] heartbeat_interval

在不记录进度消息的情况下,代理可以运行的最大分钟数。 @heartbeat_interval 为 int,默认值为 10 分钟。 创建按此间隔运行的 SQL Server 代理作业,以检查正在运行的复制代理的状态。

[ @password = ] N'password'

distributor_admin登录名的密码。 @password 是 系统名,默认为 NULL。 如果密码为 NULL 空字符串, 则@password 重置为随机值。 必须在添加第一个远程分发服务器时配置密码。 distributor_admin 登录和 @password 存储在用于 @distributor RPC连接的链接服务器条目中,包括本地连接。 如果 @distributor 是本地的, distributor_admin 密码会被设置为新的值。 对于具有远程分发服务器的发布服务器,在发布服务器和分发服务器上执行时,必须指定@passwordsp_adddistributor。 sp_changedistributor_password 可用于更改分销商密码。

重要

如果可能,请在运行时提示用户输入安全凭据。 如果必须在脚本文件中存储凭据,则必须保护文件以防止未经授权的访问。

[ @from_scripting = ] from_scripting

@from_scripting 为 位,默认值为 0。 标识为仅供参考。 不支持。 不保证以后的兼容性。

[ @security_mode = ] security_mode

确定从 Publisher 连接到远程分发商时要使用的身份验证类型。 @security_mode 是 int,默认值为 1。

0 指定 SQL Server 身份验证 1 指定 Windows 身份验证 5 指定使用 Publisher 默认托管身份进行 Microsoft Entra 身份验证。 仅当 Publisher 和远程分发器都是 Azure SQL 托管实例 资源时适用。

[ @encrypt_distributor_connection = ] N'encrypt_distributor_connection'

适用于:SQL Server 2025(17.x)及更高版本。

确定是否对从发布服务器到分发服务器的内部链接服务器连接进行加密。 这些值映射到 OLE DB 提供程序 Encrypt 的属性。 @encrypt_distributor_connection 是 nvarchar(10),不可能是 NULL。

@encrypt_distributor_connection 可以是以下值之一:

  • mandatory(Microsoft OLE DB 提供程序 19 默认)
  • no或false(默认为 Microsoft OLE DB 提供者 18)
  • true 或 yes
  • optional
  • strict

[ @trust_distributor_certificate = ] N'trust_distributor_certificate'

适用于:SQL Server 2025(17.x)及更高版本。

指示分发服务器的 TLS 证书是否应在未经验证的情况下受信任。 该值映射到 OLE DB 提供程序 TrustServerCertificate 的属性,在使用自签名证书时通常与 Mandatory 加密设置结合使用。 @trust_distributor_certificate 是 nvarchar(5),不可能是 NULL。

@trust_distributor_certificate 可以是以下值之一:

  • no(默认值)
  • yes

注意

安全默认值与基础 OLEDB 提供程序 19 相关,可增强安全性。 替代默认值的选项比将实例配置为使用受信任的证书不太安全。 重写默认值后,可以选择将 SQL Server 配置为使用证书,然后使用 sp_changedistributor_property 存储过程将 trust_distributor_certificate=no 属性设置回安全默认值。

[ @host_name_in_distributor_certificate = ] N'host_name_in_distributor_certificate'

适用于:SQL Server 2025(17.x)及更高版本。

指定分发服务器的证书中的主机名(与分发服务器名称不同时),例如,IP 地址或 DNS 别名用作分发服务器名称时。 如果证书中的主机名与分发服务器名称匹配, 请将 @host_name_in_distributor_certificate 参数留空。 @host_name_in_distributor_certificate 是任意字符串值的 nvarchar(255), 默认值为 NULL。

[ @multi_subnet_failover = ] multi_subnet_failover

适用于:SQL Server 2025 (17.x) CU 8 及更高版本。

用于在发布者与远程分发商之间传递动态链接服务器创建的信息。 @multi_subnet_failover 是 位,默认值为 0。

将参数值 1 设置为复制拓扑中,通过Always On可用性组监听器或多子网故障切换集群实例访问的远程分发器。 该设置帮助复制代理在故障切换后更快重新连接,并防止连接超时。 更多信息请参见 “与多子网故障转移连接参数”。

  • 如果 0不是使用 MultiSubnetFailover 参数创建动态链接服务器。
  • 如果 1为动态链接服务器,则使用参数创建 MultiSubnetFailover 为 1。

返回代码值

0 (成功)或 1 (失败)。

注解

sp_adddistributor 用于快照复制、事务复制和合并复制。

示例

-- This script uses sqlcmd scripting variables. They are in the form
-- $(MyVariable). For information about how to use scripting variables  
-- on the command line and in SQL Server Management Studio, see the 
-- "Executing Replication Scripts" section in the topic
-- "Programming Replication Using System Stored Procedures".

-- Install the Distributor and the distribution database.
DECLARE @distributor AS sysname;
DECLARE @distributionDB AS sysname;
DECLARE @publisher AS sysname;
DECLARE @directory AS nvarchar(500);
DECLARE @publicationDB AS sysname;
-- Specify the Distributor name.
SET @distributor = $(DistPubServer);
-- Specify the distribution database.
SET @distributionDB = N'distribution';
-- Specify the Publisher name.
SET @publisher = $(DistPubServer);
-- Specify the replication working directory.
SET @directory = N'\\' + $(DistPubServer) + '\repldata';
-- Specify the publication database.
SET @publicationDB = N'AdventureWorks2022'; 

-- Install the server MYDISTPUB as a Distributor using the defaults,
-- including autogenerating the distributor password.
USE master
EXEC sp_adddistributor @distributor = @distributor;

-- Create a new distribution database using the defaults, including
-- using Windows Authentication.
USE master
EXEC sp_adddistributiondb @database = @distributionDB, 
    @security_mode = 1;
GO

-- Create a Publisher and enable AdventureWorks2022 for replication.
-- Add MYDISTPUB as a publisher with MYDISTPUB as a local distributor
-- and use Windows Authentication.
DECLARE @distributionDB AS sysname;
DECLARE @publisher AS sysname;
-- Specify the distribution database.
SET @distributionDB = N'distribution';
-- Specify the Publisher name.
SET @publisher = $(DistPubServer);

USE [distribution]
EXEC sp_adddistpublisher @publisher=@publisher, 
    @distribution_db=@distributionDB, 
    @security_mode = 1;
GO

将分发服务器配置为信任自签名证书

若要替代 OLEDB 19 提供程序的安全默认值并设置 trust_distributor_certificate=yes ,以便分发服务器信任自签名证书,请使用以下示例:

EXECUTE sys.sp_adddistributor @trust_distributor_certificate = 'yes';

注意

安全默认值与基础 OLEDB 提供程序 19 相关,可增强安全性。 替代默认值的选项比将实例配置为使用受信任的证书不太安全。 重写默认值后,可以选择将 SQL Server 配置为使用证书,然后使用 sp_changedistributor_property 存储过程将 trust_distributor_certificate=no 属性设置回安全默认值。

欲了解更多信息,请查看 SQL Server 2025中的远程分发器破坏性变更。

权限

只有 sysadmin 固定服务器角色的成员才能执行sp_adddistributor。