Compartir a través de

使用 Azure CLI 管理 Azure Database for MySQL 的防火墙规则

Azure Database for MySQL 灵活服务器支持两种相互排斥的网络连接方法来连接到灵活服务器。 这两个选项是:

  • 公共访问(允许的 IP 地址)
  • 专用访问(虚拟网络集成)

本文重点介绍如何使用 Azure CLI 创建具有 公共访问(允许的 IP 地址) 的 Azure Database for MySQL 灵活服务器实例。 本文概述了可用于在创建服务器后创建、更新、删除、列出和显示防火墙规则的 Azure CLI 命令。 通过使用 公共访问(允许的 IP 地址),可将与 Azure Database for MySQL 灵活服务器实例的连接限制为仅允许的 IP 地址。 需要允许防火墙规则中的客户端 IP 地址。 有关详细信息,请参阅公共访问(允许的 IP 地址)。 可以在创建服务器时定义防火墙规则(建议),或稍后添加这些规则。

启动 Azure PowerShell

本快速入门需要 Azure CLI 2.0 或更高版本。 使用 az --version 查找版本。 如果需要安装或升级,请参阅 安装 Azure CLI

先决条件

使用 az login 命令登录到你的帐户。 请注意 ID 属性,该属性引用 Azure 帐户的 订阅 ID

az login

使用 az account set 命令选择帐户下的特定订阅。 使用 az login 输出中的 ID 值作为命令中的订阅参数的值。 如果有多个订阅,请选择应计费的资源所在的相应订阅。 若要获取所有订阅,请使用 az account list

az account set --subscription <subscription id>

使用 Azure CLI 在灵活服务器上创建防火墙规则

az mysql flexible-server --public access使用命令创建具有公共访问(允许的 IP 地址)的 Azure Database for MySQL 灵活服务器实例,并在创建服务器时配置防火墙规则。 使用 -public-access 选项来提供可以连接到服务器的允许 IP 地址。 提供单个 IP 地址或一系列 IP 地址以包含在允许列表中。 IP 地址范围必须用短划线分隔,不能包含任何空格。 以下示例演示了使用 Azure CLI 创建 Azure Database for MySQL 灵活服务器实例的各种选项。

有关可配置 CLI 参数的完整列表,请参阅 Azure CLI 参考文档。 例如,可以选择在以下命令中指定资源组。

例子

以下示例演示了在创建服务器期间配置防火墙规则的不同方法。

每个示例演示如何使用 --public-access 具有不同格式的参数(单个 IP、IP 范围、Azure 服务或所有 IP)来指定 IP 地址。

  • 创建具有公共访问权限的 Azure Database for MySQL 灵活服务器实例,并添加客户端 IP 地址以访问服务器。

    az mysql flexible-server create --location chinaeast2 --public-access <my_client_ip>
    
  • 创建具有公共访问权限的 Azure Database for MySQL 灵活服务器实例,并添加 IP 地址范围以有权访问此服务器。

    az mysql flexible-server create --location chinaeast2 --public-access <start_ip_address-end_ip_address>
    
  • 创建具有公共访问权限的 Azure Database for MySQL 灵活服务器实例,并允许来自 Azure IP 地址的应用程序连接到服务器。

    az mysql flexible-server create --location chinaeast2 --public-access 0.0.0.0
    

    重要

    此选项将防火墙配置为允许从 Azure 中的 Azure 服务和资源公共访问此服务器,包括来自其他客户的订阅的连接。 选择此选项时,请确保登录和用户权限仅限制对已授权用户的访问权限。

  • 创建具有公共访问权限的 Azure Database for MySQL 灵活服务器实例,并允许所有 IP 地址。

    az mysql flexible-server create --location chinaeast2 --public-access all
    

    警告

    此命令创建一个防火墙规则,其起始 IP 地址=0.0.0.0,结束 IP 地址=255.255.255.255,并且不会阻止任何 IP 地址。 Internet 上的任何主机都可以访问此服务器。 仅在不包含敏感数据的测试服务器上临时使用此规则。

  • 创建具有公共访问权限且没有 IP 地址的 Azure Database for MySQL 灵活服务器实例。

    az mysql flexible-server create --location chinaeast2 --public-access none
    

    注释

    不要在没有任何防火墙规则的情况下创建服务器。 如果未添加任何防火墙规则,则任何客户端都无法连接到服务器。

创建服务器后创建和管理防火墙规则

使用 Azure CLI 中的 az mysql flexible-server firewall-rule 命令创建、删除、列出、显示和更新防火墙规则。

命令:

  • create:创建 Azure Database for MySQL 灵活服务器防火墙规则。
  • 列表:列出 Azure Database for MySQL 灵活服务器防火墙规则。
  • 更新:更新 Azure Database for MySQL 灵活服务器防火墙规则。
  • 显示 Azure Database for MySQL 灵活服务器防火墙规则的详细信息。
  • delete:删除 Azure Database for MySQL 灵活服务器防火墙规则。

有关可配置 CLI 参数的完整列表,请参阅 Azure CLI 参考文档。 例如,在以下命令中,可以选择指定资源组。

创建防火墙规则

az mysql flexible-server firewall-rule create使用命令在服务器上创建新的防火墙规则。 若要允许访问一系列 IP 地址,请提供 IP 地址作为起始和结束 IP 地址,如以下示例所示。

az mysql flexible-server firewall-rule create --resource-group testGroup --name mydemoserver --start-ip-address 13.83.152.0 --end-ip-address 13.83.152.15

若要允许访问单个 IP 地址,请提供单个 IP 地址,如以下示例所示。

az mysql flexible-server firewall-rule create --resource-group testGroup --name mydemoserver --start-ip-address 1.1.1.1

若要允许来自 Azure IP 地址的应用程序连接到 Azure Database for MySQL 灵活服务器实例,请提供 IP 地址 0.0.0.0 作为起始 IP,如以下示例所示。

az mysql flexible-server firewall-rule create --resource-group testGroup --name mydemoserver --start-ip-address 0.0.0.0

重要

此选项将防火墙配置为允许从 Azure 中的 Azure 服务和资源公共访问此服务器,包括来自其他客户的订阅的连接。 选择此选项时,请确保身份验证和用户权限仅限制对已授权用户的访问。

成功后,每个创建命令输出都会列出 JSON 格式创建的防火墙规则的详细信息(默认情况下)。 如果失败,则结果将显示错误信息。

列出防火墙规则

使用 az mysql flexible-server firewall-rule list 命令列出服务器上的现有服务器防火墙规则。 在 -name 开关中指定服务器名称。

az mysql flexible-server firewall-rule list --name mydemoserver

输出以 JSON 格式(默认情况下)列出规则(如果有)。 可使用“-output table”开关,以更具可读性的表格格式输出结果。

az mysql flexible-server firewall-rule list --name mydemoserver --output table

更新防火墙规则

az mysql flexible-server firewall-rule update使用命令更新服务器上的现有防火墙规则。 提供现有防火墙规则的名称作为输入,以及要更新的起始 IP 地址和结束 IP 地址属性。

az mysql flexible-server firewall-rule update --name mydemoserver --rule-name FirewallRule1 --start-ip-address 13.83.152.0 --end-ip-address 13.83.152.1

如果命令成功,命令输出会列出 JSON 格式更新的防火墙规则的详细信息(默认情况下)。 如果命令失败,输出将显示错误消息文本。

注释

如果防火墙规则不存在,更新命令将创建规则。

显示防火墙规则详细信息

使用 az mysql flexible-server firewall-rule show 此命令显示服务器中的现有防火墙规则详细信息。 提供现有防火墙规则的名称作为输入。

az mysql flexible-server firewall-rule show --name mydemoserver --rule-name FirewallRule1

如果命令成功,命令输出将列出 JSON 格式指定的防火墙规则的详细信息(默认情况下)。 如果命令失败,输出将显示错误消息文本。

删除防火墙规则

az mysql flexible-server firewall-rule delete使用命令从服务器中删除现有防火墙规则。 提供当前防火墙规则的名称。

az mysql flexible-server firewall-rule delete --name mydemoserver --rule-name FirewallRule1

如果命令成功,则没有输出。 如果该命令失败,将显示错误消息文本。