你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

对于单个写入区域的 Azure Cosmos DB 帐户,使用 PowerShell 更改故障转移优先级或触发故障转移

适用于: NoSQL MongoDB Cassandra Gremlin

注意

建议使用 Azure Az PowerShell 模块与 Azure 交互。 请参阅安装 Azure PowerShell 以开始使用。 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 Az

本示例需要 Azure PowerShell Az 5.4.0 或更高版本。 运行 Get-Module -ListAvailable Az,查看已安装哪些版本。 如果需要安装,请参阅安装 Azure PowerShell 模块

通过运行 Connect-AzAccount 登录到 Azure。

示例脚本

注意

failoverPriority=0 的区域进行的任何更改都会触发手动故障转移,并且只能对配置为手动故障转移的帐户进行更改。 对其他所有区域进行的更改只是更改了 Azure Cosmos DB 帐户的故障转移优先级。

注意

此示例演示如何使用 NoSQL API 帐户。 若要将此示例用于其他 API,请复制相关属性,并将其应用于 API 特定的脚本

# Reference: Az.CosmosDB | https://docs.microsoft.com/powershell/module/az.cosmosdb
# --------------------------------------------------
# Purpose
# Update Cosmos DB account: Change region failover priority.
# Note: updating location at priority 0 triggers a failover to the new location
# --------------------------------------------------
# Variables - ***** SUBSTITUTE YOUR VALUES *****
$resourceGroupName = "myResourceGroup" # Resource Group must already exist
$accountName = "myaccount" # Must be all lower case
$locations = @("West US", "East US") # Regions ordered by UPDATED failover priority
# --------------------------------------------------

# Get existing Cosmos DB account
$account = Get-AzCosmosDBAccount -ResourceGroupName $resourceGroupName -Name $accountName

# Update account failover priority
Update-AzCosmosDBAccountFailoverPriority -InputObject $account -FailoverPolicy $locations

清理部署

运行脚本示例后,可以使用以下命令删除资源组以及与其关联的所有资源。

Remove-AzResourceGroup -ResourceGroupName "myResourceGroup"

脚本说明

此脚本使用以下命令。 表中的每条命令均链接到特定于命令的文档。

命令 注释
Azure Cosmos DB
Get-AzCosmosDBAccount 列出 Azure Cosmos DB 帐户或获取指定的 Azure Cosmos DB 帐户。
Update-AzCosmosDBAccountFailoverPriority 更新 Azure Cosmos DB 帐户区域的故障转移优先级顺序。
Azure 资源组
Remove-AzResourceGroup 删除资源组,包括所有嵌套的资源。

后续步骤

有关 Azure PowerShell 的详细信息,请参阅 Azure PowerShell 文档