使用 CLI 为 Azure SQL 数据库中的共用数据库配置活动异地复制Use CLI to configure active geo-replication for a pooled database in Azure SQL Database

此 Azure CLI 脚本示例为 Azure SQL 数据库中的共用数据库配置活动异地复制,并将其故障转移到数据库的辅助副本。This Azure CLI script example configures active geo-replication for a pooled database in Azure SQL Database and fails it over to the secondary replica of the database.

本文要求运行 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="server-$randomIdentifier"
database="database-$randomIdentifier"
pool="pool-$randomIdentifier"

secondaryResource="secondaryResource-$randomIdentifier"
secondaryLocation="China North"
secondaryServer="secondaryserver-$randomIdentifier"
secondaryPool="secondarypool-$randomIdentifier"

login="sampleLogin"
password="samplePassword123!"

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

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

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

echo "Creating $pool on $server and $secondaryPool on $secondaryServer..."
az sql elastic-pool create --name $pool --resource-group $resource --server $server --capacity 50 --db-dtu-max 50 --db-dtu-min 10 --edition "Standard"
az sql elastic-pool create --name $secondaryPool --resource-group $secondaryResource --server $secondaryServer --capacity 50 --db-dtu-max 50 --db-dtu-min 10 --edition "Standard"

echo "Creating $database in $pool..."
az sql db create --name $database --resource-group $resource --server $server --elastic-pool $pool

echo "Establishing geo-replication for $database between $server and $secondaryServer..."
az sql db replica create --name $database --partner-server $secondaryServer --resource-group $resource --server $server --elastic-pool $secondaryPool --partner-resource-group $secondaryResource

echo "Initiating failover to $secondaryServer..."
az sql db replica set-primary --name $database --resource-group $secondaryResource --server $secondaryServer

echo "Monitoring health of $database on $secondaryServer..."
az sql db replica list-links --name $database --resource-group $secondaryResource --server $secondaryServer

清理部署Clean up deployment

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

az group delete --name $resource
az group delete --name $secondaryResource

示例参考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 db replicaaz sql db replica 数据库复制命令。Database replication commands.

后续步骤Next steps

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

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