Tutorial: Add an Azure SQL Database to an autofailover group

APPLIES TO: Azure SQL Database

A failover group is a declarative abstraction layer that allows you to group multiple geo-replicated databases. 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:

  • 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 - Create a database

In this step, you create a logical SQL server and a single database that uses AdventureWorksLT sample data. You can create the database by using Azure portal menus and screens, or by using an Azure CLI or PowerShell script in the Azure Cloud Shell.

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.

To create a resource group, server, and single database in the Azure portal:

  1. Sign in to the portal.

  2. From the Search bar, search for and select Azure SQL.

  3. On the Azure SQL page, select Add.

    Add to Azure SQL

  4. On the Select SQL deployment option page, select the SQL databases tile, with Single database under Resource type. You can view more information about the different databases by selecting Show details.

  5. Select Create.

    Create single database

  6. On the Basics tab of the Create SQL database form, under Project details, select the correct Azure Subscription if it isn't already selected.

  7. Under Resource group, select Create new, enter myResourceGroup, and select OK.

  8. Under Database details, for Database name enter mySampleDatabase.

  9. For Server, select Create new, and fill out the New server form as follows:

    • Server name: Enter mysqlserver, and some characters for uniqueness.
    • Server admin login: Enter azureuser.
    • Password: Enter a password that meets requirements, and enter it again in the Confirm password field.
    • Location: Drop down and choose a location, such as (US) East US.

    Select OK.

    New server

    Record the server admin login and password so you can log in to the server and its databases. 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. To open the SQL server page, select the server name on the database Overview page.

  10. 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.
    • 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.

  11. Select Next: Networking at the bottom of the page.

    New SQL database - Basic tab

  12. On the Networking tab, under Connectivity method, select Public endpoint.

  13. Under Firewall rules, set Add current client IP address to Yes.

  14. Select Next: Additional settings at the bottom of the page.

    Networking tab

    For more information about firewall settings, see Allow Azure services and resources to access this server and Add a private endpoint.

  15. On the Additional settings tab, in the Data source section, for Use existing data, select Sample.

  16. Select Review + create at the bottom of the page.

    Additional settings tab

  17. After reviewing settings, select Create.

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.

Create your failover group and add your database to it using the Azure portal.

  1. Select Azure SQL in the left-hand menu of the Azure portal. If Azure SQL isn't in the list, select All services, then type Azure SQL in the search box. (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 created in section 1, such as mySampleDatabase.

  3. Failover groups can be configured at the server level. Select the name of the server under Server name to open the settings for the server.

    Open server for database

  4. Select Failover groups under the Settings pane, and then select Add group to create a new failover group.

    Add new failover group

  5. On the Failover Group page, enter or select the following values, and then select Create:

    • Failover 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.

      • Server name: Type in a unique name for the secondary server, such as mysqlsecondary.
      • Server admin login: Type azureuser
      • Password: Type a complex password that meets password requirements.
      • Location: Choose a location from the drop-down, such as East US. This location can't be the same location as your primary server.

      Note

      The server login and firewall settings must match that of your primary server.

      Create a secondary server for the failover group

    • Databases within the group: Once a secondary server is selected, this option becomes unlocked. 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.

    Add SQL Database to failover group

3 - Test failover

In this step, you'll fail your failover group over to the secondary server, and then fail back using the Azure portal.

Test failover using the Azure portal.

  1. Select Azure SQL in the left-hand menu of the Azure portal. If Azure SQL isn't in the list, select All services, then type Azure SQL in the search box. (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 created in the section 2, such as mySampleDatbase.

  3. Select the name of the server under Server name to open the settings for the server.

    Open server for database

  4. Select Failover groups under the Settings pane and then choose the failover group you created in section 2.

    Select the failover group from the portal

  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 sample database.

  7. Select Yes on the warning that notifies you that TDS sessions will be disconnected.

    Fail over your failover group containing your database

  8. Review which server is now primary and which server is secondary. If failover succeeded, the two servers should have swapped roles.

  9. Select Failover again to fail the servers back to their original roles.

Clean up resources

Clean up resources by deleting the resource group.

Delete the resource group using the Azure portal.

  1. 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. Type the name of the resource group, myResourceGroup, in the textbox, and then select Delete to delete the resource group.

Important

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

There are no scripts available for the Azure portal.

You can find other Azure SQL Database scripts here: Azure PowerShell and Azure CLI.

Next steps

In this tutorial, you added a database in Azure SQL Database to a failover group, and tested failover. You learned how to:

  • 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.