本主題描述如何使用 SQL Server Management Studio、Transact-SQL 或 Replication Management Objects (RMO) 在 SQL Server 2014 中建立和套用初始快照集。 使用參數化篩選的合併式發行集需要兩部分快照集。 如需詳細資訊,請參閱 建立具有參數化篩選的合併發行快照。
本主題內容
建立並套用初始快照:
使用 SQL Server Management Studio
根據預設,若 SQL Server Agent 正在執行,則當使用 [新增發行集精靈] 創建發行集後,快照代理程式會立即產生快照。 依預設,散發代理程式(針對快照式和事務複製)或合併代理程式(針對合併訂閱)會將其套用到所有訂閱。 您也可以使用 SQL Server Management Studio 和複寫監視器來產生快照集。 如需啟動複寫監視器的詳細資訊,請參閱啟動複寫監視器。
在 Management Studio 中建立快照
連線至 Microsoft SQL Server Management Studio 中的發行者,然後展開伺服器節點。
展開 複寫 資料夾,然後展開 本機發行集 資料夾。
以滑鼠右鍵按下您要建立快照集的出版物,然後按 [檢查快照集代理程序狀態]。
在 [ 檢視快照集代理程序狀態 - <發行集> ] 對話框中,按兩下 [ 啟動]。
當快照代理程式完成產生快照時,將會顯示訊息,例如“[100%] 產生 17 個發行項的快照”。
在復寫監視器中建立快照
在複寫監視器的左窗格中展開發行者群組,然後展開發行者。
以滑鼠右鍵按下您要產生快照集的發行集,然後按兩下 [ 產生快照集]。
若要檢視快照集代理程序的狀態,請按兩下 [ 代理程式] 索引標籤 。如需詳細資訊,請以滑鼠右鍵按下方格中的快照集代理程式,然後按兩下 [ 檢視詳細數據]。
套用快照
產生快照集之後,它會藉由與散發代理程式或合併代理程式同步訂閱來套用。
如果代理程式設定為持續執行 (事務複製的預設值),則快照集會在產生之後自動套用。
如果代理程式設定為依排程執行,則下次排程代理程式執行時,就會套用快照。
如果代理程式設定為視需要執行,則會在下一次執行代理程式時套用它。
使用 Transact-SQL
初始快照集可以透過程序設計方式建立,方法是建立和執行快照集代理程序作業,或從批處理檔執行快照集代理程式可執行檔。 初始快照集產生後,會在訂閱首次同步處理時傳送至訂閱者並予以套用。 如果您從命令提示字元或批處理檔執行快照集代理程式,則必須在現有快照集變成無效時重新執行代理程式。
這很重要
可能的話,會在執行階段提示使用者輸入安全性認證。 如果您必須將認證儲存在指令碼檔案中,則必須維護這個檔案的安全性,使他人無法在未獲授權的情況下擅自存取。
若要建立和執行快照代理程式任務,以產生初始快照
建立快照式、交易式或合併式發行集。 如需詳細資訊,請參閱建立出版物。
執行 sp_addpublication_snapshot (Transact-SQL) 。 指定 @publication 與下列參數:
, @job_login指定 快照集代理程式在散發者端執行所在的 Windows 驗證認證。
@job_password****,這是所提供 Windows 認證的密碼。
(選擇性)如果代理程式在連接到發行者時使用 SQL Server 驗證,則 @publisher_security_mode 的值應為 0。 在此情況下,您也必須為 @publisher_login 和 @publisher_password指定 SQL Server 驗證登入資訊。
選擇性)快照代理程式作業的同步排程。 如需詳細資訊,請參閱 指定同步處理排程。
這很重要
當利用遠端散發者來設定發行者時,提供給所有參數的值 (包括 job_login 和 job_password) 都會以純文字的方式傳給散發者。 您應該先加密「發行者」及其遠端「散發者」之間的連接,再執行這個預存程序。 如需詳細資訊,請參閱啟用資料庫引擎的加密連線 (SQL Server 組態管理員)。
將文章新增到出版物中。 如需詳細資訊,請參閱 定義發行項。
在發布出版物資料庫的發布者上,執行 sp_startpublication_snapshot (Transact-SQL),指定步驟 1 中的 @publication 值。
若要執行快照代理程式來產生初始快照
建立快照式發行集、交易式發行集或合併式發行集。 如需詳細資訊,請參閱建立出版物。
將文章新增到出版物中。 如需詳細資訊,請參閱 定義發行項。
從命令提示字元或在批處理檔中,執行 snapshot.exe啟動復寫快照集代理程式,並指定下列命令行自變數:
-出版
-發行人
-分配器
-PublisherDB
-複製類型
如果您使用 SQL Server 驗證,您也必須指定下列自變數:
-DistributorLogin
-分銷商密碼
-DistributorSecurityMode = 0
-PublisherLogin
-PublisherPassword
-PublisherSecurityMode = 0
範例 (Transact-SQL)
此範例示範如何建立交易式發行集,並為新發行集新增快照集代理作業(使用sqlcmd腳本變數)。 此範例也會啟動作業。
-- To avoid storing the login and password in the script file, the values
-- are passed into SQLCMD as scripting variables. 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".
DECLARE @publicationDB AS sysname;
DECLARE @publication AS sysname;
DECLARE @login AS sysname;
DECLARE @password AS sysname;
SET @publicationDB = N'AdventureWorks2012'; --publication database
SET @publication = N'AdvWorksCustomerTran'; -- transactional publication name
SET @login = $(Login);
SET @password = $(Password);
USE [AdventureWorks]
-- Enable transactional and snapshot replication on the publication database.
EXEC sp_replicationdboption
@dbname = @publicationDB,
@optname = N'publish',
@value = N'true';
-- Execute sp_addlogreader_agent to create the agent job.
EXEC sp_addlogreader_agent
@job_login = @login,
@job_password = @password,
-- Explicitly specify the security mode used when connecting to the Publisher.
@publisher_security_mode = 1;
-- Create new transactional publication, using the defaults.
USE [AdventureWorks2012]
EXEC sp_addpublication
@publication = @publication,
@description = N'transactional publication';
-- Create a new snapshot job for the publication, using the defaults.
EXEC sp_addpublication_snapshot
@publication = @publication,
@job_login = @login,
@job_password = @password;
-- Start the Snapshot Agent job.
EXEC sp_startpublication_snapshot @publication = @publication;
GO
此範例會建立合併式發行集,併為發行集新增快照集代理程序作業(使用 sqlcmd 變數)。 此範例也會啟動作業。
-- To avoid storing the login and password in the script file, the value
-- is passed into SQLCMD as a scripting variable. 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".
DECLARE @publicationDB AS sysname;
DECLARE @publication AS sysname;
DECLARE @login AS sysname;
DECLARE @password AS sysname;
SET @publicationDB = N'AdventureWorks2012';
SET @publication = N'AdvWorksSalesOrdersMerge';
SET @login = $(Login);
SET @password = $(Password);
-- Enable merge replication on the publication database.
USE master
EXEC sp_replicationdboption
@dbname = @publicationDB,
@optname=N'merge publish',
@value = N'true';
-- Create new merge publication, using the defaults.
USE [AdventureWorks]
EXEC sp_addmergepublication
@publication = @publication,
@description = N'Merge publication.';
-- Create a new snapshot job for the publication, using the defaults.
EXEC sp_addpublication_snapshot
@publication = @publication,
@job_login = @login,
@job_password = @password;
-- Start the Snapshot Agent job.
EXEC sp_startpublication_snapshot @publication = @publication;
GO
下列命令列引數會啟動快照代理程式,以產生合併發行的快照。
備註
新增換行符以改善可讀性。 在批處理檔中,命令必須在單行中建立。
@ECHO OFF
SET InstanceName=%computername%
REM<snippetstartmergesnapshot_10>
REM -- Declare variables
SET Publisher=%InstanceName%
SET PublicationDB=AdventureWorks2012
SET Publication=AdvWorksSalesOrdersMerge
REM --Start the Snapshot Agent to generate the snapshot for AdvWorksSalesOrdersMerge.
"C:\Program Files\Microsoft SQL Server\120\COM\SNAPSHOT.EXE" -Publication %Publication%
-Publisher %Publisher% -Distributor %Publisher% -PublisherDB %PublicationDB%
-ReplicationType 2 -OutputVerboseLevel 1 -DistributorSecurityMode 1
REM</snippetstartmergesnapshot_10>
PAUSE
使用 Replication Management Objects (RMO)
Snapshot代理程式會在建立出版物之後生成快照。 您可以使用複寫管理物件(RMO)以及直接使用受管理代碼來取用複寫代理程式功能,以程式化地產生這些快照集。 您使用的物件取決於複寫的類型。 快照代理程式可以使用SnapshotGenerationAgent物件以同步方式啟動,或使用代理程式作業以異步方式啟動。 產生初始快照之後,訂閱首次同步時,它會被傳送至訂閱者並且被套用。 每當現有的快照集不再包含有效的 up-to日期數據時,您必須重新執行代理程式。 如需詳細資訊,請參閱 維護出版物。
這很重要
可能的話,會在執行階段提示使用者輸入安全性認證。 如果您必須儲存認證,請使用 Microsoft Windows .NET Framework 提供的密碼編譯服務。
若要透過啟動快照代理程式工作(異步)來產生快照集或交易式發行集的初始快照集。
使用 ServerConnection 類別建立與發行者的連接。
建立 TransPublication 類別的執行個體。 設定出版物的 Name 和 DatabaseName 屬性,並將 ConnectionContext 屬性設定為步驟 1 中建立的連接。
LoadProperties呼叫 方法以載入 物件的其餘屬性。 如果此方法傳
false回 ,則表示步驟 2 中的發行集屬性定義不正確或發行集不存在。若 SnapshotAgentExists 的值是
false,請呼叫 CreateSnapshotAgent 以建立此快照代理程序工作。請呼叫 StartSnapshotGenerationAgentJob 方法以啟動代理程序作業以產生此出版物的快照。
(選擇性)當 SnapshotAvailable 的數值是
true時,快照可供訂閱者使用。
若要執行快照集代理程式來產生快照集或交易式發行集的初始快照集 (同步)
建立 類別的 SnapshotGenerationAgent 實例,並設定下列必要屬性:
Publisher - 發行者的名稱
PublisherDatabase - 出版資料庫的名稱
Publication - 出版物的名稱
Distributor - 散發者的名稱
PublisherSecurityMode- 連接到發行者時使用 Windows 驗證的值Integrated,或使用 SQL Server 驗證時Standard、PublisherLogin 和 PublisherPassword的值。 建議使用 Windows 驗證。
DistributorSecurityMode - 當連接到散發者時,若使用 Windows 驗證,則需要值Integrated;若使用 SQL Server 驗證,則需要值Standard以及DistributorLogin與DistributorPassword。 建議使用 Windows 驗證。
呼叫 GenerateSnapshot 方法。
若要啟動快照代理程式作業(異步)來生成合併發行的初始快照。
使用 ServerConnection 類別建立與發行者的連接。
建立 MergePublication 類別的執行個體。 設定出版物的 Name 和 DatabaseName 屬性,並將 ConnectionContext 屬性設定為步驟 1 中建立的連接。
LoadProperties呼叫 方法以載入 物件的其餘屬性。 如果此方法傳
false回 ,則表示步驟 2 中的發行集屬性定義不正確或發行集不存在。若 SnapshotAgentExists 的值是
false,請呼叫 CreateSnapshotAgent 以建立此快照代理程序工作。呼叫 StartSnapshotGenerationAgentJob 方法來啟動生成此發行集的快照之代理作業。
(選擇性)當SnapshotAvailable的值為
true時,快照集可供訂閱者使用。
若要建立初始快照以用於合併發行集,請執行快照代理程式(同步)
建立 類別的 SnapshotGenerationAgent 實例,並設定下列必要屬性:
Publisher - 發行者的名稱
PublisherDatabase - 出版資料庫的名稱
Publication - 出版物的名稱
Distributor - 散發者的名稱
PublisherSecurityMode - 當連接到發行者時,若要使用 Windows 驗證,請使用值 Integrated;若要使用 SQL Server 驗證,請使用值 Standard,以及 PublisherLogin 和 PublisherPassword 的值。 建議使用 Windows 驗證。
DistributorSecurityMode- 在連接到散發者時,使用 Windows 驗證的值Integrated,或在連接到散發者時,使用 SQL Server 驗證的值Standard與對應的DistributorLogin和DistributorPassword值。 建議使用 Windows 驗證。
為Merge設定值ReplicationType。
呼叫 GenerateSnapshot 方法。
範例 (RMO)
這個範例會同時運行快照代理程式,以產生交易發行的初始快照集。
// Set the Publisher, publication database, and publication names.
string publicationName = "AdvWorksProductTran";
string publicationDbName = "AdventureWorks2012";
string publisherName = publisherInstance;
string distributorName = publisherInstance;
SnapshotGenerationAgent agent;
try
{
// Set the required properties for Snapshot Agent.
agent = new SnapshotGenerationAgent();
agent.Distributor = distributorName;
agent.DistributorSecurityMode = SecurityMode.Integrated;
agent.Publisher = publisherName;
agent.PublisherSecurityMode = SecurityMode.Integrated;
agent.Publication = publicationName;
agent.PublisherDatabase = publicationDbName;
agent.ReplicationType = ReplicationType.Transactional;
// Start the agent synchronously.
agent.GenerateSnapshot();
}
catch (Exception ex)
{
// Implement custom application error handling here.
throw new ApplicationException(String.Format(
"A snapshot could not be generated for the {0} publication."
, publicationName), ex);
}
' Set the Publisher, publication database, and publication names.
Dim publicationName As String = "AdvWorksProductTran"
Dim publicationDbName As String = "AdventureWorks2012"
Dim publisherName As String = publisherInstance
Dim distributorName As String = publisherInstance
Dim agent As SnapshotGenerationAgent
Try
' Set the required properties for Snapshot Agent.
agent = New SnapshotGenerationAgent()
agent.Distributor = distributorName
agent.DistributorSecurityMode = SecurityMode.Integrated
agent.Publisher = publisherName
agent.PublisherSecurityMode = SecurityMode.Integrated
agent.Publication = publicationName
agent.PublisherDatabase = publicationDbName
agent.ReplicationType = ReplicationType.Transactional
' Start the agent synchronously.
agent.GenerateSnapshot()
Catch ex As Exception
' Implement custom application error handling here.
Throw New ApplicationException(String.Format( _
"A snapshot could not be generated for the {0} publication." _
, publicationName), ex)
End Try
這個範例會以異步方式啟動代理工作,以產生交易式的發行的初始快照。
// Set the Publisher, publication database, and publication names.
string publicationName = "AdvWorksProductTran";
string publicationDbName = "AdventureWorks2012";
string publisherName = publisherInstance;
TransPublication publication;
// Create a connection to the Publisher using Windows Authentication.
ServerConnection conn;
conn = new ServerConnection(publisherName);
try
{
// Connect to the Publisher.
conn.Connect();
// Set the required properties for an existing publication.
publication = new TransPublication();
publication.ConnectionContext = conn;
publication.Name = publicationName;
publication.DatabaseName = publicationDbName;
if (publication.LoadProperties())
{
// Start the Snapshot Agent job for the publication.
publication.StartSnapshotGenerationAgentJob();
}
else
{
throw new ApplicationException(String.Format(
"The {0} publication does not exist.", publicationName));
}
}
catch (Exception ex)
{
// Implement custom application error handling here.
throw new ApplicationException(String.Format(
"A snapshot could not be generated for the {0} publication."
, publicationName), ex);
}
finally
{
conn.Disconnect();
}
' Set the Publisher, publication database, and publication names.
Dim publicationName As String = "AdvWorksProductTran"
Dim publicationDbName As String = "AdventureWorks2012"
Dim publisherName As String = publisherInstance
Dim publication As TransPublication
' Create a connection to the Publisher using Windows Authentication.
Dim conn As ServerConnection
conn = New ServerConnection(publisherName)
Try
' Connect to the Publisher.
conn.Connect()
' Set the required properties for an existing publication.
publication = New TransPublication()
publication.ConnectionContext = conn
publication.Name = publicationName
publication.DatabaseName = publicationDbName
If publication.LoadProperties() Then
' Start the Snapshot Agent job for the publication.
publication.StartSnapshotGenerationAgentJob()
Else
Throw New ApplicationException(String.Format( _
"The {0} publication does not exist.", publicationName))
End If
Catch ex As Exception
' Implement custom application error handling here.
Throw New ApplicationException(String.Format( _
"A snapshot could not be generated for the {0} publication." _
, publicationName), ex)
Finally
conn.Disconnect()
End Try
另請參閱
創建出版物
建立提取訂閱
建立推送訂閱
指定同步處理排程
建立並套用快照集
使用快照初始化訂閱
複寫管理物件概念
複寫安全性最佳作法
復寫系統預存程式概念
搭配腳本變數使用 sqlcmd