使用適用於 MongoDB 的 Azure Cosmos DB API 建立 Angular 應用程式 - 建立 Azure Cosmos DB 帳戶

適用於: MongoDB

這個多部分的教學課程示範如何使用 Express 和 Angular 來建立以 Node.js 撰寫的新應用程式,然後將其連線至使用適用於 MongoDB 的 Azure Cosmos DB API 設定的 Azure Cosmos DB 帳戶

本教學課程的第 4 部分是以 第 3 部分為基礎,並涵蓋下列工作:

  • 使用 Azure CLI 建立 Azure 資源群組
  • 使用 Azure CLI 建立 Azure Cosmos DB 帳戶

影片逐步解說

必要條件

開始本教學課程的這個部分之前,請確定您已完成本教學課程第 3 部分中的步驟

在本教學課程區段中,您可以使用 Azure Cloud Shell(在因特網瀏覽器中)或 本機安裝的 Azure CLI

Azure Cloud Shell

Azure Cloud Shell 是裝載於 Azure 中的互動式殼層環境,可在瀏覽器中使用。 您可以使用 Bash 或 PowerShell 搭配 Cloud Shell,與 Azure 服務共同使用。 您可以使用 Cloud Shell 預先安裝的命令,執行本文提到的程式碼,而不必在本機環境上安裝任何工具。

要啟動 Azure Cloud Shell:

選項 範例/連結
選取程式碼或命令區塊右上角的 [試試看]。 選取 [試試看] 並不會自動將程式碼或命令複製到 Cloud Shell 中。 Screenshot that shows an example of Try It for Azure Cloud Shell.
請前往 https://shell.azure.com,或選取 [啟動 Cloud Shell] 按鈕,在瀏覽器中開啟 Cloud Shell。 Button to launch Azure Cloud Shell.
選取 Azure 入口網站右上方功能表列上的 [Cloud Shell] 按鈕。 Screenshot that shows the Cloud Shell button in the Azure portal

若要使用 Azure Cloud Shell:

  1. 啟動 Cloud Shell。

  2. 選取程式碼區塊 (或命令區塊) 上的 [複製] 按鈕以複製程式碼或命令。

  3. 透過在 Windows 和 Linux 上選取 Ctrl+Shift+V;或在 macOS 上選取 Cmd+Shift+V,將程式碼或命令貼到 Cloud Shell 工作階段中。

  4. 選取 Enter 鍵執行程式碼或命令。

登入 Azure

您將使用 Azure CLI 來建立在 Azure 中裝載應用程式所需的資源。 如果您在 Cloud Shell 中執行 Azure CLI 命令,表示您已登入。 若要在本機執行 Azure CLI 命令,請使用 az login 命令登入您的 Azure 訂用帳戶,並遵循螢幕上的指示。

az login

建立資源群組

資源群組是一個邏輯容器,其中會部署和管理 Azure 資源,例如 Web 應用程式、資料庫和記憶體帳戶。 例如,您可以選擇稍後在一個簡單的步驟中刪除整個資源群組。

在 Cloud Shell 中,使用 az group create 命令建立資源群組。 下列範例會在西歐位置建立名為 myResourceGroup 的資源群組。 若要查看免費層中 App Service 的所有支援位置,請執行 az appservice list-locations --sku FREE 命令。

az group create --name myResourceGroup --location "West Europe"

您通常會在附近的區域中建立您的資源群組和資源。

當命令完成時,JSON 輸出會顯示資源群組屬性。

提示

本教學課程會為您解說逐步建置應用程式的步驟。 如果您需要下載已完成的專案,您可以從 GitHub 上的 angular-cosmosdb 存放庫取得已完成的應用程式。

建立 Azure Cosmos DB 帳戶

使用 az cosmosdb create 命令建立 Azure Cosmos DB 帳戶。

az cosmosdb create --name <cosmosdb-name> --resource-group myResourceGroup --kind MongoDB
  • 若要 <cosmosdb-name> 使用您自己的唯一 Azure Cosmos DB 帳戶名稱,名稱在 Azure 中的所有 Azure Cosmos DB 帳戶名稱中必須是唯一的。
  • --kind MongoDB 設定可讓 Azure Cosmos DB 具有 MongoDB 用戶端連線。

命令可能需要一兩分鐘的時間才能完成。 完成時,終端機視窗會顯示新資料庫的相關信息。

建立 Azure Cosmos DB 帳戶之後:

  1. 開啟新的瀏覽器視窗並移至 https://portal.azure.com

  2. 按兩下左側列上的 [Azure Cosmos DB] 標誌 ,它會顯示您擁有的所有 Azure Cosmos DB。

  3. 按兩下您剛才建立的 Azure Cosmos DB 帳戶,選取 [ 觀] 索引標籤,向下卷動以檢視資料庫所在的對應。

    Screenshot shows the Overview of an Azure Cosmos DB DB Account.

  4. 向下捲動左側導覽,然後按兩下 [全域 複寫資料] 索引標籤,這會顯示地圖,您可以在其中查看可復寫到的不同區域。 例如,您可以按兩下 [澳大利亞東南部] 或 [澳大利亞東部],並將您的數據複寫至澳大利亞。 您可以在如何使用 Azure Cosmos DB 全域散發數據中深入瞭解全域復寫。 現在,讓我們只保留一次實例,以及當我們想要複寫時,我們知道如何。

    Screenshot shows an Azure Cosmos DB DB Account with Replicate data globally selected.

下一步

在本教學課程中,您已完成下列作業:

  • 使用 Azure CLI 建立 Azure 資源群組
  • 使用 Azure CLI 建立 Azure Cosmos DB 帳戶

您可以繼續進行本教學課程的下一個部分,使用Mongoose將 Azure Cosmos DB 連線到您的應用程式。

正在嘗試為遷移至 Azure Cosmos DB 進行容量規劃嗎? 您可以使用現有資料庫叢集的相關資訊進行容量規劃。