共用方式為


快速入門:使用 Bicep 部署 Azure DocumentDB 叢集

在這個快速入門中,你將使用 Bicep 部署一個新的 Azure DocumentDB 叢集。 這個快速入門提供逐步指引,幫助你快速開始。 這個叢集包含你所有的 MongoDB 資源:資料庫、收藏和文件。 它提供一個獨特的端點,讓工具與軟體開發套件(SDK)連接 Azure DocumentDB 並執行操作。

先決條件

  • Azure 訂用帳戶

    • 如果您沒有 Azure 訂用帳戶,請建立 免費帳戶

設定環境

設定你的 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 檔案,以定義部署 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 Resource Manager 部署前一步建立的範本。

  1. 請使用該 az group create 指令在訂閱中建立新的資源群組。

    az group create \
        --name "<resource-group-name>" \
        --location "<location>"
    
  2. az deployment group create 來部署二頭肌範本。 接著會提示你輸入 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.rd 的值注入到 adminUsernameadminPassword 參數中。

    {
      "$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
    

    這很重要

    執行這個指令前,請確定你不再需要這些資源,因為這會永久刪除它們。