快速入门:使用 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 资源组中为 MySQL 服务器创建 Azure 数据库。This quickstart describes how to use the Azure CLI to create an Azure Database for MySQL server in an Azure resource group in about five minutes. Azure CLI 用于从命令行或脚本创建和管理 Azure 资源。The Azure CLI is used to create and manage Azure resources from the command line or in scripts.

如果没有 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.

如果有多个订阅,请选择资源所在的相应订阅或对资源进行计费的订阅。If you have multiple subscriptions, choose the appropriate subscription in which the resource exists or is billed for. 使用 az account set 命令选择帐户下的特定订阅 ID。Select a specific subscription ID under your account using az account set command.

az account set --subscription 00000000-0000-0000-0000-000000000000

创建资源组Create a resource group

使用 az group create 命令创建 Azure 资源组Create an Azure resource group using the az group create command. 资源组是在其中以组的形式部署和管理 Azure 资源的逻辑容器。A resource group is a logical container into which Azure resources are deployed and managed as a group.

以下示例在 chinaeast 位置创建名为 myresourcegroup 的资源组。The following example creates a resource group named myresourcegroup in the chinaeast location.

az group create --name myresourcegroup --location chinaeast

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

使用 az mysql server create 命令创建 Azure Database for MySQL 服务器。Create an Azure Database for MySQL server with the az mysql server create command. 一个服务器可以管理多个数据库。A server can manage multiple databases. 通常,每个项目或每个用户使用一个单独的数据库。Typically, a separate database is used for each project or for each user.

设置Setting 示例值Sample value 说明Description
namename mydemoservermydemoserver 选择用于标识 Azure Database for MySQL 服务器的唯一名称。Choose a unique name that identifies 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.
sku-namesku-name GP_Gen4_2GP_Gen4_2 SKU 的名称。The name of the sku. 遵循约定“{定价层}_{计算代系}_{vCores}”的简写形式。Follows the convention {pricing tier}_{compute generation}_{vCores} in shorthand. 参阅下表详细了解 sku-name 参数。See below this table for more information about the sku-name parameter.
backup-retentionbackup-retention 77 备份保留时间。How long a backup should be retained. 单位为天。Unit is days. 范围为 7-35。Range is 7-35.
geo-redundant-backupgeo-redundant-backup 已禁用Disabled 是否应为此服务器启用异地冗余备份。Whether geo-redundant backups should be enabled for this server or not. 允许的值:“Enabled”、“Disabled”。Allowed values: Enabled, Disabled.
locationlocation chinaeastchinaeast 服务器的 Azure 位置。The Azure location for the server.
ssl-enforcementssl-enforcement EnabledEnabled 是否应为此服务器启用 SSL。Whether ssl should be enabled or not for this server. 允许的值:“Enabled”、“Disabled”。Allowed values: Enabled, Disabled.
storage-sizestorage-size 5120051200 服务器的存储容量(以 MB 为单位)。The storage capacity of the server (unit is megabytes). 有效的 storage-size 最小为 5120MB,以 1024MB 为增量递增。Valid storage-size is minimum 5120MB and increases in 1024MB increments. 有关存储大小限制的详细信息,请参阅定价层文档。See the pricing tiers document for more information about storage size limits.
版本version 5.75.7 MySQL 主版本。The MySQL major version.
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-name 参数值遵循 {定价层}_{计算层代}_{vCore 数} 约定,如以下示例中所示:The sku-name parameter value follows the convention {pricing tier}_{compute generation}_{vCores} as in the examples below:

  • --sku-name B_Gen4_1 映射到基本、第 4 代和 1 个 vCore。--sku-name B_Gen4_1 maps to Basic, Gen 4, and 1 vCore. 此选项是可用的最小 SKU。This option is the smallest SKU available.
  • --sku-name GP_Gen5_32 映射到常规用途、第 5 层和 32 个 vCore。--sku-name GP_Gen5_32 maps to General Purpose, Gen 5, and 32 vCores.
  • --sku-name MO_Gen5_2 映射到内存优化、第 5 层和 2 个 vCore。--sku-name MO_Gen5_2 maps to Memory Optimized, Gen 5, and 2 vCores.

请参阅定价层文档来了解适用于每个区域和每个层的有效值。Please see the pricing tiers documentation to understand the valid values per region and per tier.

以下示例使用服务器管理员登录名 myadmin 在资源组 myresourcegroup 中创建位于“中国东部”区域的名为 mydemoserver 的 MySQL 5.7 服务器。The following example creates a MySQL 5.7 server in China East named mydemoserver in your resource group myresourcegroup with server admin login myadmin. 这是第 4 代常规用途服务器,带有 2 个 vCore。This is a Gen 4 General Purpose server with 2 vCores. 用自己的值替换 <server_admin_password>Substitute the <server_admin_password> with your own value.

az mysql server create --resource-group myresourcegroup --name mydemoserver  --location chinaeast --admin-user myadmin --admin-password <server_admin_password> --sku-name GP_Gen4_2 --version 5.7


如果轻量级计算和 I/O 足以满足工作负荷要求,请考虑使用“基本”定价层。Consider using the Basic pricing tier if light compute and I/O are adequate for your workload. 请注意,在“基本”定价层中创建的服务器以后不能扩展到“常规用途”或“内存优化”定价层。Note that servers created in the Basic pricing tier cannot later be scaled to General Purpose or Memory Optimized. 有关详细信息,请参阅定价页See the pricing page for more information.

配置防火墙规则Configure firewall rule

使用 az mysql server firewall-rule create 命令创建 Azure Database for MySQL 服务器级防火墙规则。Create an Azure Database for MySQL server-level firewall rule using the az mysql server firewall-rule create command. 服务器级防火墙规则允许外部应用程序(如 mysql.exe 命令行工具或 MySQL Workbench)通过 Azure MySQL 服务防火墙连接到服务器。A server-level firewall rule allows an external application, such as the mysql.exe command-line tool or MySQL Workbench to connect to your server through the Azure MySQL service firewall.

以下示例创建名为 AllowMyIP 的防火墙规则,该规则允许从特定的 IP 地址 ( 进行连接。The following example creates a firewall rule called AllowMyIP that allows connections from a specific IP address, 替代与要从其进行连接的地址相对应的 IP 地址或 IP 地址范围。Substitute in the IP address or range of IP addresses that correspond to where you'll be connecting from.

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


连接到 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.

配置 SSL 设置Configure SSL settings

默认情况下,在服务器和客户端应用程序之间强制实施 SSL 连接。By default, SSL connections between your server and client applications are enforced. 此默认设置可通过 Internet 加密数据流确保“传输中”数据的安全性。This default ensures security of "in-motion" data by encrypting the data stream over the internet. 为使本快速入门教程更简单,请禁用服务器的 SSL 连接。To make this quick start easier, disable SSL connections for your server. 建议不要对生产服务器禁用 SSL。Disabling SSL is not recommended for production servers. 有关详细信息,请参阅配置应用程序中的 SSL 连接性以安全连接到 Azure Database for MySQLFor more information, see Configure SSL connectivity in your application to securely connect to Azure Database for MySQL.

以下示例禁止在 MySQL 服务器上强制实施 SSL。The following example disables enforcing SSL on your MySQL server.

az mysql server update --resource-group myresourcegroup --name mydemoserver --ssl-enforcement Disabled

获取连接信息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. 记下 fullyQualifiedDomainNameadministratorLoginMake 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": "chinaeast",
  "name": "mydemoserver",
  "resourceGroup": "myresourcegroup",
  "sku": {
    "capacity": 2,
    "family": "Gen4",
    "name": "GP_Gen4_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.exe 命令行工具连接到服务器Connect to the server using the mysql.exe command-line tool

使用 mysql.exe 命令行工具连接到服务器。Connect to your server using the mysql.exe command-line tool. 可从此处下载 MySQL 并将其安装在计算机上。You can download MySQL from here and install it on your computer.

键入下一命令:Type the next commands:

  1. 使用 mysql 命令行工具连接到服务器:Connect to the server using mysql command-line tool:

    mysql -h mydemoserver.mysql.database.chinacloudapi.cn -u myadmin@mydemoserver -p
  2. 查看服务器状态:View server status:

    mysql> status

    如果一切顺利,命令行工具应输出以下文本:If everything goes well, the command-line tool should output the following text:

C:\Users\>mysql -h mydemoserver.mysql.database.chinacloudapi.cn -u myadmin@mydemoserver -p
Enter password: ***********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 65512
Server version: MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> status
mysql  Ver 14.14 Distrib 5.6.35, for Win64 (x86_64)

Connection id:          65512
Current database:
Current user:           myadmin@
SSL:                    Not in use
Using delimiter:        ;
Server version: MySQL Community Server (GPL)
Protocol version:       10
Connection:             mydemoserver.mysql.database.chinacloudapi.cn via TCP/IP
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    gbk
Conn.  characterset:    gbk
TCP port:               3306
Uptime:                 2 days 9 hours 47 min 20 sec

Threads: 4  Questions: 34833  Slow queries: 2  Opens: 84  Flush tables: 4  Open tables: 1  Queries per second avg: 0.167



有关其他命令,请参阅 MySQL 5.7 参考手册 - 第 4.5.1 章For additional commands, see MySQL 5.7 Reference Manual - Chapter 4.5.1.

使用 MySQL Workbench GUI 工具连接到服务器Connect to the server using the MySQL Workbench GUI tool

  1. 启动客户端计算机上的 MySQL Workbench 应用程序。Launch the MySQL Workbench application on your client computer. 可以从此处下载并安装 MySQL Workbench。You can download and install MySQL Workbench from here.

  2. 在“设置新连接”对话框的“参数”选项卡上,输入以下信息:In the Setup New Connection dialog box, enter the following information on Parameters tab:


设置Setting 建议的值Suggested Value 说明Description
连接名称Connection Name 我的连接My Connection 指定此连接的标签(这可以是任何内容)Specify a label for this connection (this can be anything)
连接方法Connection Method 选择“标准(TCP/IP)”choose Standard (TCP/IP) 使用 TCP/IP 协议连接到 Azure Database for MySQLUse TCP/IP protocol to connect to Azure Database for MySQL>
主机名Hostname mydemoserver.mysql.database.chinacloudapi.cnmydemoserver.mysql.database.chinacloudapi.cn 先前记下的服务器名称。Server name you previously noted.
端口Port 33063306 使用 MySQL 的默认端口。The default port for MySQL is used.
用户名Username myadmin@mydemoserver 先前记下的服务器管理员登录名。The server admin login you previously noted.
密码Password **** 使用之前配置的管理员帐户密码。Use the admin account password you configured earlier.

单击“测试连接”以测试是否所有参数均已正确配置。Click Test Connection to test if all parameters are correctly configured. 现在,可以单击该连接,成功连接到服务器。Now, you can click the connection to successfully connect to the server.

清理资源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