使用SQL Server事务复制将SQL Server数据库迁移到Azure SQL数据库
In this guide, we’ll discuss more about migrating a SQL Server database to Azure SQL Database using SQL Server Transactional Replication.
在本指南中,我们将讨论有关使用SQL Server事务复制将SQL Server数据库迁移到Azure SQL数据库的更多信息。
With Azure SQL database, it is possible to replicate your on-premise database(s) with database(s) on the cloud in order to ensure application availability. In this article, we will see how we set up SQL Server transactional replication between an on-premise database and a database on Azure. We will deal with setting up SQL Server transactional replication—only supported feature of Azure SQL database.
使用Azure SQL数据库,可以将内部部署数据库与云上的数据库复制,以确保应用程序可用性。 在本文中,我们将了解如何在本地数据库和Azure上的数据库之间设置SQL Server事务复制。 我们将处理设置SQL Server事务复制-Azure SQL数据库仅受支持的功能。
数据库迁移 (Database Migration)
In this case, we are performing a database migration with a very short downtime using SQL Server transactional replication. I’ll discuss more in detail to setup and configure replication.
在这种情况下,我们将使用SQL Server事务复制在非常短的停机时间内执行数据库迁移。 我将详细讨论设置和配置复制。
-
Note:注意:
- Only transactional replication is supported on Azure SQL Database. Other types such as Peer-to-peer or Merge Replication topology aren’t supported Azure SQL数据库仅支持事务复制。 不支持其他类型,例如点对点或合并复制拓扑
- Azure SQL database can only be used as a subscriber. Setting up publications is not allowed on Azure Azure SQL数据库只能用作订阅服务器。 在Azure上不允许设置发布
先决条件 (Prerequisites )
To be able to set up SQL Server transactional replication on Azure, you need to have some familiarity with Microsoft SQL Server Transactional Replication as well as the Azure SQL database cloud services.
为了能够在Azure上设置SQL Server事务复制,您需要熟悉Microsoft SQL Server事务复制以及Azure SQL数据库云服务。
版本号 (Versions)
It is mandatory that you meet the following requirements in order to set up SQL Server transactional replication on Microsoft Azure SQL Database.
为了在Microsoft Azure SQL数据库上设置SQL Server事务复制,必须满足以下要求。
-
The publisher and distributor must at least be among the following version of SQL Server:
- SQL Server 2017
- SQL Server 2016
- SQL Server 2014 SP1 CU3
- SQL Server 2014 RTM CU10
- SQL Server 2012 SP2 CU8 or SP3
- SQL Server 2017
- SQL Server 2016
- SQL Server 2014 SP1 CU3
- SQL Server 2014 RTM CU10
- SQL Server 2012 SP2 CU8或SP3
- Use the latest versions of SQL Server Management Studio as well as SQL Server Data Tools in order to be able to use all the features of Azure SQL Database 使用最新版本SQL Server Management Studio以及SQL Server数据工具,以便能够使用Azure SQL数据库的所有功能
- You can use SQL Server Management Studio to configure replication, or execute T-SQL statements on the publisher. Azure Portal cannot be used to configure replication 您可以使用SQL Server Management Studio配置复制,或在发布服务器上执行T-SQL语句。 Azure门户不能用于配置复制
- The only way you can authenticate on Azure for replication is use SQL Server authentication logins 您可以在Azure上进行复制身份验证的唯一方法是使用SQL Server身份验证登录名
- Tables that are being replicated must have a primary key 要复制的表必须具有主键
- Of course, you should already have an Azure subscription 当然,您应该已经有一个Azure订阅
- The Azure SQL database subscriber is region-agnostic Azure SQL数据库订阅者与区域无关
- You can use a single publication on SQL Server, to support both, Azure as well as SQL Server (on-premises and SQL Server in an Azure virtual machine) subscriptions 您可以在SQL Server上使用单个发布来支持Azure和SQL Server(本地和Azure虚拟机中SQL Server)订阅
- The on-premise SQL Server must be used to manage, monitor and troubleshoot replication 本地SQL Server必须用于管理,监视复制并对其进行故障排除
- Only push subscriptions to Azure are supported 仅支持对Azure的推送订阅
- Only @subscriber_type = 0 is supported in the stored procedure, sp_addsubscription 存储过程sp_addsubscription中仅支持@subscriber_type = 0
- Bi-directional, immediate, updatable, or peer to peer replication are not supported by Azure SQL Database Azure SQL数据库不支持双向,即时,可更新或对等复制
数据库迁移方案 (Database Migration Scenario)
- Use SQL Server transactional replication to replicate data from your on-premise SQL Server database to Azure 使用SQL Server事务复制将数据从本地SQL Server数据库复制到Azure
- Redirect the client applications to update the Azure SQL database copy 重定向客户端应用程序以更新Azure SQL数据库副本
- Stop updating the SQL Server version of the table after the redirection by removing the publication 通过删除发布,重定向后停止更新表SQL Server版本
设置SQL Server事务复制 (Setting up SQL Server Transactional Replication)
Let’s set up SQL Server transactional replication to Azure. In this guide, I will use the Adventure Works database for setting up the replication from SQL Server to Azure SQL Database. You can download the adventureworks2016 sample database.
让我们设置SQL Server到Azure的事务复制。 在本指南中,我将使用Adventure Works数据库来设置从SQL Server到Azure SQL数据库的复制。 您可以下载Adventureworks2016示例数据库 。
In this article, I am going set up a SQL Server transactional replication by selecting a SQL Server 2016 as the publisher and distributor, and an Azure SQL Database as the subscriber. You can refer to the Prerequisites section for more details.
在本文中,我将通过选择SQL Server 2016作为发布者和分发者,并选择Azure SQL数据库作为订阅者来设置SQL Server事务复制。 您可以参考先决条件部分以了解更多详细信息。
First, let’s configure publisher and distributor on the SQL Server 2016 server. In the next section, you’ll see the steps to configure distributor.
首先,让我们在SQL Server 2016服务器上配置发布者和分发者。 在下一节中,您将看到配置分发服务器的步骤。
设定分布 (Setup Distribution)
The steps are no different than setting up a normal replication. You can refer to a previous article of mine to see the details about the distributor configuration.
这些步骤与设置普通复制没有什么不同。 您可以参考我的上一篇文章,以了解有关分发服务器配置的详细信息 。
Note: The default values are used in all pages. This way, it will be very easy and straight-forward to understand how to configure distribution database. You can then make changes according to your requirements, based on the understanding you gather this way.
注意:所有页面均使用默认值。 这样,将非常容易直接地了解如何配置分发数据库。 然后,您可以基于对这种方式的了解,根据需求进行更改。
-
Browse Object Explorer and locate the Replication folder. Right-click and select Configure Distribution
浏览对象资源管理器并找到复制文件夹。 右键单击并选择配置分发
- Now, it displays the Configure Distribution Wizard page. Click Next to proceed to the next window 现在,它将显示“配置分发向导”页面。 单击下一步进入下一个窗口
-
Next, as the setup is configured with local distributor, choose the default setting “AmbikaPrashanth will act as its own Distributor; SQL Server will create a distribution database and log” and Click Next
接下来,由于使用本地分发服务器配置了该设置,因此选择默认设置“ AmbikaPrashanth将充当其自己的分发服务器; SQL Server将创建一个分发数据库并记录”,然后单击“ 下一步”。
-
Configure the Snapshot Folder. As this is a demo setup, I’ll go with the default values and click Next
配置快照文件夹 。 因为这是一个演示设置,所以我将使用默认值,然后单击“ 下一步”。
-
In the Configure distribution database page, select the defaults and proceed by clicking Finish button
在“配置分发数据库”页面中,选择默认值,然后单击“ 完成”按钮继续。
-
In the Complete the wizard page, review the summary and then click Finish to complete the configuration of the distribution database
在“完成向导”页面中,查看摘要,然后单击“ 完成”以完成分发数据库的配置。
-
Next, in the confirmation page, you would see the progress of the entire distributor setup
接下来,在确认页面中,您将看到整个分销商设置的进度
配置发布者 (Configure Publisher)
In this section, we discuss setting up a publisher. As we know that during the setup, we can see a different type of replication that we can enable through the wizard.
在本节中,我们讨论设置发布者。 众所周知,在安装过程中,我们可以看到可以通过向导启用的另一种复制类型。
-
Browse Object Explorer, locate the Replication folder, right-click Local Publication, and then click New Publication
浏览对象资源管理器 ,找到“ 复制”文件夹,右键单击“ 本地 发布” ,然后单击“ 新建发布”。
-
In the Publication Wizard, click Next
在发布向导中 ,单击“下一步”。
-
On the publication database page, select the Adventureworks2016 database and click Next
在发布数据库页面上,选择Adventureworks2016数据库,然后单击“ 下一步”。
-
Next, select Transactional publication as the Publication Type, and click Next
接下来,选择“ 事务性发布”作为“发布类型”,然后单击“ 下一步”。
-
In the Articles page, choose all the objects that are part of the publication. In the case of this tutorial, it’s a full database migration hence, I select all the objects
Note: Since we are doing a full database migration, we are not going to set up any type of table row filtering.
在“ 文章”页面中,选择出版物中所有的对象。 在本教程中,这是一次完整的数据库迁移,因此,我选择所有对象
注意:由于我们正在进行完整的数据库迁移,因此我们将不会设置任何类型的表行过滤。
-
In the Snapshot Agent page select Create a snapshot immediately. We need to specify when we are going to run the snapshot agent that will create that first snapshot, which will then be applied to Azure SQL Database. In this case, we set it to create the snapshot immediately because we are going to initialize replication right away
在“ 快照代理”页面中,选择“ 立即创建快照” 。 我们需要指定何时要运行将创建第一个快照的快照代理,然后将其应用于Azure SQL数据库。 在这种情况下,我们将其设置为立即创建快照,因为我们将立即初始化复制。
-
On the Agent Security page, configure the Snapshot Agent using Security settings
在“ 代理安全性”页面上,使用“ 安全性”设置配置快照代理
-
In this case, we use the SQL Server Agent service. In this step, the recommended setting is to use a domain account. But for demo purposes, I am just going to use the SQL Server agent service account
在这种情况下,我们使用SQL Server代理服务。 在此步骤中,建议的设置是使用域帐户。 但出于演示目的,我将使用SQL Server代理服务帐户
- To complete the setup, click Finish 要完成设置,请单击“ 完成”。
-
In the next page, type in the publication name and click Finish
在下一页中,键入出版物名称,然后单击“ 完成”。
-
The publication setup progress appears. It summarizes the progress of the publication setup and configuration. In case of this tutorial, you would see that the Adventureworks_Pub is successfully created. The wizard will take care of creating the publication, adding the articles, and then starting the snapshot agent to take that initial snapshot of the AdventureWorks2016 database
显示出版物设置进度。 它总结了发布设置和配置的进度。 如果是本教程,您会看到成功创建了Adventureworks_Pub。 该向导将负责创建发布,添加文章,然后启动快照代理以获取AdventureWorks2016数据库的初始快照。
- Browse the Publication folder to view the newly-created publication 浏览出版物文件夹以查看新创建的出版物
配置订户 (Configure Subscriber)
Creating the subscription is the last step in setting up transaction replication.
创建预订是设置事务复制的最后一步。
-
In Object Explorer, expand the Replication folder, right-click Local Subscriptions, and then select New Subscriptions
在对象资源管理器中,展开“ 复制”文件夹,右键单击“ 本地订阅” ,然后选择“ 新建订阅”
-
Select the publication database. In case of this tutorial, it will be AdventureWorks2016
选择发布数据库。 如果是本教程,它将是AdventureWorks2016
-
As we only have one publication (the one that I just created), we’ll select it and click Next
由于我们只有一个出版物(我刚刚创建的一个出版物),因此将其选中,然后单击“ 下一步”。
-
In a normal setup, we have the option of selecting where to run the distribution agent. But for Azure SQL Database, the first option will be it. Only push subscription works for Azure
Note: Select pull subscription would not work. And for subscribers, I’m going to add a new subscriber, and this is going to be the Azure server.
在常规设置中,我们可以选择在哪里运行分发代理。 但是对于Azure SQL数据库,第一个选择就是它。 仅推送订阅适用于Azure
注意 :选择请求订阅将不起作用。 对于订阅者,我将添加一个新订阅者,这将是Azure服务器。
-
In the wizard, I have to select a SQL Server subscriber to add the Azure SQL Server
在向导中,我必须选择一个SQL Server订阅服务器以添加Azure SQL Server
-
In the next step, we create an empty subscriber. Use SQL Server Management Studio or Azure Portal to create an empty database. The database creation is part of the configuration; we need to look at the available service tier offerings (S0, S1, S2, S3, S4 and S5). Once this is done, go back to the wizard, back to the drop down, and click on Refresh database list to get the name of the database you just created
在下一步中,我们创建一个空订户。 使用SQL Server Management Studio或Azure门户创建一个空数据库。 创建数据库是配置的一部分。 我们需要查看可用的服务层产品(S0,S1,S2,S3,S4和S5)。 完成此操作后,返回向导,返回到下拉列表,然后单击“ 刷新数据库”列表以获取刚刚创建的数据库的名称。
-
Next, to connect to the distributer, we’re just going to use the SQL Server Agent account, though normally you would use a separate domain account. Finally, to connect to the subscriber, we’re going to use a SQL login. In this case, I use a SQL account myadminuser and its password, to push the commands from the distributor
Note: You can also try an alternative account that has the database owner permission set for the subscription database AdventureWorks2016
接下来,要连接到分发服务器,我们将使用SQL Server代理帐户,尽管通常情况下,您将使用单独的域帐户。 最后,要连接到订户,我们将使用SQL登录名。 在这种情况下,我使用一个SQL帐户myadminuser及其密码来从分发器中推送命令
注意 :您也可以尝试为订阅数据库AdventureWorks2016设置了数据库所有者权限的备用帐户
- Click Finish. 点击完成 。
-
We can either run the replication continuously, run it only on demand when we actually execute the job, or we can also define a schedule for it. As it is part of the database migration, it is recommended to set it to run continuously, so both, the publisher and subscriber databases, will be in sync. Next, we’re going to initialize the subscription, immediately, as soon as we are done with the wizard. These are the default configurations that are used for the demo
我们既可以连续运行复制,也可以仅在实际执行作业时按需运行复制,也可以为其定义时间表。 由于它是数据库迁移的一部分,因此建议将其设置为连续运行,这样发布者数据库和订阅者数据库都将保持同步。 接下来,一旦完成向导,我们将立即初始化订阅。 这些是用于演示的默认配置
-
At this point, the Create subscription wizard will create the subscription to the Azure SQL Database
此时,创建订阅向导将创建对Azure SQL数据库的订阅
- Next, let’s go back to the SQL Server Management Studio and verify that there is a new subscription that points to the Azure SQL Server. To verify this, launch Replication Monitor 接下来,让我们回到SQL Server Management Studio并验证是否存在指向Azure SQL Server的新订阅。 要验证这一点,请启动复制监视器
-
In the Replication Monitor, we can expand the publisher and click on the publication and get the status of the subscription that’s happening over the cloud
在Replication Monitor中,我们可以扩展发布者并单击发布,并获取通过云进行的订阅的状态
-
In the case of this article, we can see the status as running, it has no errors, and the performance is excellent. We can double click on that particular subscription if we want to and we would get a detailed history of the data that has been uploaded to the cloud
在本文的情况下,我们可以看到状态为“正在运行”,没有错误,并且性能非常好。 如果需要,我们可以双击该特定订阅,我们将获得已上传到云中的数据的详细历史记录
-
We can check the Undistributed Commands tab to see the status; a value of 0 means that both databases are in sync. In case of the demo, it is 1785, which means that the database is synchronizing at the moment
我们可以检查“未分发的命令”选项卡以查看状态; 值0表示两个数据库都处于同步状态。 在演示中,它是1785,这意味着数据库正在同步
-
Connect to the Azure SQL database and verify the data
连接到Azure SQL数据库并验证数据
摘要 (Summary)
In this article, we discussed how to perform a full database migration to Azure SQL Database using Transaction Replication. In this guide, I have selected all the objects in the database. In most cases it depends on the complexity of your schema. You might use SQL Server transactional replication along with other combination of data transfer methods for non-primary key tables. I hope this article gave you an idea how to perform database migrations. I hope you liked this article. Please leave your feedback in the comments section.
在本文中,我们讨论了如何使用事务复制将数据库完全迁移到Azure SQL数据库。 在本指南中,我选择了数据库中的所有对象。 在大多数情况下,这取决于架构的复杂性。 您可以将SQL Server事务复制与其他数据传输方法组合一起用于非主键表。 希望本文为您提供了如何执行数据库迁移的想法。 希望您喜欢这篇文章。 请在评论部分留下您的反馈。