設定發行和散發

本主題描述如何使用 SQL Server Management Studio、Transact-SQL 或 Replication Management Objects (RMO),在 SQL Server 2012 中設定發行和散發。

本主題內容

  • 開始之前:

    安全性

  • 若要設定發行和散發,請使用:

    SQL Server Management Studio

    Transact-SQL

    Replication Management Objects (RMO)

開始之前

安全性

如需詳細資訊,請參閱<安全的部署 (複寫)>。

搭配回到頁首連結使用的箭頭圖示[回到頁首]

使用 SQL Server Management Studio

使用「新增發行集精靈」或「設定散發精靈」來設定散發。 設定「散發者」後,檢視並修改在 [散發者屬性 - <Distributor>] 對話方塊中的屬性。 如果您想設定「散發者」以便 db_owner 固定資料庫角色的成員可以建立發行集,或者因為您想設定非「發行者」的遠端「散發者」,則請使用「設定散發精靈」。

若要設定散發

  1. 在 Microsoft SQL Server Management Studio 中,連接到將成為「散發者」的伺服器 (在許多情況下,「發行者」與「散發者」是同一個伺服器),然後展開伺服器節點。

  2. 在以滑鼠右鍵按一下 [複寫] 資料夾,然後按一下 [設定散發]

  3. 遵循「設定散發精靈」的指示執行下列項目:

    • 選取「散發者」。 若要使用本機「散發者」,請選取 ['<ServerName>' 將扮演本身的散發者; SQL Server 將建立散發資料庫和記錄]。 若要使用遠端散發者,請選取 [使用下列伺服器做為散發者],然後選取伺服器。 伺服器必須已設定為散發者,且必須先啟用發行者才能使用散發者。 如需詳細資訊,請參閱<在散發者端啟用遠端發行者 (SQL Server Management Studio)>。

      如果您選取遠端「散發者」,則必須在 [管理密碼] 頁面上輸入密碼才能從「發行者」連接到「散發者」。 此密碼必須符合發行者於遠端散發者啟用時所指定的密碼。

    • 指定根快照集資料夾 (針對本機「散發者」)。 快照集資料夾只是您指定為共用的目錄;從這個資料夾讀取與寫入這個資料夾的代理程式必須擁有存取資料夾的足夠權限。 使用此「散發者」的每個「發行者」在根資料夾下建立資料夾,每個發行集在要儲存快照集檔案的「發行者」資料夾下建立資料夾。 如需有關適當設定資料夾安全性的資訊,請參閱<保護快照集資料夾>。

    • 指定散發資料庫 (針對本機「散發者」)。 散發資料庫為異動複寫中所有類型的複寫與交易,儲存中繼資料和記錄資料。

    • 選擇性地啟用其他「發行者」來使用「散發者」。 如果啟用其他「發行者」來使用「散發者」,則必須在 [散發者密碼] 頁面上輸入密碼才能從這些「發行者」連接到「散發者」。

    • 選擇性地編寫組態設定的指令碼。 如需詳細資訊,請參閱<編寫複寫指令碼>。

搭配回到頁首連結使用的箭頭圖示[回到頁首]

使用 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 的值。

範例 (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 Management Objects 概念。

設定 AlwaysOn 可用性群組的複寫 (SQL Server)