Tutorial: Add an Azure SQL Database to an auto-failover 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:
- An Azure subscription. Create a free account if you don't already have one.
1 - Create a database
In this step, you create a resource group, server, single database, and server-level IP firewall rule for access to the server.
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:
Sign in to the portal.
From the Search bar, search for and select Azure SQL.
On the Azure SQL page, select Add.
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.
Select Create.
On the Basics tab of the Create SQL database form, under Project details, select the correct Azure Subscription if it isn't already selected.
Under Resource group, select Create new, enter myResourceGroup, and select OK.
Under Database details, for Database name enter mySampleDatabase.
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 AzureAdmin.
- 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) West US.
Select OK.
Record the server admin login and password so you can sign 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.
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 hardware configuration.
After making any changes, select Apply.
Select Next: Networking at the bottom of the page.
On the Networking tab, under Connectivity method, select Public endpoint.
Under Firewall rules, set Add current client IP address to Yes.
Select Next: Additional settings at the bottom of the page.
For more information about firewall settings, see Allow Azure services and resources to access this server and Add a private endpoint.
On the Additional settings tab, in the Data source section, for Use existing data, select Sample.
Optionally, enable Microsoft Defender for SQL.
Optionally, set the maintenance window so planned maintenance is performed at the best time for your database.
Select Review + create at the bottom of the page.
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.
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.
Select the database created in section 1, such as
mySampleDatabase.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.

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

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.

- Server name: Type in a unique name for the secondary server, such as
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.

3 - Test failover
In this step, you will fail your failover group over to the secondary server, and then fail back using the Azure portal.
Test failover using the Azure portal.
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.
Select the database created in the section 2, such as
mySampleDatbase.Select the name of the server under Server name to open the settings for the server.

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

Review which server is primary and which server is secondary.
Select Failover from the task pane to fail over your failover group containing your sample database.
Select Yes on the warning that notifies you that TDS sessions will be disconnected.

Review which server is now primary and which server is secondary. If failover succeeded, the two servers should have swapped roles.
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.
- Navigate to your resource group in the Azure portal.
- Select Delete resource group to delete all the resources in the group, as well as the resource group itself.
- 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.
For additional Azure SQL Database scripts, see: 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.
Maklum balas
Kirim dan lihat maklum balas untuk
