管理 Azure Cosmos 帐户Manage an Azure Cosmos account

本文介绍如何使用 Azure 门户、Azure PowerShell、Azure CLI 和 Azure 资源管理器模板管理 Azure Cosmos 帐户中的各种任务。This article describes how to manage various tasks on an Azure Cosmos account using the Azure portal, Azure PowerShell, Azure CLI, and Azure Resource Manager templates.

创建帐户Create an account

Azure 门户Azure portal

  1. 在 Azure 门户菜单或主页中,选择“创建资源” 。From the Azure portal menu or the Home page, select Create a resource.

  2. 在“新建”页面中搜索“Azure Cosmos DB”,然后选择它。 On the New page, search for and select Azure Cosmos DB.

  3. 在“Azure Cosmos DB”页上,选择“创建”。On the Azure Cosmos DB page, select Create.

  4. 在“创建 Azure Cosmos DB 帐户”页上,输入新 Azure Cosmos 帐户的基本设置。On the Create Azure Cosmos DB Account page, enter the basic settings for the new Azure Cosmos account.

    设置Setting Value 说明Description
    订阅Subscription 订阅名称Subscription name 选择要用于此 Azure Cosmos 帐户的 Azure 订阅。Select the Azure subscription that you want to use for this Azure Cosmos account.
    资源组Resource Group 资源组名称Resource group name 选择一个资源组,或者选择“新建”,然后输入新资源组的唯一名称。Select a resource group, or select Create new, then enter a unique name for the new resource group.
    帐户名Account Name 唯一的名称A unique name 输入标识此 Azure Cosmos 帐户的名称。Enter a name to identify your Azure Cosmos account. 由于 documents.azure.cn 将追加到所提供的名称以创建 URI,因此,请使用唯一的名称**。Because documents.azure.cn is appended to the name that you provide to create your URI, use a unique name.

    名称只能包含小写字母、数字和连字符 (-)。The name can only contain lowercase letters, numbers, and the hyphen (-) character. 它的长度必须介于 3-44 个字符之间。It must be between 3-44 characters in length.
    APIAPI 要创建的帐户的类型The type of account to create 选择“Core (SQL)”,以便使用 SQL 语法创建文档数据库并进行查询。Select Core (SQL) to create a document database and query by using SQL syntax.

    API 确定要创建的帐户的类型。The API determines the type of account to create. Azure Cosmos DB 提供五种 API:适用于文档数据的 Core (SQL) 和 MongoDB、适用于图形数据的 Gremlin、Azure 表和 Cassandra。Azure Cosmos DB provides five APIs: Core (SQL) and MongoDB for document data, Gremlin for graph data, Azure Table, and Cassandra. 目前,你必须为每种 API 创建单独的帐户。Currently, you must create a separate account for each API.
    应用免费层折扣Apply Free Tier Discount 应用或不应用Apply or Do not apply 使用 Azure Cosmos DB 免费层,你将在帐户中获得每秒的前 400 RU 免费的吞吐量和 5 GB 的免费存储。With Azure Cosmos DB free tier, you will get the first 400 RU/s and 5 GB of storage for free in an account. 了解免费层的详细信息。Learn more about free tier.
    位置Location 离用户最近的区域The region closest to your users 选择用于托管 Azure Cosmos DB 帐户的地理位置。Select a geographic location to host your Azure Cosmos DB account. 使用离用户最近的位置,使他们能够以最快的速度访问数据。Use the location that is closest to your users to give them the fastest access to the data.
    帐户类型Account Type 生产或非生产Production or Non-Production 如果帐户将用于生产工作负荷,请选择“生产”。Select Production if the account will be used for a production workload. 如果帐户将用于非生产环境(例如开发、测试、QA 或过渡),请选择“非生产”。Select Non-Production if the account will be used for non-production, e.g. development, testing, QA, or staging. 这是一个 Azure 资源标记设置,用于调整门户体验,但不会影响基础 Azure Cosmos DB 帐户。This is an Azure resource tag setting that tunes the Portal experience but does not affect the underlying Azure Cosmos DB account. 可以随时更改此值。You can change this value anytime.

    备注

    每个 Azure 订阅最多可以有一个免费层 Azure Cosmos DB 帐户,并且你必须在创建帐户时选择加入使用。You can have up to one free tier Azure Cosmos DB account per Azure subscription and must opt-in when creating the account. 如果看不到用于应用免费层折扣的选项,这意味着订阅中的另一个帐户已启用免费层。If you do not see the option to apply the free tier discount, this means another account in the subscription has already been enabled with free tier.

    Azure Cosmos DB 的“新建帐户”页面

  5. 选择“查看 + 创建”。Select Review + create. 可以跳过“网络”和“标记”部分 。You can skip the Network and Tags sections.

  6. 检查帐户设置,然后选择“创建”。Review the account settings, and then select Create. 创建帐户需要几分钟时间。It takes a few minutes to create the account. 等待门户页显示“你的部署已完成”消息。Wait for the portal page to display Your deployment is complete.

    Azure 门户“通知”窗格

  7. 选择“转到资源”,转到 Azure Cosmos DB 帐户页。Select Go to resource to go to the Azure Cosmos DB account page.

    Azure Cosmos DB 帐户页面

Azure CLIAzure CLI

请参阅使用 Azure CLI 创建 Azure Cosmos DB 帐户Please see Create an Azure Cosmos DB account with Azure CLI

Azure PowerShellAzure PowerShell

请参阅使用 PowerShell 创建 Azure Cosmos DB 帐户Please see Create an Azure Cosmos DB account with PowerShell

Azure Resource Manager 模板Azure Resource Manager template

请参阅使用 Azure 资源管理器模板创建 Azure Cosmos DB 帐户Please see Create Azure Cosmos DB account with Azure Resource Manager templates

在数据库帐户中添加/删除区域Add/remove regions from your database account

Azure 门户Azure portal

  1. 登录到 Azure 门户Sign in to Azure portal.

  2. 导航到 Azure Cosmos 帐户,打开“全局复制数据”菜单****。Go to your Azure Cosmos account, and open the Replicate data globally menu.

  3. 要添加区域,请在地图上选择包含与所需区域对应的 +**** 标签的六边形。To add regions, select the hexagons on the map with the + label that corresponds to your desired region(s). 另外,若要添加某个区域,请选择“+ 添加区域”选项,然后从下拉菜单中选择一个区域。****Alternatively, to add a region, select the + Add region option and choose a region from the drop-down menu.

  4. 若要删除区域,请选择带对号的蓝色六边形以从地图中清除一个或多个区域。To remove regions, clear one or more regions from the map by selecting the blue hexagons with check marks. 或者选择右侧位于区域旁边的“废纸篓”(🗑) 图标。Or select the "wastebasket" (🗑) icon next to the region on the right side.

  5. 若要保存更改,请选择“确定”。****To save your changes, select OK.

    添加或删除区域菜单

在单区域写入模式下,不能删除写入区域。In a single-region write mode, you cannot remove the write region. 必须先故障转移到另一区域,然后才能删除当前的写入区域。You must fail over to a different region before you can delete the current write region.

在多区域写入模式下,如果你至少具有一个区域,则可以添加或删除任何区域。In a multi-region write mode, you can add or remove any region, if you have at least one region.

Azure CLIAzure CLI

请参阅使用 Azure CLI 添加或删除区域Please see Add or remove regions with Azure CLI

Azure PowerShellAzure PowerShell

请参阅使用 Powershell 添加或删除区域Please see Add or remove regions with Powershell

配置多个写入区域Configure multiple write-regions

Azure 门户Azure portal

打开“全局复制数据”选项卡,选择“启用”以启用多区域写入**** ****。Open the Replicate Data Globally tab and select Enable to enable multi-region writes. 启用多区域写入后,你的帐户当前拥有的所有读取区域将变为读取和写入区域。After you enable multi-region writes, all the read regions that you currently have on the account will become read and write regions.

Azure Cosmos 帐户配置多主数据库屏幕快照

Azure CLIAzure CLI

请参阅使用 Azure CLI 启用多写入区域Please see Enable multiple-write regions with Azure CLI

Azure PowerShellAzure PowerShell

请参阅使用 Powershell 启用多写入区域Please see Enable multiple-write regions with Powershell

Resource Manager 模板Resource Manager template

可通过部署用于创建帐户的资源管理器模板和设置 enableMultipleWriteLocations: true 来将一个帐户从单主数据库迁移到多主数据库。An account can be migrated from single-master to multi-master by deploying the Resource Manager template used to create the account and setting enableMultipleWriteLocations: true. 以下 Azure 资源管理器模板是一个极简模板,它将为 SQL API 部署 Azure Cosmos 帐户,并启用两个区域和多个写入位置。The following Azure Resource Manager template is a bare minimum template that will deploy an Azure Cosmos account for SQL API with two regions and multiple write locations enabled.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "name": {
            "type": "String"
        },
        "location": {
            "type": "String",
            "defaultValue": "[resourceGroup().location]"
        },
        "primaryRegion":{
            "type":"string",
            "metadata": {
                "description": "The primary replica region for the Cosmos DB account."
            }
        },
        "secondaryRegion":{
            "type":"string",
            "metadata": {
              "description": "The secondary replica region for the Cosmos DB account."
          }
        }
    },
    "resources": [
        {
            "type": "Microsoft.DocumentDb/databaseAccounts",
            "kind": "GlobalDocumentDB",
            "name": "[parameters('name')]",
            "apiVersion": "2019-08-01",
            "location": "[parameters('location')]",
            "tags": {},
            "properties": {
                "databaseAccountOfferType": "Standard",
                "consistencyPolicy": { "defaultConsistencyLevel": "Session" },
                "locations":
                [
                    {
                        "locationName": "[parameters('primaryRegion')]",
                        "failoverPriority": 0,
                        "isZoneRedundant": false
                    },
                    {
                        "locationName": "[parameters('secondaryRegion')]",
                        "failoverPriority": 1,
                        "isZoneRedundant": false
                    }
                ],
                "enableMultipleWriteLocations": true
            }
        }
    ]
}

为 Azure Cosmos 帐户启用自动故障转移Enable automatic failover for your Azure Cosmos account

借助自动故障转移选项,在某个区域不可用时,Azure Cosmos DB 可以故障转移到具有最高故障转移优先级的区域,无需用户操作。The Automatic failover option allows Azure Cosmos DB to failover to the region with the highest failover priority with no user action should a region become unavailable. 如果启用自动故障转移,则可修改区域优先级。When automatic failover is enabled, region priority can be modified. 帐户必须具有两个或更多区域以启用自动故障转移。Account must have two or more regions to enable automatic failover.

Azure 门户Azure portal

  1. 在 Azure Cosmos 帐户中,打开“全局复制数据”窗格****。From your Azure Cosmos account, open the Replicate data globally pane.

  2. 在窗格顶部选择“自动故障转移”。****At the top of the pane, select Automatic Failover.

    “多区域复制数据”菜单

  3. 在“自动故障转移”窗格中,确保将“启用自动故障转移”设置为“开”。**** **** ****On the Automatic Failover pane, make sure that Enable Automatic Failover is set to ON.

  4. 选择“保存” ****。Select Save.

    自动故障转移门户菜单

Azure CLIAzure CLI

请参阅使用 Azure CLI 启用自动故障转移Please see Enable automatic failover with Azure CLI

Azure PowerShellAzure PowerShell

请参阅使用 Powershell 启用自动故障转移Please see Enable automatic failover with Powershell

为 Azure Cosmos 帐户设置故障转移优先级Set failover priorities for your Azure Cosmos account

Cosmos 帐户配置为自动故障转移后,可以更改区域的故障转移优先级。After a Cosmos account is configured for automatic failover, the failover priority for regions can be changed.

重要

帐户配置为自动故障转移后,不能修改写入区域(故障转移优先级为零)。You cannot modify the write region (failover priority of zero) when the account is configured for automatic failover. 要更改写入区域,必须禁用自动故障转移并执行手动故障转移。To change the write region, you must disable automatic failover and do a manual failover.

Azure 门户Azure portal

  1. 在 Azure Cosmos 帐户中,打开“多区域复制数据”窗格****。From your Azure Cosmos account, open the Replicate data multiple-regionally pane.

  2. 在窗格顶部选择“自动故障转移”。****At the top of the pane, select Automatic Failover.

    “多区域复制数据”菜单

  3. 在“自动故障转移”窗格中,确保将“启用自动故障转移”设置为“开”。**** **** ****On the Automatic Failover pane, make sure that Enable Automatic Failover is set to ON.

  4. 若要修改故障转移优先级,请将鼠标指针悬停在读取区域上,并通过在行左侧出现的三个点拖动读取区域。To modify the failover priority, drag the read regions via the three dots on the left side of the row that appear when you hover over them.

  5. 选择“保存” ****。Select Save.

    自动故障转移门户菜单

Azure CLIAzure CLI

请参阅使用 Azure CLI 设置故障转移优先级Please see Set failover priority with Azure CLI

Azure PowerShellAzure PowerShell

请参阅使用 Powershell 设置故障转移优先级Please see Set failover priority with Powershell

在 Azure Cosmos 帐户上执行手动故障转移Perform manual failover on an Azure Cosmos account

重要

Azure Cosmos 帐户必须配置为手动故障转移,才能成功执行此操作。The Azure Cosmos account must be configured for manual failover for this operation to succeed.

执行手动故障转移的过程涉及将帐户的写入区域(故障转移优先级 = 0)更改为已为该帐户配置的其他区域。The process for performing a manual failover involves changing the account's write region (failover priority = 0) to another region configured for the account.

备注

多主数据库帐户不能进行手动故障转移。Multi-master accounts cannot be manually failed over. 对于使用 Azure Cosmos SDK 的应用程序,SDK 会检测某个区域何时变为不可用,然后自动重定向到下一个最近的区域(如果在 SDK 中使用多宿主 API)。For applications using the Azure Cosmos SDK, the SDK will detect when a region becomes unavailable, then redirect automatically to the next closest region if using multi-homing API in the SDK.

Azure 门户Azure portal

  1. 导航到 Azure Cosmos 帐户,打开“全局复制数据”菜单****。Go to your Azure Cosmos account, and open the Replicate data globally menu.

  2. 在菜单顶部,选择“手动故障转移”。****At the top of the menu, select Manual Failover.

    “多区域复制数据”菜单

  3. 在“手动故障转移”**** 菜单上,选择你的新写入区域。On the Manual Failover menu, select your new write region. 选中相应的复选框,以指示你了解此选项会更改你的写入区域。Select the check box to indicate that you understand this option changes your write region.

  4. 若要触发故障转移,请选择“确定”。****To trigger the failover, select OK.

    手动故障转移门户菜单

Azure CLIAzure CLI

请参阅使用 Azure CLI 触发手动故障转移Please see Trigger manual failover with Azure CLI

Azure PowerShellAzure PowerShell

请参阅使用 Powershell 触发手动故障转移Please see Trigger manual failover with Powershell

后续步骤Next steps

有关如何管理 Azure Cosmos 帐户以及数据库和容器的详细信息和示例,请阅读以下文章:For more information and examples on how to manage the Azure Cosmos account as well as database and containers, read the following articles: