本主題描述如何使用 SQL Server Management Studio、Transact-SQL 或 Replication Management Objects (RMO) 在 SQL Server 2014 中設定發行和散發。
開始之前
安全
如需詳細資訊,請參閱 安全複寫部署。
使用 SQL Server Management Studio
使用 [新建發佈嚮導] 或 [設定分發嚮導] 來配置分發。 設定散發者之後,請在 [ 散發者屬性 - <散發者> ] 對話框中檢視和修改屬性。 如果您想要設定散發者,請使用 [設定散發精靈],讓 db_owner 固定資料庫角色的成員可以建立發行集,或因為您想要設定非發行者的遠端散發者。
設定分配
在 Microsoft SQL Server Management Studio 中,連接到將成為散發者的伺服器(在許多情況下,發行者和散發者是相同的伺服器),然後展開伺服器節點。
以滑鼠右鍵單擊 [複寫] 資料夾,然後單擊 [配置散佈]。
遵循配置分配精靈的指引:
選取經銷商。 若要使用本機散發者,請選取 [<ServerName>] 作為自己的散發者;SQL Server 會建立散發資料庫和記錄。 若要使用遠端散發者,請選取 [使用下列伺服器作為散發者],然後選取伺服器。 伺服器必須已設定為「發行者」,且「出版者」必須已啟用才能使用「發行者」。 如需詳細資訊,請參閱在散發者端啟用遠端發行者(SQL Server Management Studio)。
如果您選取遠端散發者,則必須在 [ 系統管理密碼 ] 頁面上輸入密碼,以取得從發行者到散發者的連線。 此密碼必須符合在遠端散發者端啟用發行者時所指定的密碼。
指定本機發行者使用的根目錄快照資料夾。 快照集資料夾只是您指定為共享的目錄;讀取和寫入此資料夾的代理程式必須有足夠的許可權才能存取它。 每個使用此分發者的發佈者都會在根資料夾下建立一個資料夾,而每個出版物會在發佈者的資料夾下建立資料夾以儲存快照檔案。 如需適當保護資料夾的詳細資訊,請參閱 保護快照集資料夾。
指定分發資料庫(針對本機分發者)。 散發資料庫會儲存所有類型的複寫與交易式複寫的元數據和歷史資料。
可以選擇讓其他發行者使用分發者。 如果其他發行者已啟用使用散發者,您必須在 [散發者 密碼 ] 頁面上輸入密碼,以取得從這些發行者到散發者的連線。
選擇性地編寫組態設定的腳本。 如需詳細資訊,請參閱 Scripting Replication。
使用 Transact-SQL
可以透過複寫預存程序,以程式化的方式設定複寫發行和散佈。
使用本機分發者設定發佈流程
執行 sp_get_distributor (Transact-SQL), 以判斷伺服器是否已設定為散發者。
如果結果集中安裝的值為0,請在 master 資料庫的分發者端執行sp_adddistributor(Transact-SQL)。
如果結果集中已安裝的散發資料庫的值為0,請在 master 資料庫的散發者上執行sp_adddistributiondb (Transact-SQL)。 為 @database指定分發資料庫的名稱。 您可以選擇性地指定 @max_distretention 的事務保留期間上限,以及 @history_retention的歷程記錄保留期間。 如果要建立新的資料庫,請指定所需的資料庫屬性參數。
在「散發者」,也就是「發行者」端,執行 sp_adddistpublisher(Transact-SQL),指定將作為 @working_directory的默認快照集資料夾使用的 UNC 共用。
在發行者端,執行 sp_replicationdboption (Transact-SQL) 。 指定要針對@dbname發行的資料庫、@optname的復寫類型,以及 @value的值
true。
使用遠端散發者設定發佈
執行 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的歷程記錄保留期間。 如果要建立新的資料庫,請指定所需的資料庫屬性參數。
在散發者上,執行 sp_adddistpublisher (Transact-SQL),指定將作為 @working_directory預設快照集資料夾的 UNC 共用。 如果散發者在連接到發行者時會使用 SQL Server 驗證,您也必須為 @security_mode 指定 0 值,以及@login和@password 的 Microsoft SQL Server 登入資訊。
在 master 資料庫的發行者上,執行 sp_adddistributor (Transact-SQL) 。 指定步驟 1 中用於 @password的強密碼。 發行者連接到分銷者時會使用此密碼。
在發行者端,執行 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)
在單一伺服器上配置發佈和散發功能
使用 ServerConnection 類別建立與伺服器的連線。
建立 ReplicationServer 類別的執行個體。 傳遞步驟 1 的 ServerConnection 。
建立 DistributionDatabase 類別的執行個體。
將 Name 屬性設定為資料庫名稱,並將 ConnectionContext 屬性設定為步驟 1 中的 ServerConnection。
使用InstallDistributor方法安裝分配器。 傳遞步驟 3 中的 DistributionDatabase 物件。
建立 DistributionPublisher 類別的執行個體。
設定 DistributionPublisher 的下列屬性:
Name - 發行者的名稱。
ConnectionContext - 步驟 1 中的 ServerConnection 。
DistributionDatabase - 步驟 5 中建立的資料庫名稱。
WorkingDirectory - 用於存取快照檔案的共享。
PublisherSecurity - 連接到發行者時所使用的安全性模式。 建議使用WindowsAuthentication 。
呼叫 Create 方法。
使用遠端分發伺服器設定發佈和分發
使用 ServerConnection 類別建立遠端散發者伺服器的連線。
建立 ReplicationServer 類別的執行個體。 傳遞步驟 1 的 ServerConnection 。
建立 DistributionDatabase 類別的執行個體。
將 Name 屬性設定為資料庫名稱,並將 ConnectionContext 屬性設定為步驟 1 中的 ServerConnection。
呼叫 InstallDistributor 方法來安裝發行者。 請指定一個安全密碼,用於發行者連接到遠端分發者時,以及步驟 3 中的 DistributionDatabase 物件。 如需詳細資訊,請參閱保護散發者。
這很重要
可能的話,會在執行階段提示使用者輸入安全性認證。 如果您必須儲存認證,請使用 Microsoft Windows .NET Framework 提供的密碼編譯服務。
建立 DistributionPublisher 類別的執行個體。
設定 DistributionPublisher 的下列屬性:
Name - 本機發行者伺服器的名稱。
ConnectionContext - 步驟 1 中的 ServerConnection 。
DistributionDatabase - 步驟 5 中建立的資料庫名稱。
WorkingDirectory - 用於存取快照檔案的共享。
PublisherSecurity - 連接到發行者時所使用的安全性模式。 建議使用WindowsAuthentication 。
呼叫 Create 方法。
使用 ServerConnection 類別來建立與本機發行者伺服器的連線。
建立 ReplicationServer 類別的執行個體。 將第 9 步中的 ServerConnection 傳遞出去。
呼叫 InstallDistributor 方法。 傳遞遠端散發者的名稱,以及步驟 5 中所指定之遠端散發者的密碼。
這很重要
可能的話,會在執行階段提示使用者輸入安全性認證。 如果您必須儲存認證,請使用 Windows .NET Framework 提供的 密碼編譯服務 。
範例 (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
另請參閱
檢視及修改散發者和發行者屬性
復寫系統預存程式概念
設定散發
複寫管理物件概念
設定 SQL Server AlwaysOn 可用性群組的複寫配置