本主题介绍如何使用 SQL Server Management Studio、Transact-SQL 或复制管理对象(RMO)在 SQL Server 2014 中创建和应用初始快照。 使用参数化筛选器的合并出版物需要一个包含两部分的快照。 有关详细信息,请参阅使用 参数化筛选器为合并发布创建快照。
本主题内容
若要创建并应用初始快照,请使用:
使用 SQL Server Management Studio
默认情况下,如果 SQL Server 代理正在运行,则使用“新建发布向导”创建发布后,快照代理会立即生成快照。 默认情况下,它由分发代理(用于快照和事务复制)或合并代理(对于合并订阅)应用于所有订阅。 也可以使用 SQL Server Management Studio 和复制监视器生成快照。 有关启动复制监视器的信息,请参阅 “启动复制监视器”。
在 Management Studio 中创建快照
在 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
使用复制管理对象 (RMO)
快照代理在创建发布后生成快照。 通过使用复制管理对象(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 身份验证的值,或者Standard表示连接到分发服务器时使用 SQL Server 身份验证的值,并需要提供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,或使用值Standard、PublisherLogin和PublisherPassword在连接到发布服务器时使用 SQL Server 身份验证。 建议使用 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 与脚本变量结合使用