教程:将 SQL 托管实例添加到故障转移组Tutorial: Add SQL Managed Instance to a failover group

适用于: Azure SQL 托管实例

将 Azure SQL 托管实例服务的托管实例添加到故障转移组。Add managed instances of Azure SQL Managed Instance to a failover group. 在本文中,你将学习如何:In this article, you will learn how to:

  • 创建主托管实例。Create a primary managed instance.
  • 创建辅助托管实例作为故障转移组的一部分。Create a secondary managed instance as part of a failover group.
  • 测试故障转移。Test failover.

备注

  • 学习本教程时,请确保按照为 SQL 托管实例设置故障转移组的先决条件来配置资源。When going through this tutorial, ensure you are configuring your resources with the prerequisites for setting up failover groups for SQL Managed Instance.
  • 创建托管实例可能需要花费很长时间。Creating a managed instance can take a significant amount of time. 因此,本教程可能需要几个小时才能完成。As a result, this tutorial could take several hours to complete. 要详细了解预配时间,请参阅 SQL 托管实例管理操作For more information on provisioning times, see SQL Managed Instance management operations.
  • 参与故障转移组的托管实例需要 Azure ExpressRoute 或两个连接的 VPN 网关。Managed instances participating in a failover group require either Azure ExpressRoute or two connected VPN gateways. 不支持全局 VNet 对等互连。Global VNet Peering is not supported. 本教程提供创建和连接 VPN 网关的步骤。This tutorial provides steps for creating and connecting the VPN gateways. 如果已配置 ExpressRoute,请跳过这些步骤。Skip these steps if you already have ExpressRoute configured.

先决条件Prerequisites

若要完成本教程,请确保做好以下准备:To complete this tutorial, make sure you have:

创建资源组和主托管实例Create a resource group and primary managed instance

此步骤将使用 Azure 门户或 PowerShell 为故障转移组创建资源组和主托管实例。In this step, you will create the resource group and the primary managed instance for your failover group using the Azure portal or PowerShell.

出于性能方面的考虑,将两个托管实例部署到配对区域Deploy both managed instances to paired regions for performance reasons. 与非配对区域相比,位于异地配对区域中的托管实例具有好得多的性能。Managed instances residing in geo-paired regions have much better performance compared to unpaired regions.

使用 Azure 门户创建资源组和主托管实例。Create the resource group and your primary managed instance using the Azure portal.

  1. 选择“创建”启动 SQL 托管实例 创建页。Select Create to launch the SQL managed instance creation page.

  2. 在“创建 Azure SQL 托管实例”页面的“基本信息”选项卡上: On the Create Azure SQL Managed Instance page, on the Basics tab:

    1. 在“项目详细信息”下,从下拉列表中选择你的订阅,然后选择“创建新资源组”。 Under Project Details, select your Subscription from the drop-down and then choose to Create New resource group. 键入资源组的名称,例如 myResourceGroupType in a name for your resource group, such as myResourceGroup.
    2. 在“SQL 托管实例详细信息”下,提供托管实例的名称以及要将该实例部署到的区域。Under SQL Managed Instance Details, provide the name of your managed instance, and the region where you would like to deploy your managed instance. 将“计算 + 存储”保留为默认值。Leave Compute + storage at default values.
    3. 在“管理员帐户”下,提供管理员登录名(例如 azureuser)和复杂的管理员密码。Under Administrator Account, provide an admin login, such as azureuser, and a complex admin password.

    创建辅助托管实例

  3. 将其余设置保留为默认值,然后选择“查看 + 创建”,检查 SQL 托管实例设置。Leave the rest of the settings at default values, and select Review + create to review your SQL Managed Instance settings.

  4. 选择“创建”以创建主托管实例。Select Create to create your primary managed instance.

创建辅助虚拟网络Create secondary virtual network

如果使用 Azure 门户创建托管实例,则需要单独创建虚拟网络,因为主托管实例和辅助托管实例的子网不得使用重叠的范围。If you're using the Azure portal to create your managed instance, you will need to create the virtual network separately because there is a requirement that the subnet of the primary and secondary managed instance do not have overlapping ranges. 如果使用 PowerShell 配置托管实例,请直接跳到步骤 3。If you're using PowerShell to configure your managed instance, skip ahead to step 3.

若要验证主虚拟网络的子网范围,请执行以下步骤:To verify the subnet range of your primary virtual network, follow these steps:

  1. Azure 门户中导航到你的资源组,并选择主实例的虚拟网络。In the Azure portal, navigate to your resource group and select the virtual network for your primary instance.

  2. 在“设置”下选择“子网”,并记下“地址范围”。 Select Subnets under Settings and note the Address range. 辅助托管实例的虚拟网络的子网地址范围不能与此重叠。The subnet address range of the virtual network for the secondary managed instance cannot overlap this.

    主子网

若要创建虚拟网络,请执行以下步骤:To create a virtual network, follow these steps:

  1. Azure 门户中,选择“创建资源”并搜索“虚拟网络”。In the Azure portal, select Create a resource and search for virtual network.

  2. 选择 Microsoft 发布的“虚拟网络”选项,然后在下一页选择“创建”。 Select the Virtual Network option published by Microsoft and then select Create on the next page.

  3. 填写必填字段以配置辅助托管实例的虚拟网络,然后选择“创建”。Fill out the required fields to configure the virtual network for your secondary managed instance, and then select Create.

    下表显示了辅助虚拟网络所需的值:The following table shows the values necessary for the secondary virtual network:

    字段Field Value
    名称Name 辅助托管实例要使用的虚拟网络的名称,例如 vnet-sql-mi-secondaryThe name for the virtual network to be used by the secondary managed instance, such as vnet-sql-mi-secondary.
    地址空间Address space 虚拟网络的地址空间,例如 10.128.0.0/16The address space for your virtual network, such as 10.128.0.0/16.
    订阅Subscription 主托管实例和资源组所在的订阅。The subscription where your primary managed instance and resource group reside.
    区域Region 将部署辅助托管实例的位置。The location where you will deploy your secondary managed instance.
    子网Subnet 子网的名称。The name for your subnet. 默认会提供 defaultdefault is provided for you by default.
    地址范围Address range 子网的地址范围。The address range for your subnet. 此范围必须不同于主托管实例的虚拟网络使用的子网地址范围,例如 10.128.0.0/24This must be different than the subnet address range used by the virtual network of your primary managed instance, such as 10.128.0.0/24.
       

    辅助虚拟网络值

创建辅助托管实例Create a secondary managed instance

此步骤将在 Azure 门户中创建一个辅助托管实例,同时在两个托管实例之间配置网络。In this step, you will create a secondary managed instance in the Azure portal, which will also configure the networking between the two managed instances.

第二个托管实例必须:Your second managed instance must:

  • 是空的。Be empty.
  • 与主托管实例的子网和 IP 范围不同。Have a different subnet and IP range than the primary managed instance.

使用 Azure 门户创建辅助托管实例。Create the secondary managed instance using the Azure portal.

  1. Azure 门户中选择“创建资源”,然后搜索“Azure SQL 托管实例”。In the Azure portal, select Create a resource and search for Azure SQL Managed Instance.

  2. 选择 Microsoft 发布的“Azure SQL 托管实例”选项,然后在下一页选择“创建”。 Select the Azure SQL Managed Instance option published by Microsoft, and then select Create on the next page.

  3. 在“创建 Azure SQL 托管实例”页面的“基本信息”选项卡上,填写必填字段来配置辅助托管实例 。On the Basics tab of the Create Azure SQL Managed Instance page, fill out the required fields to configure your secondary managed instance.

    下表显示了辅助托管实例所需的值:The following table shows the values necessary for the secondary managed instance:

    字段Field Value
    订阅Subscription 主托管实例所在的订阅。The subscription where your primary managed instance is.
    资源组Resource group 主托管实例所在的资源组。The resource group where your primary managed instance is.
    SQL 托管实例名称SQL Managed Instance name 新的辅助托管实例的名称,例如 sql-mi-secondaryThe name of your new secondary managed instance, such as sql-mi-secondary.
    区域Region 辅助托管实例的位置。The location for your secondary managed instance.
    SQL 托管实例管理员登录名SQL Managed Instance admin login 要用于新辅助托管实例的登录名,例如 azureuserThe login you want to use for your new secondary managed instance, such as azureuser.
    密码Password 新辅助托管实例的管理员登录名使用的复杂密码。A complex password that will be used by the admin login for the new secondary managed instance.
       
  4. 在“网络”选项卡下,对于“虚拟网络”,请从下拉列表中选择为辅助托管实例创建的虚拟网络。 Under the Networking tab, for the Virtual Network, select the virtual network you created for the secondary managed instance from the drop-down.

    辅助托管实例网络

  5. 在“其他设置”选项卡下,对于“异地复制”,请选择“用作故障转移辅助实例”旁边的“是” 。Under the Additional settings tab, for Geo-Replication, choose Yes to Use as failover secondary. 从下拉列表中选择主托管实例。Select the primary managed instance from the drop-down.

    确保排序规则和时区与主托管实例匹配。Be sure that the collation and time zone match that of the primary managed instance. 在本教程中创建的主托管实例使用了默认的 SQL_Latin1_General_CP1_CI_AS 排序规则和 (UTC) Coordinated Universal Time 时区。The primary managed instance created in this tutorial used the default of SQL_Latin1_General_CP1_CI_AS collation and the (UTC) Coordinated Universal Time time zone.

    辅助托管实例网络连接

  6. 选择“查看 + 创建”以检查辅助托管实例的设置。Select Review + create to review the settings for your secondary managed instance.

  7. 选择“创建”以创建辅助托管实例。Select Create to create your secondary managed instance.

创建主网关Create a primary gateway

对于要参与故障转移组的两个托管实例,必须在这两个托管实例的虚拟网络之间配置 ExpressRoute 或网关,以便能够进行网络通信。For two managed instances to participate in a failover group, there must be either ExpressRoute or a gateway configured between the virtual networks of the two managed instances to allow network communication. 如果选择配置 ExpressRoute 而不是连接两个 VPN 网关,请跳到步骤 7If you choose to configure ExpressRoute instead of connecting two VPN gateways, skip ahead to Step 7.

本文提供了创建两个 VPN 网关并将它们连接起来的步骤,但如果你已配置 ExpressRoute,则可以改为创建故障转移组。This article provides steps to create the two VPN gateways and connect them, but you can skip ahead to creating the failover group if you have configured ExpressRoute instead.

备注

网关的 SKU 会影响吞吐量性能。The SKU of the gateway affects throughput performance. 本教程使用最基本的 SKU (HwGw1) 来部署网关。This tutorial deploys a gateway with the most basic SKU (HwGw1). 部署更高的 SKU(示例:VpnGw3)可实现更高的吞吐量。Deploy a higher SKU (example: VpnGw3) to achieve higher throughput. 有关所有可用选项,请参阅网关 SKUFor all available options, see Gateway SKUs

使用 Azure 门户为主托管实例的虚拟网络创建网关。Create the gateway for the virtual network of your primary managed instance using the Azure portal.

  1. Azure 门户中转到你的资源组,并选择主托管实例的“虚拟网络”资源。In the Azure portal, go to your resource group and select the Virtual network resource for your primary managed instance.

  2. 在“设置”下选择“子网”,然后选择添加新的“网关子网”。 Select Subnets under Settings and then select to add a new Gateway subnet. 保留默认值。Leave the default values.

    为主托管实例添加网关

  3. 创建子网网关后,在左侧导航窗格中选择“创建资源”,然后在搜索框中键入 Virtual network gatewayOnce the subnet gateway is created, select Create a resource from the left navigation pane and then type Virtual network gateway in the search box. 选择“Azure”发布的“虚拟网络网关”资源。 Select the Virtual network gateway resource published by Azure.

    创建新的虚拟网络网关

  4. 填写必填字段,为主托管实例配置网关。Fill out the required fields to configure the gateway for your primary managed instance.

    下表显示了主托管实例的网关所需的值:The following table shows the values necessary for the gateway for the primary managed instance:

    字段Field Value
    订阅Subscription 主托管实例所在的订阅。The subscription where your primary managed instance is.
    名称Name 虚拟网络网关的名称,例如 primary-mi-gatewayThe name for your virtual network gateway, such as primary-mi-gateway.
    区域Region 主托管实例所在的区域。The region where your primary managed instance is.
    网关类型Gateway type 选择“VPN”。Select VPN.
    VPN 类型VPN Type 选择“基于路由”。Select Route-based.
    SKUSKU 保留默认值 VpnGw1Leave default of VpnGw1.
    虚拟网络Virtual network 选择在第 2 部分创建的虚拟网络,例如 vnet-sql-mi-primarySelect the virtual network that was created in section 2, such as vnet-sql-mi-primary.
    公共 IP 地址Public IP address 选择“新建”。Select Create new.
    公共 IP 地址名称Public IP address name 输入 IP 地址的名称,例如 primary-gateway-IPEnter a name for your IP address, such as primary-gateway-IP.
       
  5. 将其他值保留为默认值,然后选择“查看 + 创建”以检查虚拟网络网关的设置。Leave the other values as default, and then select Review + create to review the settings for your virtual network gateway.

    主网关设置

  6. 选择“创建”以创建新的虚拟网络网关。Select Create to create your new virtual network gateway.

创建辅助网关Create secondary gateway

此步骤使用 Azure 门户为辅助托管实例的虚拟网络创建网关。In this step, create the gateway for the virtual network of your secondary managed instance using the Azure portal.

使用 Azure 门户重复上一部分中的步骤,为辅助托管实例创建虚拟网络子网和网关。Using the Azure portal, repeat the steps in the previous section to create the virtual network subnet and gateway for the secondary managed instance. 填写必填字段,为辅助托管实例配置网关。Fill out the required fields to configure the gateway for your secondary managed instance.

下表显示了辅助托管实例的网关所需的值:The following table shows the values necessary for the gateway for the secondary managed instance:

字段Field Value
订阅Subscription 辅助托管实例所在的订阅。The subscription where your secondary managed instance is.
名称Name 虚拟网络网关的名称,例如 secondary-mi-gatewayThe name for your virtual network gateway, such as secondary-mi-gateway.
区域Region 辅助托管实例所在的区域。The region where your secondary managed instance is.
网关类型Gateway type 选择“VPN”。Select VPN.
VPN 类型VPN Type 选择“基于路由”。Select Route-based.
SKUSKU 保留默认值 VpnGw1Leave default of VpnGw1.
虚拟网络Virtual network 为辅助托管实例选择虚拟网络,例如 vnet-sql-mi-secondarySelect the virtual network for the secondary managed instance, such as vnet-sql-mi-secondary.
公共 IP 地址Public IP address 选择“新建”。Select Create new.
公共 IP 地址名称Public IP address name 输入 IP 地址的名称,例如 secondary-gateway-IPEnter a name for your IP address, such as secondary-gateway-IP.
   

辅助网关设置

连接网关Connect the gateways

此步骤在两个虚拟网络的两个网关之间创建双向连接。In this step, create a bidirectional connection between the two gateways of the two virtual networks.

使用 Azure 门户连接两个网关。Connect the two gateways using the Azure portal.

  1. Azure 门户中选择“创建资源”。Select Create a resource from the Azure portal.

  2. 在搜索框中键入 connection,然后按 Enter 进行搜索;随后你会转到 Microsoft 发布的“连接”资源。Type connection in the search box and then press enter to search, which takes you to the Connection resource, published by Microsoft.

  3. 选择“创建”以创建连接。Select Create to create your connection.

  4. 在“基本信息”页上选择以下值,然后选择“确定”。 On the Basics page, select the following values and then select OK.

    1. 为“连接类型”选择 VNet-to-VNetSelect VNet-to-VNet for the Connection type.
    2. 从下拉列表中选择订阅。Select your subscription from the drop-down.
    3. 在下拉列表中选择 SQL 托管实例的资源组。Select the resource group for SQL Managed Instance in the drop-down.
    4. 在下拉列表中选择主托管实例的位置。Select the location of your primary managed instance from the drop-down.
  5. 在“设置”页上选择或输入以下值,然后选择“确定”: On the Settings page, select or enter the following values and then select OK:

    1. 为“第一个虚拟网络网关”选择主网络网关,例如 primaryGatewayChoose the primary network gateway for the First virtual network gateway, such as primaryGateway.
    2. 为“第二个虚拟网络网关”选择辅助网络网关,例如 secondaryGatewayChoose the secondary network gateway for the Second virtual network gateway, such as secondaryGateway.
    3. 选中“建立双向连接”旁边的复选框。Select the checkbox next to Establish bidirectional connectivity.
    4. 保留默认的主连接名称,或将其重命名为所选的值。Either leave the default primary connection name, or rename it to a value of your choice.
    5. 提供连接的“共享密钥(PSK)”,例如 mi1m2pskProvide a Shared key (PSK) for the connection, such as mi1m2psk.
    6. 选择“确定”保存设置。Select OK to save your settings.

    创建网关连接

  6. 在“查看 + 创建”页上查看双向连接的设置,然后选择“确定”以创建连接。 On the Review + create page, review the settings for your bidirectional connection and then select OK to create your connection.

创建故障转移组Create a failover group

此步骤将创建故障转移组,并将两个托管实例添加到其中。In this step, you will create the failover group and add both managed instances to it.

使用 Azure 门户创建故障转移组。Create the failover group using the Azure portal.

  1. Azure 门户中切换到“所有服务”,并在搜索框中键入 managed instanceIn the Azure portal, go to All services and type in managed instance in the search box.

  2. 选择在第一部分中创建的主托管实例,例如 sql-mi-primarySelect the primary managed instance you created in the first section, such as sql-mi-primary.

  3. 在“设置”下,导航到“实例故障转移组”,然后选择“添加组”,打开“实例故障转移组”页面 。Under Settings, navigate to Instance Failover Groups and then choose Add group to open the Instance Failover Group page.

    添加故障转移组

  4. 在“实例故障转移组”页面上,键入故障转移组的名称,例如 failovergrouptutorialOn the Instance Failover Group page, type the name of your failover group, such as failovergrouptutorial. 然后,从下拉列表中选择辅助托管实例,例如 sql-mi-secondaryThen choose the secondary managed instance, such as sql-mi-secondary, from the drop-down. 选择“创建”以创建故障转移组。Select Create to create your failover group.

    创建故障转移组

  5. 故障转移组部署完成后,你将返回到“故障转移组”页。Once failover group deployment is complete, you will be taken back to the Failover group page.

测试故障转移Test failover

此步骤将故障转移组故障转移到辅助服务器,然后使用 Azure 门户故障回复。In this step, you will fail your failover group over to the secondary server, and then fail back using the Azure portal.

使用 Azure 门户测试故障转移。Test failover using the Azure portal.

  1. Azure 门户中导航到你的辅助托管实例,然后在“设置”下选择“实例故障转移组”。Navigate to your secondary managed instance within the Azure portal and select Instance Failover Groups under settings.

  2. 查看哪个托管实例是主实例,哪个托管实例是辅助实例。Review which managed instance is the primary, and which managed instance is the secondary.

  3. 选择“故障转移”,然后在有关正在断开 TDS 会话的警告中选择“是”。 Select Failover and then select Yes on the warning about TDS sessions being disconnected.

    将故障转移组故障转移

  4. 查看哪个托管实例是主实例,哪个是辅助实例。Review which managed instance is the primary and which managed instance is the secondary. 如果故障转移成功,这两个实例的角色应会交换。If failover succeeded, the two instances should have switched roles.

    故障转移后托管实例的角色已交换

  5. 转到新的 辅助 托管实例,再次选择“故障转移”,将主实例故障回复为主角色。Go to the new secondary managed instance and select Failover once again to fail the primary instance back to the primary role.

清理资源Clean up resources

若要清理资源,请依次删除托管实例、虚拟群集、所有剩余资源和资源组。Clean up resources by first deleting the managed instances, then the virtual cluster, then any remaining resources, and finally the resource group.

  1. Azure 门户中导航到你的资源组。Navigate to your resource group in the Azure portal.
  2. 选择托管实例,然后选择“删除”。Select the managed instance(s) and then select Delete. 在文本框中键入 yes 以确认你要删除该资源,然后选择“删除”。Type yes in the text box to confirm you want to delete the resource and then select Delete. 此过程可能需要一段时间才能在后台完成,在完成之前,你将无法删除“虚拟群集”或任何其他从属资源。This process may take some time to complete in the background, and until it's done, you will not be able to delete the virtual cluster or any other dependent resources. 在“活动”选项卡中监视删除过程,确认托管实例已被删除。Monitor the deletion in the Activity tab to confirm your managed instance has been deleted.
  3. 删除托管实例后,请将虚拟群集删除,方法是:在资源组中选择“虚拟群集”,然后选择“删除”。Once the managed instance is deleted, delete the virtual cluster by selecting it in your resource group, and then choosing Delete. 在文本框中键入 yes 以确认你要删除该资源,然后选择“删除”。Type yes in the text box to confirm you want to delete the resource and then select Delete.
  4. 删除任何剩余资源。Delete any remaining resources. 在文本框中键入 yes 以确认你要删除该资源,然后选择“删除”。Type yes in the text box to confirm you want to delete the resource and then select Delete.
  5. 删除资源组:选择“删除资源组”,键入资源组的名称 myResourceGroup,然后选择“删除”。 Delete the resource group by selecting Delete resource group, typing in the name of the resource group, myResourceGroup, and then selecting Delete.

完整脚本Full script

没有适用于 Azure 门户的脚本。There are no scripts available for the Azure portal.

后续步骤Next steps

在本教程中,你已在两个托管实例之间配置了一个故障转移组。In this tutorial, you configured a failover group between two managed instances. 你已了解如何:You learned how to:

  • 创建主托管实例。Create a primary managed instance.
  • 创建辅助托管实例作为故障转移组的一部分。Create a secondary managed instance as part of a failover group.
  • 测试故障转移。Test failover.

继续学习下一篇快速入门,了解如何连接到 SQL 托管实例,以及如何将数据库还原到 SQL 托管实例:Advance to the next quickstart on how to connect to SQL Managed Instance, and how to restore a database to SQL Managed Instance: