故障转移组是一种声明性抽象层,可用于对多个异地复制数据库进行分组。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.
在此步骤中,你将创建逻辑 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.
在搜索栏中,搜索并选择“SQL 数据库”。From the Search bar, search for and select SQL database.
选择“创建” 。Select Create.
在“创建 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.
在“资源组”下选择“新建”,输入“MyResourceGroup”,然后选择“确定” 。Under Resource group, select Create new, enter myResourceGroup, and select OK.
在“数据库详细信息”下,为“数据库名称”输入“mySampleDatabase”。Under Database details, for Database name enter mySampleDatabase.
对于“服务器”,选择“新建”,并按如下所示在“新服务器”窗体中填写信息: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.
在“计算 + 存储”下,若要重新配置默认值,请选择“配置数据库”。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.
在完成时选择“下一步:网络”。Select Next: Networking at the bottom of the page.
在“网络”选项卡上的“连接方法”下,选择“公共终结点”。On the Networking tab, under Connectivity method, select Public endpoint.
在“防火墙规则”下,将“添加当前客户端 IP 地址”设置为“是”。Under Firewall rules, set Add current client IP address to Yes.
在完成时选择“下一步:其他设置”。Select Next: Additional settings at the bottom of the page.
可以使用 Azure 命令行接口 (Azure CLI) 创建 Azure 资源组、服务器和单一数据库。You can create an Azure resource group, server, and single database using the Azure command-line interface (Azure CLI).在计算机上安装 Azure CLI。Install Azure CLI on your computer.
以下 Azure CLI 代码将创建用于访问服务器的资源组、服务器、单一数据库和服务器级 IP 防火墙规则。The following Azure CLI code creates a resource group, server, single database, and server-level IP firewall rule for access to the server.请确保记录生成的资源组和服务器名称,以便可以在以后管理这些资源。Make sure to record the generated resource group and server names, so you can manage these resources later.
#!/bin/bash
# Sign in to Azure and set execution context (if necessary)
az login
az account set --subscription <Subscription ID>
# Set the resource group name and location for your server
resourceGroupName=myResourceGroup-$RANDOM
location=chinaeast2
# Set an admin login and password for your database
adminlogin=azureuser
password=Azure1234567
# Set a server name that is unique to Azure DNS (<server_name>.database.chinacloudapi.cn)
servername=server-$RANDOM
# Set the ip address range that can access your database
startip=0.0.0.0
endip=0.0.0.0
# Create a resource group
az group create \
--name $resourceGroupName \
--location $location
# Create a server in the resource group
az sql server create \
--name $servername \
--resource-group $resourceGroupName \
--location $location \
--admin-user $adminlogin \
--admin-password $password
# Configure a server-level firewall rule for the server
az sql server firewall-rule create \
--resource-group $resourceGroupName \
--server $servername \
-n AllowYourIp \
--start-ip-address $startip \
--end-ip-address $endip
# Create a gen5 2 vCore database in the server
az sql db create \
--resource-group $resourceGroupName \
--server $servername \
--name mySampleDatabase \
--sample-name AdventureWorksLT \
--edition GeneralPurpose \
--family Gen5 \
--capacity 2 \
可以使用 Windows PowerShell 创建资源组、服务器和单一数据库。You can create a resource group, server, and single database using Windows PowerShell.如果不想使用 Azure Cloud Shell,请安装 Azure PowerShell 模块。If you don't want to use the Azure Cloud Shell, install the Azure PowerShell module.
备注
本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module.你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020.若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介。To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module.有关 Az 模块安装说明,请参阅安装 Azure PowerShell。For Az module installation instructions, see Install Azure PowerShell.
以下 PowerShell 代码将创建用于访问服务器的 Azure 资源组、服务器、单一数据库和防火墙规则。The following PowerShell code creates an Azure resource group, server, single database, and firewall rule for access to the server.请确保记录生成的资源组和服务器名称,以便可以在以后管理这些资源。Make sure to record the generated resource group and server names, so you can manage these resources later.
# Set variables for your server and database
$subscriptionId = '<SubscriptionID>'
$resourceGroupName = "myResourceGroup-$(Get-Random)"
$location = "China East 2"
$adminLogin = "azureuser"
$password = "Azure1234567"
$serverName = "mysqlserver-$(Get-Random)"
$databaseName = "mySampleDatabase"
# The ip address range that you want to allow to access your server
$startIp = "0.0.0.0"
$endIp = "0.0.0.0"
# Show randomized variables
Write-host "Resource group name is" $resourceGroupName
Write-host "Server name is" $serverName
# Connect to Azure
Connect-AzAccount
# Set subscription ID
Set-AzContext -SubscriptionId $subscriptionId
# Create a resource group
Write-host "Creating resource group..."
$resourceGroup = New-AzResourceGroup -Name $resourceGroupName -Location $location -Tag @{Owner="SQLDB-Samples"}
$resourceGroup
# Create a server with a system wide unique server name
Write-host "Creating primary server..."
$server = New-AzSqlServer -ResourceGroupName $resourceGroupName `
-ServerName $serverName `
-Location $location `
-SqlAdministratorCredentials $(New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $adminLogin, $(ConvertTo-SecureString -String $password -AsPlainText -Force))
$server
# Create a server firewall rule that allows access from the specified IP range
Write-host "Configuring firewall for primary server..."
$serverFirewallRule = New-AzSqlServerFirewallRule -ResourceGroupName $resourceGroupName `
-ServerName $serverName `
-FirewallRuleName "AllowedIPs" -StartIpAddress $startIp -EndIpAddress $endIp
$serverFirewallRule
# Create General Purpose Gen4 database with 1 vCore
Write-host "Creating a gen5 2 vCore database..."
$database = New-AzSqlDatabase -ResourceGroupName $resourceGroupName `
-ServerName $serverName `
-DatabaseName $databaseName `
-Edition GeneralPurpose `
-VCore 2 `
-ComputeGeneration Gen5 `
-MinimumCapacity 2 `
-SampleName "AdventureWorksLT"
$database
前面的代码使用以下 PowerShell 命令:The preceding code uses these PowerShell cmdlets:
此步骤在现有的服务器与另一区域中的新服务器之间创建一个故障转移组。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.
在 Azure 门户的左侧菜单上选择“所有服务”。Select All Services on the left-hand menu of the Azure portal.
在搜索框中键入 sql servers。Type sql servers in the search box.
在 服务器名称 下选择服务器的名称以打开服务器的设置。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:
故障转移组名称:键入唯一的故障转移组名称,例如 failovergrouptutorial。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.
服务器名称:键入辅助服务器的唯一名称,例如 mysqlsecondary。Server 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.
使用 PowerShell 创建故障转移组,并将数据库添加到其中。Create your failover group and add your database to it using PowerShell.
备注
服务器登录名和防火墙设置必须与主服务器相匹配。The server login and firewall settings must match that of your primary server.
# $subscriptionId = '<SubscriptionID>'
# $resourceGroupName = "myResourceGroup-$(Get-Random)"
# $location = "China East 2"
# $adminLogin = "azureuser"
# $password = "PWD27!"+(New-Guid).Guid
# $serverName = "mysqlserver-$(Get-Random)"
# $databaseName = "mySampleDatabase"
$drLocation = "China North 2"
$drServerName = "mysqlsecondary-$(Get-Random)"
$failoverGroupName = "failovergrouptutorial-$(Get-Random)"
# The ip address range that you want to allow to access your server
# (leaving at 0.0.0.0 will prevent outside-of-azure connections to your DB)
$startIp = "0.0.0.0"
$endIp = "0.0.0.0"
# Show randomized variables
Write-host "DR Server name is" $drServerName
Write-host "Failover group name is" $failoverGroupName
# Create a secondary server in the failover region
Write-host "Creating a secondary server in the failover region..."
$drServer = New-AzSqlServer -ResourceGroupName $resourceGroupName `
-ServerName $drServerName `
-Location $drLocation `
-SqlAdministratorCredentials $(New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $adminlogin, $(ConvertTo-SecureString -String $password -AsPlainText -Force))
$drServer
# Create a server firewall rule that allows access from the specified IP range
Write-host "Configuring firewall for secondary server..."
$serverFirewallRule = New-AzSqlServerFirewallRule -ResourceGroupName $resourceGroupName `
-ServerName $drServerName `
-FirewallRuleName "AllowedIPs" -StartIpAddress $startIp -EndIpAddress $endIp
$serverFirewallRule
# Create a failover group between the servers
$failovergroup = Write-host "Creating a failover group between the primary and secondary server..."
New-AzSqlDatabaseFailoverGroup `
-ResourceGroupName $resourceGroupName `
-ServerName $serverName `
-PartnerServerName $drServerName `
-FailoverGroupName $failoverGroupName `
-FailoverPolicy Automatic `
-GracePeriodWithDataLossHours 2
$failovergroup
# Add the database to the failover group
Write-host "Adding the database to the failover group..."
Get-AzSqlDatabase `
-ResourceGroupName $resourceGroupName `
-ServerName $serverName `
-DatabaseName $databaseName | `
Add-AzSqlDatabaseToFailoverGroup `
-ResourceGroupName $resourceGroupName `
-ServerName $serverName `
-FailoverGroupName $failoverGroupName
Write-host "Successfully added the database to the failover group..."
本教程的此部分使用以下 PowerShell cmdlet:This portion of the tutorial uses the following PowerShell cmdlets:
将一个或多个数据库添加到 Azure SQL 数据库中的故障转移组。Adds one or more databases to a failover group in Azure SQL Database.
使用 Azure CLI 创建故障转移组,并将数据库添加到其中。Create your failover group and add your database to it using the Azure CLI.
备注
服务器登录名和防火墙设置必须与主服务器相匹配。The server login and firewall settings must match that of your primary server.
#!/bin/bash
# set variables
$failoverLocation = "China East 2"
$failoverServer = "failoverServer-$randomIdentifier"
$failoverGroup = "failoverGroup-$randomIdentifier"
echo "Creating a secondary server in the DR region..."
az sql server create --name $failoverServer --resource-group $resourceGroup --location $failoverLocation --admin-user $login --admin-password $password
echo "Creating a failover group between the two servers..."
az sql failover-group create --name $failoverGroup --partner-server $failoverServer --resource-group $resourceGroup --server $server --add-db $database --failover-policy Automatic
本教程的此部分使用以下 Azure CLI cmdlet:This portion of the tutorial uses the following Azure CLI cmdlets:
此步骤将故障转移组故障转移到辅助服务器,然后使用 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.
在 Azure 门户中导航到你的“SQL 服务器”服务器。Navigate to your SQL servers server within the Azure portal.
在“设置”窗格下选择“故障转移组”,然后选择在第 2 部分创建的故障转移组。 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.
在告知将会断开 TDS 会话连接的警告中选择“是”。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.
使用 PowerShell 测试故障转移。Test failover using PowerShell.
检查辅助副本的角色:Check the role of the secondary replica:
# Set variables
# $resourceGroupName = "myResourceGroup-$(Get-Random)"
# $serverName = "mysqlserver-$(Get-Random)"
# $failoverGroupName = "failovergrouptutorial-$(Get-Random)"
# Check role of secondary replica
Write-host "Confirming the secondary replica is secondary...."
(Get-AzSqlDatabaseFailoverGroup `
-FailoverGroupName $failoverGroupName `
-ResourceGroupName $resourceGroupName `
-ServerName $drServerName).ReplicationRole
故障转移到辅助服务器:Fail over to the secondary server:
# Set variables
# $resourceGroupName = "myResourceGroup-$(Get-Random)"
# $serverName = "mysqlserver-$(Get-Random)"
# $failoverGroupName = "failovergrouptutorial-$(Get-Random)"
# Failover to secondary server
Write-host "Failing over failover group to the secondary..."
Switch-AzSqlDatabaseFailoverGroup `
-ResourceGroupName $resourceGroupName `
-ServerName $drServerName `
-FailoverGroupName $failoverGroupName
Write-host "Failed failover group successfully to" $drServerName
将故障转移组还原到主服务器:Revert failover group back to the primary server:
# Set variables
# $resourceGroupName = "myResourceGroup-$(Get-Random)"
# $serverName = "mysqlserver-$(Get-Random)"
# $failoverGroupName = "failovergrouptutorial-$(Get-Random)"
# Revert failover to primary server
Write-host "Failing over failover group to the primary...."
Switch-AzSqlDatabaseFailoverGroup `
-ResourceGroupName $resourceGroupName `
-ServerName $serverName `
-FailoverGroupName $failoverGroupName
Write-host "Failed failover group successfully back to" $serverName
本教程部分使用以下 PowerShell cmdlet:This portion of the tutorial uses the following PowerShell cmdlets:
执行 Azure SQL 数据库故障转移组的故障转移。Executes a failover of an Azure SQL Database failover group.
使用 Azure CLI 测试故障转移。Test failover using the Azure CLI.
确认哪个服务器是辅助服务器:Verify which server is the secondary:
echo "Verifying which server is in the secondary role..."
az sql failover-group list --server $server --resource-group $resourceGroup
故障转移到辅助服务器:Fail over to the secondary server:
echo "Failing over group to the secondary server..."
az sql failover-group set-primary --name $failoverGroup --resource-group $resourceGroup --server $failoverServer
echo "Successfully failed failover group over to" $failoverServer
将故障转移组还原到主服务器:Revert failover group back to the primary server:
echo "Failing over group back to the primary server..."
az sql failover-group set-primary --name $failoverGroup --resource-group $resourceGroup --server $server
echo "Successfully failed failover group back to" $server
本教程的此部分使用以下 Azure CLI cmdlet:This portion of the tutorial uses the following Azure CLI cmdlets:
选择“删除资源组”即可删除该资源组中的所有资源以及该组本身。Select Delete resource group to delete all the resources in the group, as well as the resource group itself.
在文本框中键入资源组的名称 myResourceGroup,然后选择“删除”以删除该资源组。Type the name of the resource group, myResourceGroup, in the textbox, and then select Delete to delete the resource group.
使用 PowerShell 删除资源组。Delete the resource group using PowerShell.
# Set variables
# $resourceGroupName = "myResourceGroup-$(Get-Random)"
# Remove the resource group
Write-host "Removing resource group..."
Remove-AzResourceGroup -ResourceGroupName $resourceGroupName
Write-host "Resource group removed =" $resourceGroupName
本教程的此部分使用以下 PowerShell cmdlet:This portion of the tutorial uses the following PowerShell cmdlets:
使用 Azure CLI 删除资源组。Delete the resource group by using the Azure CLI.
echo "Cleaning up resources by removing the resource group..."
az group delete --name $resourceGroup
echo "Successfully removed resource group" $resourceGroup
本教程的此部分使用以下 Azure CLI cmdlet:This portion of the tutorial uses the following Azure CLI cmdlets:
删除资源组,包括所有嵌套的资源。Deletes a resource group including all nested resources.
重要
若要保留资源组但删除辅助数据库,请先将其从故障转移组中移除,然后再将其删除。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.
# Set variables for your server and database
$subscriptionId = '<SubscriptionID>'
$randomIdentifier = $(Get-Random)
$resourceGroupName = "myResourceGroup-$randomIdentifier"
$location = "China East 2"
$adminLogin = "azureuser"
$password = "PWD27!"+(New-Guid).Guid
$serverName = "mysqlserver-$randomIdentifier"
$databaseName = "mySampleDatabase"
$drLocation = "China North 2"
$drServerName = "mysqlsecondary-$randomIdentifier"
$failoverGroupName = "failovergrouptutorial-$randomIdentifier"
# The ip address range that you want to allow to access your server
# Leaving at 0.0.0.0 will prevent outside-of-azure connections
$startIp = "0.0.0.0"
$endIp = "0.0.0.0"
# Show randomized variables
Write-host "Resource group name is" $resourceGroupName
Write-host "Password is" $password
Write-host "Server name is" $serverName
Write-host "DR Server name is" $drServerName
Write-host "Failover group name is" $failoverGroupName
# Set subscription ID
Set-AzContext -SubscriptionId $subscriptionId
# Create a resource group
Write-host "Creating resource group..."
$resourceGroup = New-AzResourceGroup -Name $resourceGroupName -Location $location -Tag @{Owner="SQLDB-Samples"}
$resourceGroup
# Create a server with a system wide unique server name
Write-host "Creating primary logical server..."
$server = New-AzSqlServer -ResourceGroupName $resourceGroupName `
-ServerName $serverName `
-Location $location `
-SqlAdministratorCredentials $(New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $adminLogin, $(ConvertTo-SecureString -String $password -AsPlainText -Force))
$server
# Create a server firewall rule that allows access from the specified IP range
Write-host "Configuring firewall for primary logical server..."
$serverFirewallRule = New-AzSqlServerFirewallRule -ResourceGroupName $resourceGroupName `
-ServerName $serverName `
-FirewallRuleName "AllowedIPs" -StartIpAddress $startIp -EndIpAddress $endIp
$serverFirewallRule
# Create General Purpose Gen5 database with 2 vCore
Write-host "Creating a gen5 2 vCore database..."
$database = New-AzSqlDatabase -ResourceGroupName $resourceGroupName `
-ServerName $serverName `
-DatabaseName $databaseName `
-Edition GeneralPurpose `
-VCore 2 `
-ComputeGeneration Gen5 `
-MinimumCapacity 2 `
-SampleName "AdventureWorksLT"
$database
# Create a secondary server in the failover region
Write-host "Creating a secondary logical server in the failover region..."
$drServer = New-AzSqlServer -ResourceGroupName $resourceGroupName `
-ServerName $drServerName `
-Location $drLocation `
-SqlAdministratorCredentials $(New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $adminlogin, $(ConvertTo-SecureString -String $password -AsPlainText -Force))
$drServer
# Create a failover group between the servers
$failovergroup = Write-host "Creating a failover group between the primary and secondary server..."
New-AzSqlDatabaseFailoverGroup `
-ResourceGroupName $resourceGroupName `
-ServerName $serverName `
-PartnerServerName $drServerName `
-FailoverGroupName $failoverGroupName `
-FailoverPolicy Automatic `
-GracePeriodWithDataLossHours 2
$failovergroup
# Add the database to the failover group
Write-host "Adding the database to the failover group..."
Get-AzSqlDatabase `
-ResourceGroupName $resourceGroupName `
-ServerName $serverName `
-DatabaseName $databaseName | `
Add-AzSqlDatabaseToFailoverGroup `
-ResourceGroupName $resourceGroupName `
-ServerName $serverName `
-FailoverGroupName $failoverGroupName
Write-host "Successfully added the database to the failover group..."
# Check role of secondary replica
Write-host "Confirming the secondary replica is secondary...."
(Get-AzSqlDatabaseFailoverGroup `
-FailoverGroupName $failoverGroupName `
-ResourceGroupName $resourceGroupName `
-ServerName $drServerName).ReplicationRole
# Failover to secondary server
Write-host "Failing over failover group to the secondary..."
Switch-AzSqlDatabaseFailoverGroup `
-ResourceGroupName $resourceGroupName `
-ServerName $drServerName `
-FailoverGroupName $failoverGroupName
Write-host "Failed failover group successfully to" $drServerName
# Revert failover to primary server
Write-host "Failing over failover group to the primary...."
Switch-AzSqlDatabaseFailoverGroup `
-ResourceGroupName $resourceGroupName `
-ServerName $serverName `
-FailoverGroupName $failoverGroupName
Write-host "Failed failover group successfully back to" $serverName
# Show randomized variables
Write-host "Resource group name is" $resourceGroupName
Write-host "Password is" $password
Write-host "Server name is" $serverName
Write-host "DR Server name is" $drServerName
Write-host "Failover group name is" $failoverGroupName
# Clean up resources by removing the resource group
# Write-host "Removing resource group..."
# Remove-AzResourceGroup -ResourceGroupName $resourceGroupName
# Write-host "Resource group removed =" $resourceGroupName
此脚本使用以下命令。 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation.
在本教程中,你已将一个 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.