为 Azure SQL 数据库配置故障转移组Configure a failover group for Azure SQL Database

适用于: Azure SQL 数据库 Azure SQL 托管实例

本主题介绍如何为 Azure SQL 数据库和 Azure SQL 托管实例配置自动故障转移组This topic teaches you how to configure an auto-failover group for Azure SQL Database and Azure SQL Managed Instance.

单一数据库Single database

使用 Azure 门户或 PowerShell 创建故障转移组,并将单一数据库添加到其中。Create the failover group and add a single database to it using the Azure portal or PowerShell.

先决条件Prerequisites

请注意以下先决条件:Consider the following prerequisites:

  • 辅助服务器的服务器登录名和防火墙设置必须与主服务器相匹配。The server login and firewall settings for the secondary server must match that of your primary server.

创建故障转移组Create failover group

使用 Azure 门户创建故障转移组,并将单一数据库添加到其中。Create your failover group and add your single database to it using the Azure portal.

  1. Azure 门户的左侧菜单中选择“Azure SQL”。Select Azure SQL in the left-hand menu of the Azure portal. 如果 Azure SQL 不在列表中,请选择“所有服务”,然后在搜索框中键入 Azure SQL。If Azure SQL is not in the list, select All services , then type Azure SQL in the search box. (可选)选择“Azure SQL”旁边的星号将其收藏并将其添加为左侧导航栏中的项。(Optional) Select the star next to Azure SQL to favorite it and add it as an item in the left-hand navigation.

  2. 选择要添加到故障转移组中的数据库。Select the database you want to add to the failover group.

  3. 服务器名称 下选择服务器的名称以打开服务器的设置。Select the name of the server under Server name to open the settings for the server.

    打开单一数据库的服务器

  4. 在“设置”窗格下选择“故障转移组”,然后选择“添加组”以创建新的故障转移组。 Select Failover groups under the Settings pane, and then select Add group to create a new failover group.

    添加新的故障转移组

  5. 在“故障转移组”页上输入或选择所需的值,然后选择“创建”。 On the Failover Group page, enter or select the required values, and then select Create .

    • 组中的数据库 :选择要添加到故障转移组中的数据库。Databases within the group : Choose the database you want to add to your failover group. 将数据库添加到故障转移组的操作会自动启动异地复制过程。Adding the database to the failover group will automatically start the geo-replication process.

    将 SQL 数据库添加到故障转移组

测试故障转移Test failover

使用 Azure 门户或 PowerShell 测试故障转移组的故障转移。Test failover of your failover group using the Azure portal or PowerShell.

使用 Azure 门户测试故障转移组的故障转移。Test failover of your failover group using the Azure portal.

  1. Azure 门户的左侧菜单中选择“Azure SQL”。Select Azure SQL in the left-hand menu of the Azure portal. 如果 Azure SQL 不在列表中,请选择“所有服务”,然后在搜索框中键入 Azure SQL 。If Azure SQL is not in the list, select All services , then type "Azure SQL" in the search box. (可选)选择“Azure SQL”旁边的星号将其收藏并将其添加为左侧导航栏中的项。(Optional) Select the star next to Azure SQL to favorite it and add it as an item in the left-hand navigation.

  2. 选择要添加到故障转移组中的数据库。Select the database you want to add to the failover group.

    打开单一数据库的服务器

  3. 在“设置”窗格下选择“故障转移组”,然后选择刚刚创建的故障转移组。 Select Failover groups under the Settings pane and then choose the failover group you just created.

    在门户中选择故障转移组

  4. 查看哪个服务器是主服务器,哪个服务器是辅助服务器。Review which server is primary and which server is secondary.

  5. 从任务窗格中选择“故障转移”,以对包含数据库的故障转移组进行故障转移。Select Failover from the task pane to fail over your failover group containing your database.

  6. 在告知将会断开 TDS 会话连接的警告中选择“是”。Select Yes on the warning that notifies you that TDS sessions will be disconnected.

    对包含数据库的故障转移组进行故障转移

  7. 查看哪个服务器现在是主服务器,哪个服务器是辅助服务器。Review which server is now primary and which server is secondary. 如果故障转移成功,这两个服务器的角色应会交换。If failover succeeded, the two servers should have swapped roles.

  8. 再次选择“故障转移”,使服务器恢复其原始角色。Select Failover again to fail the servers back to their original roles.

重要

如果需要删除辅助数据库,请先将其从故障转移组中移除,然后再将其删除。If you need to delete the secondary database, remove it from the failover group before deleting it. 如果在从故障转移组中移除辅助数据库之前将其删除,则可能会导致不可预知的行为。Deleting a secondary database before it is removed from the failover group can cause unpredictable behavior.

弹性池Elastic pool

使用 Azure 门户或 PowerShell 创建故障转移组,并将弹性池添加到其中。Create the failover group and add an elastic pool to it using the Azure portal, or PowerShell.

先决条件Prerequisites

请注意以下先决条件:Consider the following prerequisites:

  • 辅助服务器的服务器登录名和防火墙设置必须与主服务器相匹配。The server login and firewall settings for the secondary server must match that of your primary server.

创建故障转移组Create the failover group

使用 Azure 门户或 PowerShell 为弹性池创建故障转移组。Create the failover group for your elastic pool using the Azure portal or PowerShell.

使用 Azure 门户创建故障转移组,并将弹性池添加到其中。Create your failover group and add your elastic pool to it using the Azure portal.

  1. Azure 门户的左侧菜单中选择“Azure SQL”。Select Azure SQL in the left-hand menu of the Azure portal. 如果 Azure SQL 不在列表中,请选择“所有服务”,然后在搜索框中键入 Azure SQL 。If Azure SQL is not in the list, select All services , then type "Azure SQL" in the search box. (可选)选择“Azure SQL”旁边的星号将其收藏并将其添加为左侧导航栏中的项。(Optional) Select the star next to Azure SQL to favorite it and add it as an item in the left-hand navigation.

  2. 选择要添加到故障转移组中的弹性池。Select the elastic pool you want to add to the failover group.

  3. 在“概述”窗格上,选择 服务器名称 下的服务器名称以打开服务器的设置。On the Overview pane, select the name of the server under Server name to open the settings for the server.

    打开弹性池的服务器

  4. 在“设置”窗格下选择“故障转移组”,然后选择“添加组”以创建新的故障转移组。 Select Failover groups under the Settings pane, and then select Add group to create a new failover group.

    添加新的故障转移组

  5. 在“故障转移组”页上输入或选择所需的值,然后选择“创建”。 On the Failover Group page, enter or select the required values, and then select Create . 创建新的辅助服务器,或选择现有的辅助服务器。Either create a new secondary server, or select an existing secondary server.

  6. 选择“组中的数据库”,然后选择要添加到故障转移组中的弹性池。Select Databases within the group then choose the elastic pool you want to add to the failover group. 如果辅助服务器上没有弹性池,则会出现一条警告,提示你在辅助服务器上创建弹性池。If an elastic pool does not already exist on the secondary server, a warning appears prompting you to create an elastic pool on the secondary server. 选择该警告,然后选择“确定”以在辅助服务器上创建弹性池。Select the warning, and then select OK to create the elastic pool on the secondary server.

    将弹性池添加到故障转移组中

  7. 选择“选择”以将弹性池设置应用到故障转移组,然后选择“创建”以创建故障转移组。 Select Select to apply your elastic pool settings to the failover group, and then select Create to create your failover group. 将弹性池添加到故障转移组的操作会自动启动异地复制过程。Adding the elastic pool to the failover group will automatically start the geo-replication process.

测试故障转移Test failover

使用 Azure 门户或 PowerShell 测试弹性池的故障转移。Test failover of your elastic pool using the Azure portal or PowerShell.

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

  1. Azure 门户的左侧菜单中选择“Azure SQL”。Select Azure SQL in the left-hand menu of the Azure portal. 如果 Azure SQL 不在列表中,请选择“所有服务”,然后在搜索框中键入 Azure SQL 。If Azure SQL is not in the list, select All services , then type "Azure SQL" in the search box. (可选)选择“Azure SQL”旁边的星号将其收藏并将其添加为左侧导航栏中的项。(Optional) Select the star next to Azure SQL to favorite it and add it as an item in the left-hand navigation.

  2. 选择要添加到故障转移组中的弹性池。Select the elastic pool you want to add to the failover group.

  3. 在“概述”窗格上,选择 服务器名称 下的服务器名称以打开服务器的设置。On the Overview pane, select the name of the server under Server name to open the settings for the server.

    打开弹性池的服务器

  4. 在“设置”窗格下选择“故障转移组”,然后选择在第 2 部分创建的故障转移组。 Select Failover groups under the Settings pane and then choose the failover group you created in section 2.

    在门户中选择故障转移组

  5. 查看哪个服务器是主服务器,哪个服务器是辅助服务器。Review which server is primary, and which server is secondary.

  6. 在“任务”窗格中选择“故障转移”,以故障转移包含弹性池的故障转移组。Select Failover from the task pane to fail over your failover group containing your elastic pool.

  7. 在告知将会断开 TDS 会话连接的警告中选择“是”。Select Yes on the warning that notifies you that TDS sessions will be disconnected.

    对包含数据库的故障转移组进行故障转移

  8. 查看哪个服务器是主服务器,哪个服务器是辅助服务器。Review which server is primary, which server is secondary. 如果故障转移成功,这两个服务器的角色应会交换。If failover succeeded, the two servers should have swapped roles.

  9. 再次选择“故障转移”,将故障转移组故障回复到原始设置。Select Failover again to fail the failover group back to the original settings.

重要

如果需要删除辅助数据库,请先将其从故障转移组中移除,然后再将其删除。If you need to delete the secondary database, remove it from the failover group before deleting it. 如果在从故障转移组中移除辅助数据库之前将其删除,则可能会导致不可预知的行为。Deleting a secondary database before it is removed from the failover group can cause unpredictable behavior.

SQL 托管实例SQL Managed Instance

使用 Azure 门户或 PowerShell 在 Azure SQL 托管实例中的两个托管实例之间创建故障转移组。Create a failover group between two managed instances in Azure SQL Managed Instance by using the Azure portal or PowerShell.

需要配置 ExpressRoute 或为每个 SQL 托管实例的虚拟网络创建网关,连接两个网关,然后创建故障转移组。You will need to either configure ExpressRoute or create a gateway for the virtual network of each SQL Managed Instance, connect the two gateways, and then create the failover group.

出于性能方面的考虑,将两个托管实例部署到配对区域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.

先决条件Prerequisites

请注意以下先决条件:Consider the following prerequisites:

  • 辅助托管实例必须为空。The secondary managed instance must be empty.
  • 辅助虚拟网络的子网范围不得与主虚拟网络的子网范围重叠。The subnet range for the secondary virtual network must not overlap the subnet range of the primary virtual network.
  • 辅助托管实例的排序规则和时区必须与主托管实例的排序规则和时区匹配。The collation and timezone of the secondary managed instance must match that of the primary managed instance.
  • 连接两个网关时,两个连接的 共享密钥 应该相同。When connecting the two gateways, the Shared Key should be the same for both connections.

创建主虚拟网络网关Create primary virtual network gateway

如果尚未配置 ExpressRoute,则可使用 Azure 门户或 PowerShell 创建主虚拟网关。If you have not configured ExpressRoute, you can create the primary virtual network gateway with the Azure portal, or PowerShell.

备注

网关的 SKU 会影响吞吐量性能。The SKU of the gateway affects throughput performance. 本文使用最基本的 SKU (HwGw1) 来部署网关。This article 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 primary virtual network gateway 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. 选择“Microsoft”发布的“虚拟网络网关”资源。 Select the Virtual network gateway resource published by Microsoft .

    创建新的虚拟网络网关

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

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

    字段Field ValueValue
    订阅Subscription 主托管实例所在的订阅。The subscription where your primary managed instance is.
    名称Name 虚拟网络网关的名称。The name for your virtual network 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.
    位置Location 辅助托管实例和辅助虚拟网络所在的位置。The location where your secondary managed instance and secondary virtual network is.
    虚拟网络Virtual network 为辅助托管实例选择虚拟网络。Select the virtual network for your secondary managed instance.
    公共 IP 地址Public IP address 选择“新建”。Select Create new .
    公共 IP 地址名称Public IP address name 输入 IP 地址的名称。Enter a name for your IP address.
       
  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 virtual network gateway

使用 Azure 门户或 PowerShell 创建辅助虚拟网络网关。Create the secondary virtual network gateway using the Azure portal or PowerShell.

重复上一部分中的步骤,为辅助托管实例创建虚拟网络子网和网关。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 ValueValue
订阅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.
位置Location 辅助托管实例和辅助虚拟网络所在的位置。The location where your secondary managed instance and secondary virtual network is.
虚拟网络Virtual network 选择在第 2 部分创建的虚拟网络,例如 vnet-sql-mi-secondarySelect the virtual network that was created in section 2, 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

使用 Azure 门户或 PowerShell 在两个网关之间创建连接。Create connections between the two gateways using the Azure portal or PowerShell.

需要创建的两个连接 - 从主网关到辅助网关的连接,以及从辅助网关到主网关的连接。Two connections need to be created - the connection from the primary gateway to the secondary gateway, and then the connection from the secondary gateway to the primary gateway.

对两个连接使用的共享密钥应该相同。The shared key used for both connections should be the same for each connection.

使用 Azure 门户在两个网关之间创建连接。Create connections between 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 tab, 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. 在下拉列表中选择托管实例的资源组。Select the resource group for your managed instance in the drop-down.
    4. 在下拉列表中选择主托管实例的位置。Select the location of your primary managed instance from the drop-down.
  5. 在“设置”选项卡上选择或输入以下值,然后选择“确定”: On the Settings tab, select or enter the following values and then select OK :

    1. 为“第一个虚拟网络网关”选择主网络网关,例如 Primary-GatewayChoose the primary network gateway for the First virtual network gateway , such as Primary-Gateway.
    2. 为“第二个虚拟网络网关”选择辅助网络网关,例如 Secondary-GatewayChoose the secondary network gateway for the Second virtual network gateway , such as Secondary-Gateway.
    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. 在“摘要”选项卡上查看双向连接的设置,然后选择“确定”以创建连接。 On the Summary tab, review the settings for your bidirectional connection and then select OK to create your connection.

创建故障转移组Create the failover group

使用 Azure 门户或 PowerShell 为托管实例创建故障转移组。Create the failover group for your managed instances by using the Azure portal or PowerShell.

使用 Azure 门户为 SQL 托管实例创建故障转移组。Create the failover group for your SQL Managed Instances by using the Azure portal.

  1. Azure 门户的左侧菜单中选择“Azure SQL”。Select Azure SQL in the left-hand menu of the Azure portal. 如果 Azure SQL 不在列表中,请选择“所有服务”,然后在搜索框中键入 Azure SQL。If Azure SQL is not in the list, select All services , then type Azure SQL in the search box. (可选)选择“Azure SQL”旁边的星号将其收藏并将其添加为左侧导航栏中的项。(Optional) Select the star next to Azure SQL to favorite it and add it as an item in the left-hand navigation.

  2. 选择要添加到故障转移组中的主托管实例。Select the primary managed instance you want to add to the failover group.

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

    添加故障转移组

  4. 在“实例故障转移组”页上键入故障转移组的名称,然后从下拉列表中选择辅助托管实例。On the Instance Failover Group page, type the name of your failover group and then choose the secondary managed instance 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 门户或 PowerShell 测试故障转移组的故障转移。Test failover of your failover group using the Azure portal or PowerShell.

使用 Azure 门户测试故障转移组的故障转移。Test failover of your failover group 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 manged instance is the primary and which 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.

使用专用链接,可以将逻辑服务器关联到虚拟网络和子网中的特定专用 IP 地址。Using a private link allows you to associate a logical server to a specific private IP address within the virtual network and subnet.

若要将专用链接用于故障转移组,请执行以下操作:To use a private link with your failover group, do the following:

  1. 请确保主服务器和辅助服务器位于配对区域中。Ensure your primary and secondary servers are in a paired region.
  2. 在每个区域中创建虚拟网络和子网,以托管主服务器和辅助服务器的专用终结点,使其的 IP 地址空间不重叠。Create the virtual network and subnet in each region to host private endpoints for primary and secondary servers such that they have non-overlapping IP address spaces. 例如,主虚拟网络地址范围 10.0.0.0/16 与辅助虚拟网络地址范围 10.0.0.1/16 重叠。For example, the primary virtual network address range of 10.0.0.0/16 and the secondary virtual network address range of 10.0.0.1/16 overlaps. 有关虚拟网络地址范围的详细信息,请参阅博客:设计 Azure 虚拟网络For more information about virtual network address ranges, see the blog designing Azure virtual networks.
  3. 为主服务器创建专用终结点和 Azure 专用 DNS 区域。Create a private endpoint and Azure Private DNS zone for the primary server.
  4. 同时为辅助服务器创建专用终结点,但这次选择重复使用为主服务器创建的同一专用 DNS 区域。Create a private endpoint for the secondary server as well, but this time choose to reuse the same Private DNS zone that was created for the primary server.
  5. 建立专用链接后,可以按照本文前面所述的步骤创建故障转移组。Once the private link is established, you can create the failover group following the steps outlined previously in this article.

定位侦听器终结点Locate listener endpoint

配置故障转移组后,将应用程序的连接字符串更新为侦听器终结点。Once your failover group is configured, update the connection string for your application to the listener endpoint. 这会使应用程序保持连接到故障转移组侦听器,而不是连接到主数据库、弹性池或实例数据库。This will keep your application connected to the failover group listener, rather than the primary database, elastic pool, or instance database. 这样,就不必在每次数据库实体发生故障转移时都手动更新连接字符串,并且流量将路由到当前充当主实体的任何实体。That way, you don't have to manually update the connection string every time your database entity fails over, and traffic is routed to whichever entity is currently primary.

侦听器终结点采用 fog-name.database.chinacloudapi.cn 格式,查看故障转移组时,它会显示在 Azure 门户中:The listener endpoint is in the form of fog-name.database.chinacloudapi.cn, and is visible in the Azure portal, when viewing the failover group:

故障转移组连接字符串

备注Remarks

  • 删除单一数据库或共用数据库的故障转移组不会停止复制,也不会删除已复制的数据库。Removing a failover group for a single or pooled database does not stop replication, and it does not delete the replicated database. 若要在删除单一数据库或共用数据库后将其添加回到故障转移组,需要手动停止异地复制,并从辅助服务器中删除该数据库。You will need to manually stop geo-replication and delete the database from the secondary server if you want to add a single or pooled database back to a failover group after it's been removed. 尝试将数据库添加到故障转移组时,不执行上述两项操作可能会导致类似于 The operation cannot be performed due to multiple errors 的错误。Failing to do either may result in an error similar to The operation cannot be performed due to multiple errors when attempting to add the database to the failover group.

后续步骤Next steps

有关配置故障转移组的详细步骤,请参阅以下教程:For detailed steps configuring a failover group, see the following tutorials:

有关 Azure SQL 数据库高可用性选项的概述,请参阅异地复制自动故障转移组For an overview of Azure SQL Database high availability options, see geo-replication and auto-failover groups.