sp_adddistpublisher (Transact-SQL)

适用于:SQL ServerAzure SQL 托管实例

配置发布服务器以使用指定的分发数据库。 此存储过程在分发服务器上的任何数据库中执行。 存储过程 sp_adddistributor(Transact-SQL)sp_adddistributiondb(Transact-SQL) 必须在使用此存储过程之前运行。

Transact-SQL 语法约定

语法

sp_adddistpublisher
    [ @publisher = ] N'publisher'
    , [ @distribution_db = ] N'distribution_db'
    [ , [ @security_mode = ] security_mode ]
    [ , [ @login = ] N'login' ]
    [ , [ @password = ] N'password' ]
    [ , [ @working_directory = ] N'working_directory' ]
    [ , [ @trusted = ] N'trusted' ]
    [ , [ @encrypted_password = ] encrypted_password ]
    [ , [ @thirdparty_flag = ] thirdparty_flag ]
    [ , [ @publisher_type = ] N'publisher_type' ]
    [ , [ @storage_connection_string = ] N'storage_connection_string' ]
[ ; ]

参数

[ @publisher = ] N'publisher'

发布服务器名称。 @publisher为 sysname,无默认值。

注意

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

[ @distribution_db = ] N'distribution_db'

分发数据库的名称。 @distribution_db为 sysname,无默认值。 复制代理使用该参数连接到发布服务器。

[ @security_mode = ] security_mode

实现的安全模式。 此参数仅供副本 (replica)代理用于连接到发布服务器,以便排队更新订阅或非 SQL Server 发布服务器。 @security_modeint,可以是这些值之一。

说明
0 分发服务器上的复制代理使用 SQL Server 身份验证连接到发布服务器。
1(默认值) 分发服务器中的复制代理使用 Windows 身份验证连接到发布服务器。

[ @login = ] N'login'

登录名。 如果 security_mode0,则此参数是必需的。 @login为 sysname,默认值为 NULL. 复制代理使用该参数连接到发布服务器。

[ @password = ] N'password'

密码。 @password为 sysname,默认值为 NULL. 复制代理使用该参数连接到发布服务器。

重要

不要使用空白密码。 请使用强密码。

[ @working_directory = ] N'working_directory'

用于存储发布数据和架构文件的工作目录的名称。 @working_directory为 nvarchar(255),默认为ReplData此 SQL Server 实例的文件夹。 例如 C:\Program Files\Microsoft SQL Server\MSSQL\MSSQL.1\ReplData。 名称应按 UNC 格式指定。

对于Azure SQL 数据库,请使用 \\<storage_account>.file.core.windows.net\<share>

[ @trusted = ] N'trusted'

@trusted已弃用,仅用于向后兼容。 @trusted为 nvarchar(5),默认值为 false. 将此参数设置为任何参数,但 false 会导致错误。

[ @encrypted_password = ] encrypted_password

不再支持设置此参数。 @encrypted_password为,默认值为 0. 将此参数设置为 1 导致错误。

[ @thirdparty_flag = ] thirdparty_flag

指定发布服务器何时为 SQL Server。 @thirdparty_flag位,可以是以下值之一。

说明
0(默认值) SQL Server 数据库。
1 SQL Server 以外的数据库。

[ @publisher_type = ] N'publisher_type'

指定发布服务器不是 SQL Server 时的发布服务器类型。 @publisher_typesysname,可以是以下值之一。

说明
MSSQLSERVER(默认值) 指定 SQL Server 发布服务器。
ORACLE 指定标准的 Oracle 发布服务器。
ORACLE GATEWAY 指定 Oracle 网关发布服务器。

有关 Oracle 发布服务器与 Oracle 网关发布服务器之间的差异的详细信息,请参阅 配置 Oracle 发布服务器

[ @storage_connection_string = ] N'storage_connection_string'

Azure SQL 数据库是必需的。 @storage_connection_string为 nvarchar(255),默认值为 NULL. 在存储设置下>,使用Azure 门户的访问密钥。

注意

Azure SQL 数据库上的发布服务器和分发服务器数据库需要SQL 托管实例。 有关详细信息,请参阅复制和Azure SQL 数据库

返回代码值

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

注解

sp_adddistpublisher由快照 副本 (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_adddistpublisher