配置发布和分发

本主题介绍如何使用 SQL Server Management Studio、Transact-SQL 或复制管理对象 (RMO) 在 SQL Server 2014 中配置发布和分发。

开始之前

安全性

有关详细信息,请参阅 安全复制部署

使用 SQL Server Management Studio

可以使用新建发布向导或配置分发向导配置分发。 配置分发服务器后,在“ 分发服务器属性 - <分发服务器> ”对话框中查看和修改属性。 如果要配置分发服务器以使 db_owner 固定数据库角色的成员可以创建发布,或者要配置一个非发布服务器的远程分发服务器,请使用配置分发向导。

配置分发

  1. 在 Microsoft SQL Server Management Studio 中,连接到将要作为分发服务器的服务器(许多情况下,发布服务器和分发服务器是同一服务器),然后展开该服务器节点。

  2. 右键单击 “复制” 文件夹,然后单击 “配置分发”

  3. 随着配置分发向导执行下列操作:

    • 选择分发服务器。 若要使用本地分发服务器,请选择“ServerName>”<将充当自己的分发服务器;SQL Server将创建分发数据库和日志。 若要使用远程分发服务器,请选择 “使用以下服务器作为分发服务器” ,再选择一个服务器。 该服务器必须已配置为分发服务器,并且启用发布服务器使用此分发服务器。 有关详细信息,请参阅在分发服务器 (SQL Server Management Studio) 启用远程发布服务器

      如果选择远程分发服务器,则必须在 “管理密码” 页上输入从发布服务器连接到分发服务器的密码。 此密码必须与在远程分发服务器上启用发布服务器时所指定的密码相匹配。

    • 指定根快照文件夹(适用于本地分发服务器)。 快照文件夹只是指定共享的目录。向此文件夹中执行读写操作的代理必须对其具有足够的访问权限。 每个使用此分发服务器的发布服务器都在根文件夹下创建一个文件夹,而每个发布则在发布服务器文件夹下创建用于存储快照文件的文件夹。 有关正确保护文件夹的详细信息,请参阅保护快照文件夹的安全

    • 指定分发数据库(适用于本地分发服务器)。 分发数据库存储事务性复制的所有复制和事务类型的元数据和历史记录数据。

    • 还可以让其他发布服务器使用该分发服务器(可选)。 如果其他发布服务器能够使用分发服务器,则必须在 “分发服务器密码” 页上输入从这些发布服务器连接到分发服务器的密码。

    • 还可以为配置设置编写脚本(可选)。 有关详细信息,请参阅 Scripting Replication

“使用 Transact-SQL”

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

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

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

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

    • 如果结果集中 安装的分发数据库 值为 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) 执行,以确定服务器是否已配置为分发服务器。

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

    • 如果结果集中 安装的分发数据库 值为 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。

示例 (Transact-SQL)

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

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

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

使用复制管理对象 (RMO)

在单个服务器上配置发布和分发

  1. 使用 ServerConnection 类创建与服务器的连接。

  2. 创建的 ReplicationServer 类的实例。 传递步骤 1 中的 ServerConnection

  3. 创建的 DistributionDatabase 类的实例。

  4. Name 属性设置为数据库名称,并将 ConnectionContext 属性设置为步骤 1 中的 ServerConnection

  5. 通过调用 InstallDistributor 方法安装分布服务器。 传递步骤 3 中的 DistributionDatabase 对象。

  6. 创建的 DistributionPublisher 类的实例。

  7. 设置 DistributionPublisher的以下属性:

  8. 调用 Create 方法。

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

  1. 使用 ServerConnection 类创建与远程分发服务器的连接。

  2. 创建的 ReplicationServer 类的实例。 传递步骤 1 中的 ServerConnection

  3. 创建的 DistributionDatabase 类的实例。

  4. Name 属性设置为数据库名称,并将 ConnectionContext 属性设置为步骤 1 中的 ServerConnection

  5. 通过调用 InstallDistributor 方法安装分布服务器。 指定安全密码(在连接到远程分发服务器时由发布服务器使用)和步骤 3 中的 DistributionDatabase 对象。 有关详细信息,请参阅保护分发服务器的安全

    重要

    如果可能,请在运行时提示用户输入安全凭据。 如果必须存储凭据,请使用 Windows .NET Framework 提供的 Cryptographic Services Microsoft (加密服务)。

  6. 创建的 DistributionPublisher 类的实例。

  7. 设置 DistributionPublisher的以下属性:

  8. 调用 Create 方法。

  9. 使用 ServerConnection 类创建与本地发布服务器的连接。

  10. 创建的 ReplicationServer 类的实例。 传递步骤 9 中的 ServerConnection

  11. 调用 InstallDistributor 方法。 传递远程分发服务器的名称和在步骤 5 中指定的远程分发服务器的密码。

    重要

    如果可能,请在运行时提示用户输入安全凭据。 如果必须存储凭据,请使用 Windows .NET Framework 提供的 Cryptographic Services (加密服务)。

示例 (RMO)

可以通过使用复制管理对象 (RMO) 以编程方式配置复制发布和分发。

// Set the server and database names
string distributionDbName = "distribution";
string publisherName = publisherInstance;
string publicationDbName = "AdventureWorks2012";

DistributionDatabase distributionDb;
ReplicationServer distributor;
DistributionPublisher publisher;
ReplicationDatabase publicationDb;

// Create a connection to the server using Windows Authentication.
ServerConnection conn = new ServerConnection(publisherName);

try
{
    // Connect to the server acting as the Distributor 
    // and local Publisher.
    conn.Connect();

    // Define the distribution database at the Distributor,
    // but do not create it now.
    distributionDb = new DistributionDatabase(distributionDbName, conn);
    distributionDb.MaxDistributionRetention = 96;
    distributionDb.HistoryRetention = 120;

    // Set the Distributor properties and install the Distributor.
    // This also creates the specified distribution database.
    distributor = new ReplicationServer(conn);
    distributor.InstallDistributor((string)null, distributionDb);

    // Set the Publisher properties and install the Publisher.
    publisher = new DistributionPublisher(publisherName, conn);
    publisher.DistributionDatabase = distributionDb.Name;
    publisher.WorkingDirectory = @"\\" + publisherName + @"\repldata";
    publisher.PublisherSecurity.WindowsAuthentication = true;
    publisher.Create();

    // Enable AdventureWorks2012 as a publication database.
    publicationDb = new ReplicationDatabase(publicationDbName, conn);

    publicationDb.EnabledTransPublishing = true;
    publicationDb.EnabledMergePublishing = true;
}
catch (Exception ex)
{
    // Implement appropriate error handling here.
    throw new ApplicationException("An error occured when installing distribution and publishing.", ex);
}
finally
{
    conn.Disconnect();
}
' Set the server and database names
Dim distributionDbName As String = "distribution"
Dim publisherName As String = publisherInstance
Dim publicationDbName As String = "AdventureWorks2012"

Dim distributionDb As DistributionDatabase
Dim distributor As ReplicationServer
Dim publisher As DistributionPublisher
Dim publicationDb As ReplicationDatabase

' Create a connection to the server using Windows Authentication.
Dim conn As ServerConnection = New ServerConnection(publisherName)

Try
    ' Connect to the server acting as the Distributor 
    ' and local Publisher.
    conn.Connect()

    ' Define the distribution database at the Distributor,
    ' but do not create it now.
    distributionDb = New DistributionDatabase(distributionDbName, conn)
    distributionDb.MaxDistributionRetention = 96
    distributionDb.HistoryRetention = 120

    ' Set the Distributor properties and install the Distributor.
    ' This also creates the specified distribution database.
    distributor = New ReplicationServer(conn)
    distributor.InstallDistributor((CType(Nothing, String)), distributionDb)

    ' Set the Publisher properties and install the Publisher.
    publisher = New DistributionPublisher(publisherName, conn)
    publisher.DistributionDatabase = distributionDb.Name
    publisher.WorkingDirectory = "\\" + publisherName + "\repldata"
    publisher.PublisherSecurity.WindowsAuthentication = True
    publisher.Create()

    ' Enable AdventureWorks2012 as a publication database.
    publicationDb = New ReplicationDatabase(publicationDbName, conn)

    publicationDb.EnabledTransPublishing = True
    publicationDb.EnabledMergePublishing = True

Catch ex As Exception
    ' Implement appropriate error handling here.
    Throw New ApplicationException("An error occured when installing distribution and publishing.", ex)

Finally
    conn.Disconnect()

End Try

另请参阅

查看和修改分发服务器和发布服务器属性
Replication System Stored Procedures Concepts
配置分发
复制管理对象概念
为 AlwaysOn 可用性组配置复制 (SQL Server)