快速入门:使用 Azure CLI 创建 Azure DocumentDB 群集

在本快速入门中,你将使用Azure CLI来创建和管理 Azure DocumentDB 群集。 还可以配置防火墙规则、获取群集连接字符串并更新群集配置。

先决条件

  • 一份 Azure 订阅。 如果没有 Azure 订阅,请创建一个试用帐户
  • 如需在本地运行 CLI 参考命令,请安装 Azure CLI。 如果在 Windows 或 macOS 上运行,请考虑在 Docker 容器中运行 Azure CLI。 有关详细信息,请参阅如何在 Docker 容器中运行 Azure CLI

    • 如果使用本地安装,请使用 az login 命令登录到Azure CLI。 若要完成身份验证过程,请遵循终端中显示的步骤。 有关其他登录选项,请参阅使用 Azure CLI 登录

    • 出现提示时,请在首次使用时安装 Azure CLI 扩展。 有关扩展的详细信息,请参阅 将扩展与 Azure CLI 配合使用

    • 运行az version命令,以查看已安装的版本和依赖库。 若要升级到最新版本,请运行az upgrade

安装或更新 Azure CLI 的 Azure DocumentDB 扩展。

az extension add --name documentdb --upgrade

创建 Azure DocumentDB 群集

使用 az group create 创建资源组。 将占位符替换为环境的值。

az group create \
    --name <resource-group> \
    --location <azure-region>

检查目标区域中是否有群集名称可用。

az documentdb mongocluster check-name-availability \
    --name <cluster-name> \
    --location <azure-region>

使用 az documentdb mongocluster create. 创建群集。 群集名称必须多区域唯一。 预览 GeoReplicas 功能使群集有资格成为同一区域或跨区域副本的源。 它不会创建副本。

az documentdb mongocluster create \
    --name <cluster-name> \
    --resource-group <resource-group> \
    --location <azure-region> \
    --admin-user <admin-user> \
    --admin-password <admin-password> \
    --server-version 8.0 \
    --tier M30 \
    --storage-size 128 \
    --storage-type PremiumSSDv2 \
    --shard-count 1 \
    --high-availability Disabled \
    --auth-allowed-modes NativeAuth \
    --public-network-access Enabled \
    --preview-features GeoReplicas \
    --no-wait

等待群集完成预配。

az documentdb mongocluster wait \
    --name <cluster-name> \
    --resource-group <resource-group> \
    --created

创建源群集时启用 GeoReplicas ,因为以后无法添加该功能。 群集完成预配后,使用 az documentdb mongocluster replica create 创建副本。 该服务将 AsyncReplica 角色分配给同一区域副本,并将 GeoAsyncReplica 角色分配给跨区域副本。

对于生产群集,将 --high-availability 设置为 ZoneRedundantPreferred。 高可用性与副本支持相互独立。 有关详细信息,请参阅 Azure DocumentDB 中的高可用性

配置防火墙规则

创建允许来自特定 IPv4 地址的连接的防火墙规则。 对范围的开始和结尾使用相同的地址,以允许一个地址。

az documentdb mongocluster firewall-rule create \
    --name <firewall-rule-name> \
    --cluster-name <cluster-name> \
    --resource-group <resource-group> \
    --start-ip-address <client-ip-address> \
    --end-ip-address <client-ip-address>

对于生产工作负荷,请使用 专用终结点 而不是公共网络访问。

查看群集

显示新群集的属性。

az documentdb mongocluster show \
    --name <cluster-name> \
    --resource-group <resource-group>

列出资源组中的所有群集。

az documentdb mongocluster list \
    --resource-group <resource-group> \
    --output table

获取连接字符串

获取群集的连接字符串。

az documentdb mongocluster list-connection-strings \
    --cluster-name <cluster-name> \
    --resource-group <resource-group>

将连接字符串视为机密。 不要将它们存储在源代码中或将其提交到源代码管理中。

查看所有参数

使用 --help 参数查看命令的所有参数和示例。

az documentdb mongocluster create --help

更新群集

仅更新要更改的属性。 以下命令更改计算层。

az documentdb mongocluster update \
    --name <cluster-name> \
    --resource-group <resource-group> \
    --tier M40

重置管理员密码

使用 az documentdb mongocluster reset-password.. 重置本机管理员密码。

az documentdb mongocluster reset-password \
    --name <cluster-name> \
    --resource-group <resource-group> \
    --admin-password <new-admin-password>

清理资源

不再需要群集时,请将其删除。

az documentdb mongocluster delete \
    --name <cluster-name> \
    --resource-group <resource-group> \
    --yes