sp_adddistributor (Transact-SQL)

适用于:SQL ServerAzure SQL 托管实例

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

Transact-SQL 语法约定

语法

sp_adddistributor
    [ @distributor = ] N'distributor'
    [ , [ @heartbeat_interval = ] heartbeat_interval ]
    [ , [ @password = ] N'password' ]
    [ , [ @from_scripting = ] from_scripting ]
[ ; ]

参数

[ @distributor = ] N'distributor'

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

注意

服务器名称可以指定为 <Hostname>,<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的密码将设置为新值。 对于具有远程分发服务器的发布服务器,在发布服务器和分发服务器上执行sp_adddistributor时,必须指定@password的相同值 sp_changedistributor_password可用于更改分发服务器密码。

重要

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

[ @from_scripting = ] from_scripting

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

返回代码值

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

备注

sp_adddistributor用于快照 副本 (replica)、事务副本 (replica)和合并副本 (replica)。

示例

-- 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

权限

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