快速入门:使用 Azure CLI 为 MySQL 服务器创建 Azure 数据库Quickstart: Create an Azure Database for MySQL server using Azure CLI

备注

将要查看的是 Azure Database for MySQL 的新服务。You are viewing the new service of Azure Database for MySQL. 若要查看经典 MySQL Database for Azure 的文档,请访问此页To view the documentation for classic MySQL Database for Azure, please visit this page.

提示

考虑使用更简单的 az mysql up Azure CLI 命令(当前处于预览状态)。Consider using the simpler az mysql up Azure CLI command (currently in preview). 试用快速入门Try out the quickstart.

本快速入门教程介绍如何使用 Azure CLI 在大约 5 分钟内创建 Azure Database for MySQL 服务器。This quickstart shows how to use the Azure CLI commands to create an Azure Database for MySQL server in five minutes. 如果没有 Azure 订阅,请在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

备注

本文要求运行 Azure CLI 2.0 或更高版本。This article requires that you are running the Azure CLI version 2.0 or later. 运行 az --version 即可查找版本。Run az --version to find the version. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

先决条件Prerequisites

本文要求在本地运行 Azure CLI 2.0 或更高版本。This article requires that you're running the Azure CLI version 2.0 or later locally. 若要查看安装的版本,请运行 az --version 命令。To see the version installed, run the az --version command. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

你将需要使用 az login 命令登录到你的帐户。You'll need to log in to your account using the az login command. 请注意 id 属性,该属性指的是 Azure 帐户的订阅 ID。Note the id property, which refers to Subscription ID for your Azure account.

az login

使用 az account set 命令选择帐户下的特定订阅。Select the specific subscription under your account using az account set command. 记下 az login 输出中的 id 值,以用作命令中订阅参数的值。Make a note of the id value from the az login output to use as the value for subscription argument in the command. 如果有多个订阅,请选择应计费的资源所在的相应订阅。If you have multiple subscriptions, choose the appropriate subscription in which the resource should be billed. 若要获取所有订阅,请使用 az account listTo get all your subscription, use az account list.

az account set --subscription <subscription id>

创建 Azure Database for MySQL 服务器Create an Azure Database for MySQL server

使用 az group create 命令创建 Azure 资源组,然后在此资源组中创建 MySQL 服务器。Create an Azure resource group using the az group create command and then create your MySQL server inside this resource group. 应提供唯一名称。You should provide a unique name. 以下示例在 chinaeast2 位置创建名为 myresourcegroup 的资源组。The following example creates a resource group named myresourcegroup in the chinaeast2 location.

az group create --name myresourcegroup --location chinaeast2

使用 az mysql server create 命令为 MySQL 服务器创建 Azure 数据库。Create an Azure Database for MySQL server with the az mysql server create command. 一台服务器可以管理多个数据库。A server can contain multiple databases.

az mysql server create --resource-group myresourcegroup --name mydemoserver --location chinaeast2 --admin-user myadmin --admin-password <server_admin_password> --sku-name GP_Gen5_2 

下面是上述参数的详细信息:Here are the details for arguments above :

设置Setting 示例值Sample value 说明Description
namename mydemoservermydemoserver 输入 Azure Database for MySQL 服务器的唯一名称。Enter a unique name for your Azure Database for MySQL server. 服务器名称只能包含小写字母、数字和连字符 (-) 字符。The server name can contain only lowercase letters, numbers, and the hyphen (-) character. 必须包含 3 到 63 个字符。It must contain from 3 to 63 characters.
resource-groupresource-group myresourcegroupmyresourcegroup 提供 Azure 资源组的名称。Provide the name of the Azure resource group.
locationlocation chinaeast2chinaeast2 服务器的 Azure 位置。The Azure location for the server.
admin-useradmin-user myadminmyadmin 管理员的登录用户名。The username for the administrator login. 不能是 azure_superuseradminadministratorrootguestpublicIt cannot be azure_superuser, admin, administrator, root, guest, or public.
admin-passwordadmin-password 安全密码secure password 管理员用户的密码。The password of the administrator user. 该密码必须包含 8 到 128 个字符。It must contain between 8 and 128 characters. 密码必须包含以下三个类别的字符:英文大写字母、英文小写字母、数字和非字母数字字符。Your password must contain characters from three of the following categories: English uppercase letters, English lowercase letters, numbers, and non-alphanumeric characters.
sku-namesku-name GP_Gen5_2GP_Gen5_2 输入定价层和计算配置的名称。Enter the name of the pricing tier and compute configuration. 请遵循简写约定 {pricing tier} {compute generation} {vCores}。Follows the convention {pricing tier}{compute generation}{vCores} in shorthand. 有关详细信息,请参阅定价层See the pricing tiers for more information.

重要

  • 服务器上的默认 MySQL 版本为 5.7。The default MySQL version on your server is 5.7 . 目前还提供了 5.6 和 8.0 版本。We currently have 5.6 and 8.0 versions also available.
  • 若要查看 az mysql server create 命令的所有参数,请参阅此参考文档To view all the arguments for az mysql server create command, see this reference document.
  • 默认情况下,SSL 在服务器上启用。SSL is enabled by default on your server . 有关 SSL 的详细信息,请参阅配置 SSL 连接For more infroamtion on SSL, see Configure SSL connectivity

配置服务器级防火墙规则Configure a server-level firewall rule

默认情况下,创建的新服务器使用防火墙规则进行保护,并且无法公开访问。By default the new server created is protected with firewall rules and not accessible publicly. 可以使用 az mysql server firewall-rule create 命令在服务器上配置防火墙规则。You can configure the firewall rule on your server using the az mysql server firewall-rule create command. 这将允许你在本地连接到服务器。This will allow you to connect to the server locally.

以下示例创建名为 AllowMyIP 的防火墙规则,该规则允许从特定的 IP 地址 (192.168.0.1) 进行连接。The following example creates a firewall rule called AllowMyIP that allows connections from a specific IP address, 192.168.0.1. 替换将从其连接的 IP 地址。Replace the IP address you will be connecting from. 如果需要,可以使用一系列 IP 地址。You can use an range of IP addresses if needed. 不知道如何查找你的 IP,则转到 https://whatismyipaddress.com/ 获取 IP 地址。Don't know how to look for your IP, then go to https://whatismyipaddress.com/ to get your IP address.

az mysql server firewall-rule create --resource-group myresourcegroup --server mydemoserver --name AllowMyIP --start-ip-address 192.168.0.1 --end-ip-address 192.168.0.1

备注

连接到 Azure Database for MySQL 时,经端口 3306 进行通信。Connections to Azure Database for MySQL communicate over port 3306. 如果尝试从企业网络内部进行连接,则可能不允许经端口 3306 的出站流量。If you try to connect from within a corporate network, outbound traffic over port 3306 might not be allowed. 如果是这样,则无法连接到服务器,除非 IT 部门打开了端口 3306。If this is the case, you can't connect to your server unless your IT department opens port 3306.

获取连接信息Get the connection information

若要连接到服务器,需要提供主机信息和访问凭据。To connect to your server, you need to provide host information and access credentials.

az mysql server show --resource-group myresourcegroup --name mydemoserver

结果采用 JSON 格式。The result is in JSON format. 记下 fullyQualifiedDomainName 和 administratorLogin 。Make a note of the fullyQualifiedDomainName and administratorLogin.

{
  "administratorLogin": "myadmin",
  "earliestRestoreDate": null,
  "fullyQualifiedDomainName": "mydemoserver.mysql.database.chinacloudapi.cn",
  "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myresourcegroup/providers/Microsoft.DBforMySQL/servers/mydemoserver",
  "location": "chinaeast2",
  "name": "mydemoserver",
  "resourceGroup": "myresourcegroup",
  "sku": {
    "capacity": 2,
    "family": "Gen5",
    "name": "GP_Gen5_2",
    "size": null,
    "tier": "GeneralPurpose"
  },
  "sslEnforcement": "Enabled",
  "storageProfile": {
    "backupRetentionDays": 7,
    "geoRedundantBackup": "Disabled",
    "storageMb": 5120
  },
  "tags": null,
  "type": "Microsoft.DBforMySQL/servers",
  "userVisibleState": "Ready",
  "version": "5.7"
}

使用 mysql 命令行客户端连接到 Azure Database for MySQL 服务器Connect to Azure Database for MySQL server using mysql command-line client

可以在本地环境中使用常用的客户端工具(mysql.exe 命令行工具)连接到服务器。You can connect to your server using a popular client tool, mysql.exe command-line tool on your local environment.

   mysql -h mydemoserver.mysql.database.chinacloudapi.cn -u myadmin@mydemoserver -p

清理资源Clean up resources

如果不需要将这些资源用于其他快速入门/教程,则可通过执行以下命令将其删除:If you don't need these resources for another quickstart/tutorial, you can delete them by doing the following command:

az group delete --name myresourcegroup

若要删除新创建的服务器,可运行 az mysql server delete 命令。If you would just like to delete the one newly created server, you can run az mysql server delete command.

az mysql server delete --resource-group myresourcegroup --name mydemoserver

后续步骤Next steps