設定發行和散發

本主題描述如何使用 SQL Server Management Studio、Transact-SQL 或 Replication Management Objects (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 

使用 Replication Management Objects (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 物件。 如需詳細資訊,請參閱保護散發者

    重要

    可能的話,會在執行階段提示使用者輸入安全性認證。 如果您必須儲存認證,請使用 Microsoft Windows .NET Framework 提供的密碼編譯服務

  6. 建立 DistributionPublisher 類別的執行個體。

  7. 設定 DistributionPublisher的以下屬性:

  8. 呼叫 Create 方法。

  9. 使用 ServerConnection 類別建立與本機發行者伺服器的連接。

  10. 建立 ReplicationServer 類別的執行個體。 傳遞步驟 9 的 ServerConnection

  11. 呼叫 InstallDistributor 方法。 傳遞遠端散發者的名稱以及步驟 5 中指定之遠端散發者的密碼。

    重要

    可能的話,會在執行階段提示使用者輸入安全性認證。 如果您必須儲存認證,請使用 Windows .NET Framework 提供的 密碼編譯服務

範例 (RMO)

您可以使用 Replication Management Objects (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)