如何配置发布和分发(复制 Transact-SQL 编程)

可以使用复制存储过程以编程方式配置复制发布和分发。

使用本地分发服务器配置发布

  1. 执行sp_get_distributor (Transact-SQL)以确定服务器是否已配置为分发服务器。

    • 如果结果集中 installed 的值为 0,请在分发服务器上对 master 数据库执行 sp_adddistributor (Transact-SQL)

    • 如果结果集中 distribution db installed 的值为 0,请在分发服务器上对 master 数据库执行 sp_adddistributiondb (Transact-SQL)。为 @database 指定分发数据库的名称。或者,您可以选择为 @max_distretention 指定最长事务保持期,并为 @history_retention 指定历史记录保持期。如果要创建一个新的数据库,请指定所需的数据库属性参数。

  2. 在分发服务器(也是发布服务器)上,执行 sp_adddistpublisher (Transact-SQL),为 @working_directory 指定将作为默认快照文件夹的 UNC 共享目录。

  3. 在发布服务器上,执行 sp_replicationdboption (Transact-SQL)。为 @dbname 指定要发布的数据库,为 @optname 指定复制的类型,并将 @value 的值指定为 true。

使用远程分发服务器配置发布

  1. 执行 sp_get_distributor (Transact-SQL) 以确定服务器是否已配置为分发服务器。

    • 如果结果集中 installed 的值为 0,请在分发服务器上对 master 数据库执行 sp_adddistributor (Transact-SQL)。为 @password 指定强密码。发布服务器在连接到分发服务器时将使用 distributor_admin 帐户的此密码。

    • 如果结果集中 distribution db installed 的值为 0,请在分发服务器上对 master 数据库执行 sp_adddistributiondb (Transact-SQL)。为 @database 指定分发数据库的名称。或者,您可以选择为 @max_distretention 指定最长事务保持期,并为 @history_retention 指定历史记录保持期。如果要创建一个新的数据库,请指定所需的数据库属性参数。

  2. 在分发服务器上,执行 sp_adddistpublisher (Transact-SQL),为 @working_directory 指定将作为默认快照文件夹的 UNC 共享目录。如果分发服务器在连接到发布服务器时将使用 SQL Server 身份验证,则还必须将 @security_mode 的值指定为 0,并为 @login@password 指定 Microsoft SQL Server 登录信息。

  3. 在发布服务器上,对 master 数据库执行 sp_adddistributor (Transact-SQL)。为 @password 指定步骤 1 中使用的强密码。发布服务器在连接到分发服务器时将使用此密码。

  4. 在发布服务器上,执行 sp_replicationdboption (Transact-SQL)。为 @dbname 指定要发布的数据库,为 @optname 指定复制的类型,并将 @value 的值指定为 true。

示例

下面的示例说明了如何以编程方式配置发布和分发。在本示例中,使用脚本变量提供要配置为发布服务器和本地分发服务器的服务器的名称。

-- 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'AdventureWorks2008R2'; 

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