在 sys.servers 表中创建一个条目(如果没有),将服务器条目标记为分发服务器,并存储属性信息。 此存储过程在数据库的分发服务器上 master 执行,以注册服务器并将其标记为分发服务器。 对于远程分发服务器,它还在发布服务器上从 master 数据库执行以注册远程分发服务器。
语法
sp_adddistributor
[ @distributor = ] N'distributor'
[ , [ @heartbeat_interval = ] heartbeat_interval ]
[ , [ @password = ] N'password' ]
[ , [ @from_scripting = ] from_scripting ]
[ , [ @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' ]
[ ; ]
参数
[ @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为 sysname,默认值为 NULL. 如果密码为 NULL 空字符串, 则@password 重置为随机值。 必须在添加第一个远程分发服务器时配置密码。
distributor_admin登录名和@password存储用于分发服务器 RPC 连接的链接服务器条目,包括本地连接。 如果分发服务器是本地的,则distributor_admin的密码将设置为新值。 对于具有远程分发服务器的发布服务器,在发布服务器和分发服务器上执行时,必须指定@passwordsp_adddistributor。
sp_changedistributor_password可用于更改分发服务器密码。
重要
如果可能,请在运行时提示用户输入安全凭据。 如果必须在脚本文件中存储凭据,则必须保护文件以防止未经授权的访问。
[ @from_scripting = ] from_scripting
@from_scripting为位,默认值为 0. 标识为仅供参考。 不支持。 不保证以后的兼容性。
[ @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 optionalstrict
[ @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.
返回代码值
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。