使用 Azure CLI 为 MongoDB API for Azure Cosmos DB 创建数据库(具有自动缩放功能和共享集合)Create a database with autoscale and shared collections for MongoDB API for Azure Cosmos DB using Azure CLI

备注

在 Azure China 中使用 Azure CLI 2.0 之前,请首先运行 az cloud set -n AzureChinaCloud 更改云环境。Before you can use Azure CLI 2.0 in Azure China, please run az cloud set -n AzureChinaCloud first to change the cloud environment. 如果要切换回全局 Azure,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Global Azure, run az cloud set -n AzureCloud again.

选择在本地安装并使用 CLI 时,本主题要求运行 Azure CLI 2.9.1 或更高版本。When you choose to install and use the CLI locally, this topic requires that you are running the Azure CLI version 2.9.1 or later. 运行 az --version 即可查找版本。Run az --version to find the version. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

示例脚本Sample script

#!/bin/bash

# Create a MongoDB API database with autoscale and 2 collections that share throughput

# Generate a unique 10 character alphanumeric string to ensure unique resource names
uniqueId=$(env LC_CTYPE=C tr -dc 'a-z0-9' < /dev/urandom | fold -w 10 | head -n 1)

# Variables for MongoDB API resources
resourceGroupName="Group-$uniqueId"
location='chinanorth2'
accountName="cosmos-$uniqueId" #needs to be lower case
serverVersion='3.6' #3.2 or 3.6
databaseName='database1'
maxThroughput=4000 #minimum = 4000
collection1Name='collection1'
collection2Name='collection2'

# Create a resource group
az group create -n $resourceGroupName -l $location

# Create a Cosmos account for MongoDB API
az cosmosdb create \
    -n $accountName \
    -g $resourceGroupName \
    --kind MongoDB \
    --server-version $serverVersion \
    --default-consistency-level Eventual \
    --locations regionName='China North 2' failoverPriority=0 isZoneRedundant=False

# Create a MongoDB API database with shared autoscale throughput
az cosmosdb mongodb database create \
    -a $accountName \
    -g $resourceGroupName \
    -n $databaseName \
    --max-throughput $maxThroughput

# Create two MongoDB API collections to share throughput
az cosmosdb mongodb collection create \
    -a $accountName \
    -g $resourceGroupName \
    -d $databaseName \
    -n $collection1Name \
    --shard 'myShardKey1'

az cosmosdb mongodb collection create \
    -a $accountName \
    -g $resourceGroupName \
    -d $databaseName \
    -n $collection2Name \
    --shard 'myShardKey2'

清理部署Clean up deployment

运行脚本示例后,可以使用以下命令删除资源组以及与其关联的所有资源。After the script sample has been run, the following command can be used to remove the resource group and all resources associated with it.

az group delete --name $resourceGroupName

脚本说明Script explanation

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

CommandCommand 说明Notes
az group createaz group create 创建用于存储所有资源的资源组。Creates a resource group in which all resources are stored.
az cosmosdb createaz cosmosdb create 创建 Azure Cosmos DB 帐户。Creates an Azure Cosmos DB account.
az cosmosdb mongodb database createaz cosmosdb mongodb database create 创建 Azure Cosmos MongoDB API 数据库。Creates an Azure Cosmos MongoDB API database.
az cosmosdb mongodb collection createaz cosmosdb mongodb collection create 创建 Azure Cosmos MongoDB API 集合。Creates an Azure Cosmos MongoDB API collection.
az group deleteaz group delete 删除资源组,包括所有嵌套的资源。Deletes a resource group including all nested resources.

后续步骤Next steps

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

可以在 Azure Cosmos DB CLI GitHub 存储库中找到所有 Azure Cosmos DB CLI 脚本示例。All Azure Cosmos DB CLI script samples can be found in the Azure Cosmos DB CLI GitHub Repository.