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

快速入门:使用 Bicep 部署 Azure DocumentDB 群集

在本快速入门中,你将使用 Bicep 部署新的 Azure DocumentDB 群集。 本快速入门提供分步说明,帮助你快速入门。 此群集包含所有 MongoDB 资源:数据库、集合和文档。 它为工具和软件开发工具包(SDK)提供了一个唯一的终结点,用于连接到 Azure DocumentDB 并执行操作。

先决条件

配置环境

设置 Azure CLI 环境以管理订阅中的 Azure DocumentDB 资源。

  1. 从空目录中开始。

  2. 登录到 Azure CLI。

    az login
    
  3. 检查目标 Azure 订阅。

    az account show
    

    注释

    如果未连接到预期的订阅,请使用以下命令更改订阅:

    az account set --subscription "<subscription-name>"
    

    有关详细信息,请参阅 使用 Azure CLI 管理 Azure 订阅

准备 Bicep 模板

创建并配置 Bicep 文件以定义部署 Azure DocumentDB 群集所需的资源。

  1. 在项目目录中创建新的 main.bicep 文件。

  2. 将此模板添加到文件的内容。

    @description('Cluster name')
    @minLength(8)
    @maxLength(40)
    param clusterName string = 'msdocs-${uniqueString(resourceGroup().id)}'
    
    @description('Location for the cluster.')
    param location string = resourceGroup().location
    
    @description('Username for admin user')
    param adminUsername string
    
    @secure()
    @description('Password for admin user')
    @minLength(8)
    @maxLength(128)
    param adminPassword string
    
    resource cluster 'Microsoft.DocumentDB/mongoClusters@2025-09-01' = {
      name: clusterName
      location: location
      properties: {
        administrator: {
          userName: adminUsername
          password: adminPassword
        }
        serverVersion: '8.0'
        sharding: {
          shardCount: 1
        }
        storage: {
          sizeGb: 32
        }
        highAvailability: {
          targetMode: 'Disabled'
        }
        compute: {
          tier: 'M10'
        }
      }
    }
    
    resource firewallRules 'Microsoft.DocumentDB/mongoClusters/firewallRules@2025-09-01' = {
      parent: cluster
      name: 'AllowAllAzureServices'
      properties: {
        startIpAddress: '0.0.0.0'
        endIpAddress: '0.0.0.0'
      }
    }
    

    小窍门

    有关使用 Microsoft.DocumentDB/mongoclusters 资源的选项的详细信息,请参阅 Microsoft.DocumentDB/mongoclusters 文档

部署模板

使用 Azure 资源管理器部署部署在上一步中创建的模板。

  1. 使用 az group create 命令在订阅中创建新的资源组。

    az group create \
        --name "<resource-group-name>" \
        --location "<location>"
    
  2. 使用 az deployment group create 部署 Bicep 模板。 然后,系统会提示您为参数 adminUsernameadminPassword 输入值。

    az deployment group create \
        --resource-group "<resource-group-name>" \
        --template-file 'main.bicep'
    

    小窍门

    或者,使用此选项 --parameters 传入具有预定义值的参数文件。

    az deployment group create \
        --resource-group "<resource-group-name>" \
        --template-file 'main.bicep' \
        --parameters @main.parameters.json
    

    此示例 JSON 文件分别注入clusteradminP@ssw.rdadminUsernameadminPassword参数的值。

    {
      "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "adminUsername": {
          "value": "clusteradmin"
        },
        "adminPassword": {
          "value": "P@ssw.rd"
        }
      }
    }
    
  3. 等待部署操作完成,再继续。

查看已部署的资源

列出部署到资源组的 Azure DocumentDB 资源。

  1. 使用 az resource list 来获取您的资源组中的资源列表。

    az resource list \
        --resource-group "<resource-group-name>" \
        --namespace "Microsoft.DocumentDB" \
        --resource-type "mongoClusters" \
        --query "[].name" \
        --output json
    
  2. 在示例输出中,查找类型为Microsoft.DocumentDB/mongoClusters的资源。 下面是预期输出类型的示例:

    [
      "msdocs-documentdb-example-cluster"
    ]
    

清理资源

完成 Azure DocumentDB 群集后,可以删除创建的 Azure 资源,以免产生更多费用。

  1. 使用 az group delete 从订阅中删除资源组。

    az group delete \
        --name "<resource-group-name>" \
        --yes \
        --no-wait
    

    重要

    在运行此命令之前,请确保不再需要资源,因为它会永久删除它们。