快速入门:使用 Azure CLI 创建 Azure Database for MariaDB 服务器Quickstart: Create an Azure Database for MariaDB server by using the Azure CLI

Azure CLI 可用于从命令行或脚本创建和管理 Azure 资源。You can use the Azure CLI to create and manage Azure resources from the command line or in scripts. 本快速入门教程介绍如何使用 Azure CLI 在大约 5 分钟内在 Azure 资源组中创建 Azure Database for MariaDB 服务器。This quickstart describes how to use the Azure CLI to create an Azure Database for MariaDB server in an Azure resource group in about five minutes.

如果没有 Azure 订阅,请在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

就本快速入门来说,必须运行 Azure CLI 2.0 或更高版本。For this quickstart, you must run Azure CLI version 2.0 or later. 运行 az --version 即可查找版本。Run az --version to find the version. 如果需要安装或升级 CLI,请参阅安装 Azure CLI 2.0If you need to install or upgrade the CLI, see Install Azure CLI 2.0.

如果有多个订阅,请选择要计费的资源所在的订阅,或者本身要计费的订阅。If you have multiple subscriptions, choose the subscription that contains the resource or the subscription in which you are billed. 若要选择帐户中的特定订阅 ID,请使用 az account set 命令:To select a specific subscription ID in your account, use the 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 by using the az group create command. 资源组是在其中以组的形式部署和管理 Azure 资源的逻辑容器。A resource group is a logical container in which Azure resources are deployed and managed as a group.

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

az group create --name myresourcegroup --location chinaeast2

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

使用 az mariadb server create 命令创建 Azure Database for MariaDB 服务器。Create an Azure Database for MariaDB server by using the az mariadb 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 MariaDB 服务器的唯一名称。Enter a unique name that identifies your Azure Database for MariaDB server. 服务器名称只能包含小写字母、数字和连字符 (-) 字符。The server name can contain only lowercase letters, numbers, and the hyphen (-) character. 它必须包含 3 到 63 个字符。It must contain between 3 and 63 characters.
resource-groupresource-group myresourcegroupmyresourcegroup 输入 Azure 资源组的名称。Enter the name of the Azure resource group.
sku-namesku-name GP_Gen5_2GP_Gen5_2 SKU 的名称。The name of the SKU. 请遵循简写约定:定价层_计算代_vCore 数Follows the convention pricing tier_compute generation_vCores in shorthand. 有关 sku-name 参数的详细信息,请查看此表后面的部分。For more information about the sku-name parameter, see the section after this table.
backup-retentionbackup-retention 77 保留备份的时长。How long a backup should be retained. 单位为天。The unit is days. 范围:7 到 35。Range: 7 to 35.
geo-redundant-backupgeo-redundant-backup 已禁用Disabled 是否应该为此服务启用异地冗余备份。Whether geo-redundant backups should be enabled for this server. 允许的值:EnabledDisabledAllowed values: Enabled, Disabled.
locationlocation chinaeast2chinaeast2 服务器的 Azure 位置。The Azure location for the server.
ssl-enforcementssl-enforcement 已启用Enabled 是否应该为此服务器启用 SSL。Whether SSL should be enabled for this server. 允许的值:EnabledDisabledAllowed values: Enabled, Disabled.
storage-sizestorage-size 5120051200 服务器的存储容量(单位是兆字节)。The storage capacity of the server (the unit is megabytes). 有效的存储大小最小为 5120 MB,以 1024 MB 为增量。Valid storage sizes are 5,120 MB (minimum) with increases in 1,024-MB increments. 有关存储大小限制的详细信息,请参阅定价层For more information about storage size limits, see Pricing tiers.
版本version 10.210.2 MariaDB 主要引擎版本。The MariaDB major engine version.
admin-useradmin-user myadminmyadmin 用于管理员登录的用户名。The user name for the administrator login. admin-user 参数不能是“azure_superuser”、“admin”、“administrator”、“root”、“guest”或“public”。 The admin-user parameter can't be azure_superuser, admin, administrator, root, guest, or public.
admin-passwordadmin-password 你的密码 your password 管理员用户的密码。The password of the administrator user. 密码必须包含 8 到 128 个字符。Your password must contain between 8 and 128 characters. 密码必须包含以下三个类别的字符:英文大写字母、英文小写字母、数字和非字母数字字符。It 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_Gen5_1 映射到基本、第 5 代和 1 个 vCore。--sku-name B_Gen5_1 maps to Basic, Gen 5, 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.

若要按区域和层了解有效值,请参阅定价层For information about valid values by region and for tiers, see Pricing tiers.

以下示例在“中国东部 2”区域创建一个名为 mydemoserver 的服务器。The following example creates a server named mydemoserver in the China East 2 region. 该服务器在资源组 myresourcegroup 中,其服务器管理员登录名为 myadminThe server is in the resource group myresourcegroup and has the server admin login myadmin. 该服务器为“第 5 代”服务器,属于“常规用途”定价层,有 2 个 vCore。The server is a Gen 5 server in the General Purpose pricing tier and it has 2 vCores. 服务器名称映射到 DNS 名称,必须在 Azure 中全局唯一。A server name maps to a DNS name and must be globally unique in Azure. <server_admin_password> 替换为你自己的服务器管理员密码。Substitute <server_admin_password> with your own server admin password.

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

备注

如果轻量级计算和 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 a firewall rule

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

以下示例创建名为 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 地址或 IP 地址范围,这些地址或地址范围对应于要从其进行连接的位置。Substitute an IP address or range of IP addresses that corresponds to the location you connect from.

az mariadb 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 MariaDB 时,经端口 3306 进行通信。Connections to Azure Database for MariaDB communicate over port 3306. 如果尝试从企业网络内部进行连接,则可能不允许经端口 3306 的出站流量。If you try to connect from inside a corporate network, outbound traffic over port 3306 might not be allowed. 在这种情况下,若要连接到服务器,必须要求 IT 部门打开端口 3306。In this case, you can connect to your server only if 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 setting ensures security of "in-motion" data by encrypting the data stream over the internet. 就本快速入门来说,请禁用服务器的 SSL 连接。For this quickstart, disable SSL connections for your server. 建议不要对生产服务器禁用 SSL。Disabling SSL is not recommended for production servers. 有关详细信息,请参阅配置应用程序中的 SSL 连接性以安全连接到 Azure Database for MariaDBFor more information, see Configure SSL connectivity in your application to securely connect to Azure Database for MariaDB.

以下示例禁止在 Azure Database for MariaDB 服务器上强制实施 SSL:The following example disables SSL enforcing on your Azure Database for MariaDB server:

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

获取连接信息Get connection information

若要连接到服务器,需要提供主机信息和访问凭据。To connect to your server, you need to provide host information and access credentials. 若要获取连接信息,请运行以下命令:To get the connection information, run the following command:

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

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

{
  "administratorLogin": "myadmin",
  "earliestRestoreDate": null,
  "fullyQualifiedDomainName": "mydemoserver.mariadb.database.chinacloudapi.cn",
  "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myresourcegroup/providers/Microsoft.DBforMariaDB/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.DBforMariaDB/servers",
  "userVisibleState": "Ready",
  "version": "10.2"
}

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

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

若要使用 mysql 命令行工具连接到服务器,请执行以下操作:To connect to the server by using the mysql command-line tool:

  1. 连接到服务器:Connect to the server:

    mysql -h mydemoserver.mariadb.database.chinacloudapi.cn -u myadmin@mydemoserver -p
    
  2. mysql> 提示符处查看服务器状态:View the server status at the mysql> prompt:

    status
    

    应会看到类似于以下文本的内容:You should see something similar to the following text:

    C:\Users\>mysql -h mydemoserver.mariadb.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: 5.6.39.0 MariaDB Server
    
    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
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> status
    --------------
    mysql  Ver 14.14 Distrib 5.7.23, for Linux (x86_64)
    
    Connection id:          64681
    Current database:
    Current user:           myadmin@40.118.201.21
    SSL:                    Cipher in use is AES256-SHA
    Current pager:          stdout
    Using outfile:          ''
    Using delimiter:        ;
    Server version:         5.6.39.0 MariaDB Server
    Protocol version:       10
    Connection:             mydemoserver.mariadb.database.chinacloudapi.cn via TCP/IP
    Server characterset:    latin1
    Db     characterset:    latin1
    Client characterset:    utf8
    Conn.  characterset:    utf8
    TCP port:               3306
    Uptime:                 1 day 3 hours 28 min 50 sec
    
    Threads: 10  Questions: 29002  Slow queries: 0  Opens: 33  Flush tables: 3  Open tables: 1  Queries per second avg: 0.293
    --------------
    
    mysql>
    

提示

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

使用 MySQL Workbench 连接服务器Connect to the server by using MySQL Workbench

  1. 打开客户端计算机上的 MySQL Workbench。Open MySQL Workbench on your client computer. 如果尚未安装,请下载并安装此应用程序。If it's not already installed, download and install the application.

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

    设置新连接

    设置Setting 建议的值Suggested value 说明Description
    连接名称Connection Name 演示连接Demo connection 输入此连接的标签(连接名称可以是任何内容)Enter a label for this connection (the connection name can be anything)
    连接方法Connection Method 标准 (TCP/IP)Standard (TCP/IP) 使用 TCP/IP 协议连接到 Azure Database for MariaDBUse the TCP/IP protocol to connect to Azure Database for MariaDB
    主机名Hostname mydemoserver.mariadb.database.chinacloudapi.cnmydemoserver.mariadb.database.chinacloudapi.cn 前面记下的服务器名称。The server name that you noted earlier.
    端口Port 33063306 Azure Database for MariaDB 的默认端口。The default port for Azure Database for MariaDB.
    用户名Username myadmin@mydemoservermyadmin@mydemoserver 前面记下的服务器管理员登录名。The server admin login that you noted earlier.
    密码Password 你的密码 your password 使用之前设置的管理员帐户密码。Use the admin account password that you set up earlier.
  3. 若要检查所有参数是否已正确配置,请选择“测试连接” 。To check whether all parameters are configured correctly, select Test Connection.

  4. 选择该连接,成功连接到服务器。Select the connection to successfully connect to the server.

清理资源Clean up resources

如果不需要将本快速入门中使用的资源用于其他快速入门或教程,则可通过运行以下命令将其删除:If you don't need the resources that you used in this quickstart for another quickstart or tutorial, you can delete them by running the following command:

az group delete --name myresourcegroup

如果只需删除在本快速入门中创建的服务器,请运行 az mariadb server delete 命令:If you want to delete only the server that you created in this quickstart, run the az mariadb server delete command:

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

后续步骤Next steps