教程:将 Azure SQL 数据库添加到自动故障转移组Tutorial: Add an Azure SQL Database to an autofailover group

适用于:是Azure SQL 数据库 APPLIES TO: yesAzure SQL Database

故障转移组是一种声明性抽象层,可用于对多个异地复制数据库进行分组。A failover group is a declarative abstraction layer that allows you to group multiple geo-replicated databases. 了解如何使用 Azure 门户、PowerShell 或 Azure CLI 为 Azure SQL 数据库配置故障转移组并测试故障转移。Learn to configure a failover group for an Azure SQL Database and test failover using either the Azure portal, PowerShell, or the Azure CLI. 本教程介绍以下操作:In this tutorial, you'll learn how to:

  • 在 Azure SQL 数据库中创建数据库Create a database in Azure SQL Database
  • 为数据库创建两个服务器之间的故障转移组。Create a failover group for the database between two servers.
  • 测试故障转移。Test failover.

先决条件Prerequisites

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

1 - 创建数据库1 - Create a database

在此步骤中,你将创建逻辑 SQL 服务器和使用 AdventureWorksLT 示例数据的单一数据库In this step, you create a logical SQL server and a single database that uses AdventureWorksLT sample data. 可以通过使用 Azure 门户菜单和屏幕,或通过使用 Azure CLI 或 PowerShell 脚本来创建数据库。You can create the database by using Azure portal menus and screens, or by using an Azure CLI or PowerShell script.

所有方法都包括设置服务器级防火墙规则,以允许用于访问服务器的计算机的公共 IP 地址。All the methods include setting up a server-level firewall rule to allow the public IP address of the computer you're using to access the server. 有关创建服务器级防火墙规则的详细信息,请参阅创建服务器级防火墙For more information about creating server-level firewall rules, see Create a server-level firewall. 还可以设置数据库级防火墙规则。You can also set database-level firewall rules. 请参阅创建数据库级防火墙规则See Create a database-level firewall rule.

若要在 Azure 门户中创建资源组、服务器和单一数据库,请执行以下操作:To create a resource group, server, and single database in the Azure portal:

  1. 登录门户Sign in to the portal.

  2. 在搜索栏中,搜索并选择“SQL 数据库”。From the Search bar, search for and select SQL database.

  3. 选择“创建” 。Select Create.

    创建单一数据库

  4. 在“创建 SQL 数据库”窗体的“基本信息”选项卡上的“项目详细信息”下,选择正确的 Azure订阅(如果尚未选择)。On the Basics tab of the Create SQL database form, under Project details, select the correct Azure Subscription if it isn't already selected.

  5. 在“资源组”下选择“新建”,输入“MyResourceGroup”,然后选择“确定” 。Under Resource group, select Create new, enter myResourceGroup, and select OK.

  6. 在“数据库详细信息”下,为“数据库名称”输入“mySampleDatabase”。Under Database details, for Database name enter mySampleDatabase.

  7. 对于“服务器”,选择“新建”,并按如下所示在“新服务器”窗体中填写信息:For Server, select Create new, and fill out the New server form as follows:

    • 服务器名称:输入“mysqlserver”和一些字符以实现唯一性。Server name: Enter mysqlserver, and some characters for uniqueness.
    • 服务器管理员登录名:输入“azureuser”。Server admin login: Enter azureuser.
    • 密码:输入符合要求的密码,然后在“确认密码”字段中再次输入该密码。Password: Enter a password that meets requirements, and enter it again in the Confirm password field.
    • 位置:单击下拉箭头并选择一个位置,例如“中国东部 2”。Location: Drop down and choose a location, such as China East 2.

    选择“确定” 。Select OK.

    新建服务器

    请记录服务器管理员登录名和密码,以便可以登录服务器及其数据库。Record the server admin login and password so you can log in to the server and its databases. 如果忘记了登录名或密码,那么在数据库创建之后,可在“SQL 服务器”页上获取登录名或重置密码。If you forget your login or password, you can get the login name or reset the password on the SQL server page after database creation. 若要打开“SQL 服务器”页,请在数据库“概述”页上选择服务器名称 。To open the SQL server page, select the server name on the database Overview page.

  8. 在“计算 + 存储”下,若要重新配置默认值,请选择“配置数据库”。Under Compute + storage, if you want to reconfigure the defaults, select Configure database.

    在“配置”页上,可以选择:On the Configure page, you can optionally:

    • 将“计算层”从“预配”更改为“无服务器”。Change the Compute tier from Provisioned to Serverless.
    • 查看并更改“Vcore”和“数据最大大小”设置 。Review and change the settings for vCores and Data max size.
    • 选择“更改配置”来更改硬件代系。Select Change configuration to change the hardware generation.

    进行更改后,请选择“应用”。After making any changes, select Apply.

  9. 在完成时选择“下一步:网络”。Select Next: Networking at the bottom of the page.

    新建 SQL 数据库 -“基本信息”选项卡

  10. 在“网络”选项卡上的“连接方法”下,选择“公共终结点”。On the Networking tab, under Connectivity method, select Public endpoint.

  11. 在“防火墙规则”下,将“添加当前客户端 IP 地址”设置为“是”。Under Firewall rules, set Add current client IP address to Yes.

  12. 在完成时选择“下一步:其他设置”。Select Next: Additional settings at the bottom of the page.

    “网络”选项卡

    有关防火墙设置的详细信息,请参阅允许 Azure 服务和资源访问此服务器For more information about firewall settings, see Allow Azure services and resources to access this server.

  13. 在“其他设置”选项卡上的“数据源”部分中,对于“使用现有数据”,请选择“示例”。On the Additional settings tab, in the Data source section, for Use existing data, select Sample.

  14. 在页面底部选择“查看 + 创建”。Select Review + create at the bottom of the page.

    “其他设置”选项卡

  15. 查看设置后,选择“创建”。After reviewing settings, select Create.

2 - 创建故障转移组2 - Create the failover group

此步骤在现有的服务器与另一区域中的新服务器之间创建一个故障转移组In this step, you' will create a failover group between an existing server and a new server in another region. 然后,将示例数据库添加到故障转移组。Then add the sample database to the failover group.

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

  1. Azure 门户的左侧菜单上选择“所有服务”。Select All Services on the left-hand menu of the Azure portal.

  2. 在搜索框中键入 sql serversType sql servers in the search box.

  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 following values, and then select Create:

    • 故障转移组名称:键入唯一的故障转移组名称,例如 failovergrouptutorialFailover group name: Type in a unique failover group name, such as failovergrouptutorial.

    • 辅助服务器:选择“配置所需设置”选项,然后选择“创建新服务器”。Secondary server: Select the option to configure required settings and then choose to Create a new server. 或者,可以选择现有的服务器作为辅助服务器。Alternatively, you can choose an already-existing server as the secondary server. 输入以下值后,选择“选择”。After entering the following values, select Select.

      • 服务器名称:键入辅助服务器的唯一名称,例如 mysqlsecondaryServer name: Type in a unique name for the secondary server, such as mysqlsecondary.
      • 服务器管理员登录名:键入 azureuserServer admin login: Type azureuser
      • 密码:键入符合密码要求的复杂密码。Password: Type a complex password that meets password requirements.
      • 位置:从下拉列表中选择一个位置,例如“中国北部 2”。Location: Choose a location from the drop-down, such as China North 2. 此位置不能与主服务器的位置相同。This location can't be the same location as your primary server.

      备注

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

      为故障转移组创建辅助服务器

    • 组中的数据库:选择辅助服务器后,此选项将解除锁定。Databases within the group: Once a secondary server is selected, this option becomes unlocked. 使用该选项来选择要添加的数据库:请选择在第 1 部分创建的数据库。Select it to Select databases to add and then choose the database you created in section 1. 将数据库添加到故障转移组的操作会自动启动异地复制过程。Adding the database to the failover group will automatically start the geo-replication process.

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

3 - 测试故障转移3 - Test failover

此步骤将故障转移组故障转移到辅助服务器,然后使用 Azure 门户故障回复。In this step, you'll 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 门户中导航到你的“SQL 服务器”服务器。Navigate to your SQL servers server within the Azure portal.

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

    在门户中选择故障转移组

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

  4. 在任务窗格中选择“故障转移”,以故障转移包含你的示例数据库的故障转移组。Select Failover from the task pane to fail over your failover group containing your sample database.

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

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

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

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

清理资源Clean up resources

通过删除资源组来清理资源。Clean up resources by deleting the resource group.

使用 Azure 门户删除资源组。Delete the resource group using the Azure portal.

  1. Azure 门户中导航到你的资源组。Navigate to your resource group in the Azure portal.
  2. 选择“删除资源组”即可删除该资源组中的所有资源以及该组本身。Select Delete resource group to delete all the resources in the group, as well as the resource group itself.
  3. 在文本框中键入资源组的名称 myResourceGroup,然后选择“删除”以删除该资源组。Type the name of the resource group, myResourceGroup, in the textbox, and then select Delete to delete the resource group.

重要

若要保留资源组但删除辅助数据库,请先将其从故障转移组中移除,然后再将其删除。If you want to keep the resource group but 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.

完整脚本Full scripts

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

可在以下文档中找到其他 Azure SQL 数据库脚本:Azure PowerShellAzure CLIYou can find other Azure SQL Database scripts here: Azure PowerShell and Azure CLI.

后续步骤Next steps

在本教程中,你已将一个 Azure SQL 数据库中的数据库添加到故障转移组,并测试了故障转移。In this tutorial, you added a database in Azure SQL Database to a failover group, and tested failover. 你已了解如何:You learned how to:

  • 在 Azure SQL 数据库中创建数据库Create a database in Azure SQL Database
  • 为数据库创建两个服务器之间的故障转移组。Create a failover group for the database between two servers.
  • 测试故障转移。Test failover.

请继续学习下一篇教程,了解如何将弹性池添加到故障转移组。Advance to the next tutorial on how to add your elastic pool to a failover group.