使用 CLI 将 Azure SQL 数据库弹性池添加到故障转移组Use CLI to add an Azure SQL Database elastic pool to a failover group

此 Azure CLI 脚本示例创建一个单一数据库,将其添加到弹性池,创建一个故障转移组,然后测试故障转移。This Azure CLI script example creates a single database, adds it to an elastic pool, creates a failover group, and tests failover.

本文要求运行 Azure CLI 2.0 或更高版本。This article requires that you are running the Azure CLI version 2.0 or later. 运行 az --version 即可查找版本。Run az --version to find the version. 如需进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install the Azure CLI.

示例脚本Sample script

登录 AzureSign in to Azure

如果没有 Azure 订阅,可在开始前创建一个试用订阅If you don't have an Azure subscription, create a Trial Subscription before you begin.

$subscription = "<subscriptionId>" # add subscription here

az account set -s $subscription # ...or use 'az login'

运行脚本Run the script

#!/bin/bash
location="China East"
randomIdentifier=random123

resource="resource-$randomIdentifier"
server="sqlserver-$randomIdentifier"
pool="pool-$randomIdentifier"
database="database-$randomIdentifier"

failover="failover-$randomIdentifier"
failoverLocation="China North"
failoverServer="sqlsecondary-$randomIdentifier"

login="sampleLogin"
password="samplePassword123!"

echo "Using resource group $resource with login: $login, password: $password..."

echo "Creating $resource..."
az group create --name $resource --location "$location"

echo "Creating $server in $location..."
az sql server create --name $server --resource-group $resource --location "$location"  --admin-user $login --admin-password $password

echo "Creating $database on $server..."
az sql db create --name $database --resource-group $resource --server $server --sample-name AdventureWorksLT

echo "Creating $pool on $server..."
az sql elastic-pool create --name $pool --resource-group $resource --server $server

echo "Adding $database to $pool..."
az sql db update --elastic-pool $pool --name $database --resource-group $resource --server $server

echo "Creating $failoverServer in $failoverLocation..."
az sql server create --name $failoverServer --resource-group $resource --location "$failoverLocation"  --admin-user $login --admin-password $password

echo "Creating $pool on $failoverServer..."
az sql elastic-pool create --name $pool --resource-group $resource --server $failoverServer

echo "Creating $failover between $server and $failoverServer..."
az sql failover-group create --name $failover --partner-server $failoverServer --resource-group $resource --server $server --failover-policy Automatic --grace-period 2

databaseId=$(az sql elastic-pool list-dbs --name $pool --resource-group $resource --server $server --query [0].name -o json | tr -d '"')

echo "Adding $database to $failover..."
az sql failover-group update --name $failover --add-db $databaseId --resource-group $resource --server $server

echo "Confirming role of $failoverServer is secondary..." # note ReplicationRole property
az sql failover-group show --name $failover --resource-group $resource --server $server

echo "Failing over to $failoverServer..."
az sql failover-group set-primary --name $failover --resource-group $resource --server $failoverServer 

echo "Confirming role of $failoverServer is now primary..." # note ReplicationRole property
az sql failover-group show --name $failover --resource-group $resource --server $server

echo "Failing back to $server...."
az sql failover-group set-primary --name $failover --resource-group $resource --server $server

清理部署Clean up deployment

使用以下命令删除资源组及其相关的所有资源。Use the following command to remove the resource group and all resources associated with it.

az group delete --name $resource

示例参考Sample reference

此脚本使用以下命令。This script uses the following commands. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation.

az sql elastic-poolaz sql elastic-pool 弹性池命令。Elastic pool commands.
az sql failover-group az sql failover-group 故障转移组命令。Failover group commands.

后续步骤Next steps

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档For more information on the Azure CLI, see Azure CLI documentation.

可以在 Azure SQL 数据库 Azure CLI 脚本中找到其他 SQL 数据库 Azure CLI 脚本示例。Additional SQL Database Azure CLI script samples can be found in the Azure SQL Database Azure CLI scripts.