教程:使用 Azure 门户设计 Azure Database for MariaDB 数据库Tutorial: Design an Azure Database for MariaDB database by using the Azure portal

Azure Database for MariaDB 是一种托管服务,可用于在云中运行、管理和缩放高可用性的 MySQL 数据库。Azure Database for MariaDB is a managed service you can use to run, manage, and scale highly available MySQL databases in the cloud. 使用 Azure 门户可以轻松管理服务器和设计数据库。By using the Azure portal, you can easily manage your server and design a database.

本教程介绍如何使用 Azure 门户完成以下操作:In this tutorial, you use the Azure portal to learn how to:

  • 创建 Azure Database for MariaDBCreate an Azure Database for MariaDB
  • 配置服务器防火墙Configure the server firewall
  • 使用 mysql 命令行工具创建数据库Use the mysql command-line tool to create a database
  • 加载示例数据Load sample data
  • 查询数据Query data
  • 更新数据Update data
  • 还原数据Restore data

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

登录到 Azure 门户Sign in to the Azure portal

在浏览器中转到 Azure 门户In your browser, go to the Azure portal. 输入登录到门户所需的凭据。Enter your credentials to sign in to the portal. 默认视图是服务仪表板。The default view is your service dashboard.

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

创建 Azure Database for MariaDB 服务器时,请使用定义好的一组计算和存储资源You create an Azure Database for MariaDB server with a defined set of compute and storage resources. 将在 Azure 资源组中创建服务器。The server is created in an Azure resource group.

  1. 选择门户左上角的“创建资源”按钮 (+) 。Select the Create a resource button (+) in the upper left corner of the portal.

  2. 在搜索框中键入“MariaDB”以查找该服务 。Type MariaDB in the search box to find the service.

    转到 MySQL

  3. 选择“Azure Database for MariaDB” 磁贴,然后选择“创建” 。Select the Azure Database for MariaDB tile, and then select Create. 输入或选择所需信息。Enter or select the required information.

    创建窗体

    设置Setting 建议的值Suggested value 字段说明Field description
    服务器名称Server name 唯一的服务器名称a unique server name 选择用于标识 Azure Database for MariaDB 服务器的唯一名称。Choose a unique name that identifies your Azure Database for MariaDB server. 例如,mydemoserverFor example, mydemoserver. 域名 .mariadb.database.chinacloudapi.cn 将追加到你输入的服务器名称后面。The domain name .mariadb.database.chinacloudapi.cn is appended to the server name you enter. 服务器名称只能包含小写字母、数字和连字符 (-) 字符。The server name can contain only lowercase letters, numbers, and the hyphen (-) character. 它必须包含 3 到 63 个字符。It must contain between 3 and 63 characters.
    订阅Subscription 订阅your subscription 选择要用于服务器的 Azure 订阅。Select the Azure subscription that you want to use for your server. 如果有多个订阅,请选择要计费的资源所在的订阅。If you have multiple subscriptions, choose the subscription in which you are billed for the resource.
    资源组Resource group myresourcegroupmyresourcegroup 输入新的资源组名称,或选择现有的资源组。Enter a new resource group name or select an existing resource group.
    选择源Select source 空白Blank 选择“空白” 即可创建新服务器。Select Blank to create a new server. (如果要从现有 Azure Database for MariaDB 服务器的异地备份创建服务器,请选择“备份” )。(If you're creating a server from a geo-backup of an existing Azure Database for MariaDB server, select Backup).
    服务器管理员登录名Server admin login myadminmyadmin 连接到服务器时需要使用的登录帐户。A sign-in account to use when you connect to the server. 管理员登录名不能是“azure_superuser”、“admin”、“administrator”、“root”、“guest”或“public”。 The admin sign-in name can't be azure_superuser, admin, administrator, root, guest, or public.
    密码Password 由用户选择your choice 为服务器管理员帐户输入新密码。Enter a new password for the server admin account. 该密码必须包含 8 到 128 个字符。It must contain between 8 and 128 characters. 密码必须包含以下三个类别的字符:英文大写字母、英文小写字母、数字 (0-9)和非字母数字字符(!, $, #, % 等)。Your password must contain characters from three of the following categories: English uppercase letters, English lowercase letters, numbers (0-9), and non-alphanumeric characters (!, $, #, %, and so on).
    确认密码Confirm password 由用户选择your choice 确认管理员帐户密码。Confirm the admin account password.
    位置Location 离用户最近的区域the region closest to your users 选择最靠近用户或其他 Azure 应用程序的位置。Select the location that is closest to your users or to your other Azure applications.
    版本Version 最新版本the latest version 最新版本,有特定要求(即要求使用其他版本)的除外。The latest version (unless you have specific requirements for using a different version).
    定价层Pricing tier 请参阅说明。See description. 新服务器的计算、存储和备份配置。The compute, storage, and backup configurations for your new server. 选择“定价层” > “常规用途”。Select Pricing tier > General Purpose. 为以下设置保留默认值:Keep the default values for the following settings:
    • 计算代系(第 5 代)Compute Generation (Gen 5)
    • vCore(4 个 vCore)vCore (4 vCores)
    • 存储空间 (100 GB)Storage (100 GB)
    • 备份保持期(7 天)Backup Retention Period (7 days)

    若要在异地冗余存储中启用服务器备份,请选择“异地冗余”作为“备份冗余选项” 。To enable your server backups in geo-redundant storage, for Backup Redundancy Options, select Geographically Redundant.

    若要保存此定价层选择,请选择“确定” 。To save this pricing tier selection, select OK. 下一个屏幕截图捕获了这些选择。The next screenshot captures these selections.

    定价层

  4. 选择“创建” 。Select Create. 一两分钟后,新的 Azure Database for MariaDB 服务器将在云中运行。In a minute or two, a new Azure Database for MariaDB server is running in the cloud. 若要监视部署过程,请在工具栏上选择“通知”。 To monitor the deployment process, select Notifications on the toolbar.

配置防火墙Configure the firewall

Azure Database for MariaDB 受防火墙保护。An Azure Database for MariaDB is protected by a firewall. 默认情况下,将拒绝与服务器和服务器内数据库的所有连接。By default, all connections to the server and to databases inside the server are rejected. 首次连接到 Azure Database for MariaDB 之前,请配置防火墙以添加客户端计算机的公共网络 IP 地址(或 IP 地址范围)。Before you connect to Azure Database for MariaDB for the first time, configure the firewall to add the client computer's public network IP address (or the IP address range).

  1. 选择新创建的服务器,然后选择“连接安全性” 。Select your newly created server, and then select Connection security.

    连接安全性

  2. 可以选择“添加我的 IP” 或在此处配置防火墙规则。You can select Add My IP or configure firewall rules here. 创建规则后请记得选择“保存” 。Remember to select Save after you create the rules.

现在可以使用 mysql 命令行工具或 MySQL Workbench 来连接到服务器。You can now connect to the server by using the mysql command-line tool or MySQL Workbench.

Tip

Azure Database for MariaDB 服务器通过端口 3306 进行通信。The Azure Database for MariaDB server communicates over port 3306. 如果尝试从企业网络内部进行连接,则网络的防火墙可能不允许经端口 3306 的出站流量。If you try to connect from inside a corporate network, outbound traffic over port 3306 might not be allowed by your network's firewall. 这种情况下,若要连接到Azure Database for MariaDB 服务器,必须要求 IT 部门打开端口 3306。In this case, to connect to the Azure Database for MariaDB server, your IT department must open port 3306.

获取连接信息Get connection information

从 Azure 门户获取 Azure Database for MariaDB 服务器的“服务器名称”(完全限定)和“服务器管理员登录名” 。Get values for Server name (fully qualified) and Server admin login name for your Azure Database for MariaDB server from the Azure portal. 使用 mysql 命令行工具通过完全限定的服务器名称连接到服务器。You use the fully qualified server name to connect to your server by using the mysql command-line tool.

  1. Azure 门户的左侧菜单中,选择“所有资源”。 In the Azure portal, in the left menu, select All resources. 输入服务器名称,搜索 Azure Database for MariaDB 服务器。Enter the server name and search for your Azure Database for MariaDB server. 选择服务器名称以查看服务器详细信息。Select the server name to view the server details.

  2. 在“概览”页中记下“服务器名称”和“服务器管理员登录名”的值。 On the Overview page, make a note of the values for Server name and Server admin login name. 也可选择每个字段旁边的复制按钮,将值复制到剪贴板。You can also select the copy button next to each field to copy the value to the clipboard.

    服务器属性

在我们的示例中,服务器名称是 mydemoserver.mariadb.database.chinacloudapi.cn,服务器管理员登录名是 myadmin@mydemoserverIn our example, the server name is mydemoserver.mariadb.database.chinacloudapi.cn and the server admin login name is myadmin@mydemoserver.

通过 mysql 连接到服务器Connect to the server by using mysql

使用 mysql 命令行工具建立与 Azure Database for MariaDB 服务器的连接。Use the mysql command-line tool to establish a connection to your Azure Database for MariaDB server.

输入进行连接的命令:Enter the command to connect:

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

创建空数据库Create a blank database

连接到服务器后,请创建一个要使用的空数据库:When you’re connected to the server, create a blank database to work with:

CREATE DATABASE mysampledb;

出现提示时,请运行以下命令,将连接切换到新建的数据库:At the prompt, run the following command to switch the connection to your newly created database:

USE mysampledb;

在数据库中创建表Create tables in the database

了解如何连接到 Azure Database for MariaDB 数据库以后,可以完成一些基本任务。Now that you know how to connect to the Azure Database for MariaDB database, you can complete some basic tasks.

首先,创建表并加载一些数据。First, create a table and load it with some data. 创建一个存储清单信息的表:Let's create a table that stores inventory information:

CREATE TABLE inventory (
    id serial PRIMARY KEY, 
    name VARCHAR(50), 
    quantity INTEGER
);

将数据加载到表Load data in the tables

表创建好后,可向其插入一些数据。Now that you have a table, insert some data in the table. 在打开的命令提示符窗口中运行以下查询,插入几行数据:In the open Command Prompt window, run the following query to insert some rows of data:

INSERT INTO inventory (id, name, quantity) VALUES (1, 'banana', 150); 
INSERT INTO inventory (id, name, quantity) VALUES (2, 'orange', 154);

查询和更新表中的数据Query and update the data in the tables

运行以下查询,从数据库表中检索信息:Run the following query to retrieve information from the database table:

SELECT * FROM inventory;

还可以更新表中的数据:You can also update the data in the tables:

UPDATE inventory SET quantity = 200 WHERE name = 'banana';

检索数据时行也会进行更新:The row is updated when you retrieve data:

SELECT * FROM inventory;

将数据库还原到以前的时间点Restore a database to a previous point in time

假设你意外删除了一个重要的数据库表,并且无法轻松地恢复数据。Imagine that you accidentally deleted an important database table and can't recover the data easily. 在 Azure Database for MariaDB 中,可以将服务器还原到某个时间点,方法是在新服务器上创建数据库的副本。In Azure Database for MariaDB, you can restore the server to a point in time by creating a copy of the databases on your new server. 可以使用此新服务器恢复已删除的数据。You can use this new server to recover your deleted data. 以下步骤将示例服务器还原到添加此表之前的时间点:The following steps restore the sample server to a point in time before the table was added:

  1. 在 Azure 门户中,找到 Azure Database for MariaDB。In the Azure portal, locate your Azure Database for MariaDB. 在“概览”页上,选择“还原”。 On the Overview page, select Restore.

    还原数据库

  2. 在“还原”页上,输入或选择以下信息: On the Restore page, enter or select the following information:

    “还原”窗体

    • 还原点:在列出的时间范围内选择要还原到的时间点。Restore point: Select a point in time that you want to restore to, in the timeframe listed. 请确保将本地时区转换为 UTC。Make sure you convert your local time zone to UTC.
    • 还原到新服务器:输入一个要还原到的新服务器名称。Restore to new server: Enter a new server name to restore to.
    • 位置:此区域与源服务器相同,不能更改。Location: The region is same as the source server and can't be changed.
    • 定价层:定价层与源服务器相同,不能更改。Pricing tier: The pricing tier is the same as the source server and can't be changed.
  3. 选择“确定”,将服务器还原到删除该表之前的某个时间点还原到某个时间点Select OK to restore the server to a point in time restore to a point in time before the table was deleted. 还原服务器时,将创建服务器在所选时间点的新副本。Restoring a server creates a new copy of the server at the point in time that you selected.

后续步骤Next steps

本教程介绍如何使用 Azure 门户完成以下操作:In this tutorial, you use the Azure portal to learned how to:

  • 创建 Azure Database for MariaDBCreate an Azure Database for MariaDB
  • 配置服务器防火墙Configure the server firewall
  • 使用 mysql 命令行工具创建数据库Use mysql command-line tool to create a database
  • 加载示例数据Load sample data
  • 查询数据Query data
  • 更新数据Update data
  • 还原数据Restore data