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

本快速入门介绍如何使用 PowerShell 在 Azure 资源组中创建 Azure Database for MariaDB 服务器。This quickstart describes how to use PowerShell to create an Azure Database for MariaDB server in an Azure resource group. 可以使用 PowerShell 以交互方式或者通过脚本创建和管理 Azure 资源。You can use PowerShell to create and manage Azure resources interactively or in scripts.

先决条件Prerequisites

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

本文要求安装 Az PowerShell 模块,并使用 Connect-AzAccount cmdlet 连接到 Azure 帐户。This article requires that you install the Az PowerShell module and connect to your Azure account using the Connect-AzAccount cmdlet. 有关安装 Az PowerShell 模块的详细信息,请参阅安装 Azure PowerShellFor more information about installing the Az PowerShell module, see Install Azure PowerShell.

重要

尽管 Az.MariaDb PowerShell 模块为预览版,但必须使用以下命令从 Az PowerShell 模块单独安装它:Install-Module -Name Az.MariaDb -AllowPrereleaseWhile the Az.MariaDb PowerShell module is in preview, you must install it separately from the Az PowerShell module using the following command: Install-Module -Name Az.MariaDb -AllowPrerelease.

如果这是你第一次使用 Azure Database for MariaDB 服务,必须注册 Microsoft.DBforMariaDB 资源提供程序。If this is your first time using the Azure Database for MariaDB service, you must register the Microsoft.DBforMariaDB resource provider.

Register-AzResourceProvider -ProviderNamespace Microsoft.DBforMariaDB

如果有多个 Azure 订阅,请选择应当计费的资源所在的相应订阅。If you have multiple Azure subscriptions, choose the appropriate subscription in which the resources should be billed. 使用 Set-AzContext cmdlet 选择特定的订阅 ID。Select a specific subscription ID using the Set-AzContext cmdlet.

Set-AzContext -SubscriptionId 00000000-0000-0000-0000-000000000000

创建资源组Create a resource group

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

以下示例在“中国东部 2”区域创建名为“myresourcegroup”的资源组 。The following example creates a resource group named myresourcegroup in the China East 2 region.

New-AzResourceGroup -Name myresourcegroup -Location chinaeast2

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

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

下表列出了 New-AzMariaDbServer cmdlet 的常用参数和示例值。The following table contains a list of commonly used parameters and sample values for the New-AzMariaDbServer cmdlet.

设置Setting 示例值Sample value 说明Description
名称Name mydemoservermydemoserver 选择用于标识 Azure Database for MariaDB 服务器的在 Azure 中全局唯一的名称。Choose a globally unique name in Azure that identifies your Azure Database for MariaDB server. 该服务器名称只能包含字母、数字和连字符 (-)。The server name can only contain letters, numbers, and the hyphen (-) character. 在创建过程中,指定的任何大写字符将自动转换为小写。Any uppercase characters that are specified are automatically converted to lowercase during the creation process. 必须包含 3 到 63 个字符。It must contain from 3 to 63 characters.
ResourceGroupNameResourceGroupName myresourcegroupmyresourcegroup 提供 Azure 资源组的名称。Provide the name of the Azure resource group.
SKUSku GP_Gen5_2GP_Gen5_2 SKU 的名称。The name of the SKU. 请遵循简写约定:pricing-tier_compute-generation_vCores。Follows the convention pricing-tier_compute-generation_vCores in shorthand. 有关 SKU 参数的详细信息,请参阅此表格后面的信息。For more information about the Sku parameter, see the information following this table.
BackupRetentionDayBackupRetentionDay 77 备份保留时间。How long a backup should be retained. 单位为天。Unit is days. 范围为 7-35。Range is 7-35.
GeoRedundantBackupGeoRedundantBackup EnabledEnabled 是否应为此服务器启用异地冗余备份。Whether geo-redundant backups should be enabled for this server or not. 不能为基本定价层级中的服务器启用此值,并且在创建服务器后无法更改此值。This value cannot be enabled for servers in the basic pricing tier and it cannot be changed after the server is created. 允许的值:“Enabled”、“Disabled”。Allowed values: Enabled, Disabled.
位置Location chinaeast2chinaeast2 服务器的 Azure 区域。The Azure region for the server.
SslEnforcementSslEnforcement EnabledEnabled 是否应该为此服务器启用 SSL。Whether SSL should be enabled or not for this server. 允许的值:“Enabled”、“Disabled”。Allowed values: Enabled, Disabled.
StorageInMbStorageInMb 5120051200 服务器的存储容量(以 MB 为单位)。The storage capacity of the server (unit is megabytes). 有效的 StorageInMb 最小为 5120 MB,以 1024 MB 为增量递增。Valid StorageInMb is a minimum of 5120 MB and increases in 1024 MB increments. 有关存储大小限制的详细信息,请参阅 Azure Database for MariaDB 定价层For more information about storage size limits, see Azure Database for MariaDB pricing tiers.
版本Version 5.75.7 MariaDB 主版本。The MariaDB major version.
AdministratorUserNameAdministratorUserName myadminmyadmin 管理员的登录用户名。The username for the administrator login. 不能是 azure_superuseradminadministratorrootguestpublicIt cannot be azure_superuser, admin, administrator, root, guest, or public.
AdministratorLoginPasswordAdministratorLoginPassword <securestring> 管理员用户的密码,采用安全字符串格式。The password of the administrator user in the form of a secure string. 该密码必须包含 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 参数值遵循 pricing-tier_compute-generation_vCores 约定,如以下示例所示 。The Sku parameter value follows the convention pricing-tier_compute-generation_vCores as shown in the following examples.

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

有关各区域和层级的有效 SKU 值的信息,请参阅 Azure Database for MariaDB 定价层For information about valid Sku values by region and for tiers, see Azure Database for MariaDB pricing tiers.

以下示例使用服务器管理员登录名 myadmin 在“中国东部 2”区域中的“myresourcegroup”资源组内创建名为“mydemoserver”的 MariaDB 服务器 。The following example creates a MariaDB server in the China East 2 region named mydemoserver in the myresourcegroup resource group with a server admin login of myadmin. 此服务器是常规用途定价层中的第 5 代服务器,其中启用了 2 个 vCore 和异地冗余备份。It is a Gen 5 server in the general-purpose pricing tier with 2 vCores and geo-redundant backups enabled. 记下示例的第一行中使用的密码,因为这是 MariaDB 服务器管理员帐户的密码。Document the password used in the first line of the example as this is the password for the MariaDB server admin account.

提示

服务器名称映射到 DNS 名称,必须在 Azure 中全局唯一。A server name maps to a DNS name and must be globally unique in Azure.

$Password = Read-Host -Prompt 'Please enter your password' -AsSecureString
New-AzMariaDbServer -Name mydemoserver -ResourceGroupName myresourcegroup -Sku GP_Gen5_2 -GeoRedundantBackup Enabled -Location chinaeast2 -AdministratorUsername myadmin -AdministratorLoginPassword $Password

如果轻量级计算和 I/O 足以满足工作负荷要求,请考虑使用基本定价层级。Consider using the basic pricing tier if light compute and I/O are adequate for your workload.

重要

在基本定价层中创建的服务器以后无法扩展到常规用途或内存优化层级,并且无法异地复制。Servers created in the basic pricing tier cannot be later scaled to general-purpose or memory- optimized and cannot be geo-replicated.

配置防火墙规则Configure a firewall rule

使用 New-AzMariaDbFirewallRule cmdlet 创建 Azure Database for MariaDB 服务器级防火墙规则。Create an Azure Database for MariaDB server-level firewall rule using the New-AzMariaDbFirewallRule cmdlet. 服务器级防火墙规则允许外部应用程序(如 mysql 命令行工具或 MariaDB Workbench)穿过 Azure Database for MariaDB 服务防火墙连接到服务器。A server-level firewall rule allows an external application, such as the mysql command-line tool or MariaDB 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 named 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 correspond to the location that you are connecting from.

New-AzMariaDbFirewallRule -Name AllowMyIP -ResourceGroupName myresourcegroup -ServerName mydemoserver -StartIPAddress 192.168.0.1 -EndIPAddress 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 within a corporate network, outbound traffic over port 3306 might not be allowed. 在这种情况下,仅当 IT 部门打开了端口 3306 时,才能连接到服务器。In this scenario, you can only connect to the server 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 ensures the security of in-motion data by encrypting the data stream over the Internet. 就本快速入门来说,请禁用服务器的 SSL 连接。For this quickstart, disable SSL connections for your server. 有关详细信息,请参阅配置应用程序中的 SSL 连接性以安全连接到 Azure Database for MariaDBFor more information, see Configure SSL connectivity in your application to securely connect to Azure Database for MariaDB.

警告

建议不要对生产服务器禁用 SSL。Disabling SSL is not recommended for production servers.

以下示例在 Azure Database for MariaDB 服务器上禁用 SSL。The following example disables SSL on your Azure Database for MariaDB server.

Update-AzMariaDbServer -Name mydemoserver -ResourceGroupName myresourcegroup -SslEnforcement Disabled

获取连接信息Get the connection information

若要连接到服务器,需要提供主机信息和访问凭据。To connect to your server, you need to provide host information and access credentials. 使用以下示例来确定连接信息。Use the following example to determine the connection information. 记下 FullyQualifiedDomainName 和 AdministratorLogin 的值。Make a note of the values for FullyQualifiedDomainName and AdministratorLogin.

Get-AzMariaDbServer -Name mydemoserver -ResourceGroupName myresourcegroup |
  Select-Object -Property FullyQualifiedDomainName, AdministratorLogin
FullyQualifiedDomainName                    AdministratorLogin
------------------------                    ------------------
mydemoserver.mariadb.database.chinacloudapi.cn       myadmin

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

使用 mysql 命令行工具连接到服务器。Connect to your server using the mysql command-line tool. 若要下载并安装该命令行工具,请参阅 MySQL 社区下载To download and install the command-line tool, see MySQL Community Downloads.

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

    mysql -h <servername>.mariadb.database.chinacloudapi.cn -u myadmin@<servername> -p
    
  2. 查看服务器状态。View server status.

    mysql> status
    
    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 64793
    Server version: 5.6.42.0 MariaDB Server
    
    Copyright (c) 2000, 2020, 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
    --------------
    /usr/bin/mysql  Ver 14.14 Distrib 5.7.29, for Linux (x86_64) using  EditLine wrapper
    
    Connection id:          64793
    Current database:
    Current user:           myadmin@myipaddress
    SSL:                    Cipher in use is ECDHE-RSA-AES256-GCM-SHA384
    Current pager:          stdout
    Using outfile:          ''
    Using delimiter:        ;
    Server version:         5.6.42.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:                 17 min 4 sec
    
    Threads: 19  Questions: 482  Slow queries: 0  Opens: 50  Flush tables: 3  Open tables: 12  Queries per second avg: 0.470
    --------------
    
    mysql>
    

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

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

  1. 启动客户端计算机上的 MySQL Workbench 应用程序。Launch the MySQL Workbench application on your client computer. 若要下载并安装 MySQL Workbench,请参阅下载 MySQL WorkbenchTo download and install MySQL Workbench, see Download MySQL Workbench.

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

    设置新连接

    设置Setting 建议的值Suggested Value 说明Description
    连接名称Connection Name 我的连接My Connection 指定此连接的标签Specify a label for this connection
    连接方法Connection Method 标准 (TCP/IP)Standard (TCP/IP) 使用 TCP/IP 协议连接到 Azure Database for MariaDBUse TCP/IP protocol to connect to Azure Database for MariaDB
    主机名Hostname mydemoserver.mariadb.database.chinacloudapi.cn 你在前面记下的服务器名称。Server name you previously noted
    端口Port 33063306 MariaDB 的默认端口The default port for MariaDB
    用户名Username myadmin@mydemoserver 你在前面记下的服务器管理员登录名The server admin login you previously noted
    密码Password ************* 使用前面配置的管理员帐户密码Use the admin account password you configured earlier
  3. 若要测试是否正确配置了参数,请单击“测试连接”按钮。To test if the parameters are configured correctly, click the Test Connection button.

  4. 选择用于连接到服务器的连接。Select the connection to connect to the server.

清理资源Clean up resources

如果其他快速入门或教程不需要使用本快速入门中创建的资源,可以运行以下示例将其删除。If the resources created in this quickstart aren't needed for another quickstart or tutorial, you can delete them by running the following example.

注意

以下示例删除指定的资源组及其包含的所有资源。The following example deletes the specified resource group and all resources contained within it. 如果指定的资源组中存在本快速入门范围外的资源,这些资源也会被删除。If resources outside the scope of this quickstart exist in the specified resource group, they will also be deleted.

Remove-AzResourceGroup -Name myresourcegroup

若要仅删除本快速入门中创建的服务器而不删除资源组,请使用 Remove-AzMariaDbServer cmdlet。To delete only the server created in this quickstart without deleting the resource group, use the Remove-AzMariaDbServer cmdlet.

Remove-AzMariaDbServer -Name mydemoserver -ResourceGroupName myresourcegroup

后续步骤Next steps