Azure Database for MariaDB:使用 MySQL Workbench 连接和查询数据Azure Database for MariaDB: Use MySQL Workbench to connect and query data

本快速入门演示如何使用 MySQL Workbench 连接到 Azure Database for MariaDB 实例。This quickstart demonstrates how to connect to an Azure Database for MariaDB instance by using MySQL Workbench.

先决条件Prerequisites

此快速入门使用以下任意指南中创建的资源作为起点:This quickstart uses the resources that are created in either of the following guides as a starting point:

安装 MySQL WorkbenchInstall MySQL Workbench

下载 MySQL Workbench 并将其安装在计算机上。Download MySQL Workbench and install it on your computer.

获取连接信息Get connection information

获取连接到 Azure Database for MariaDB 实例所需的连接信息。Get the connection information that's required to connect to the Azure Database for MariaDB instance. 需要完全限定的服务器名称和登录凭据。You need the fully qualified server name and sign-in credentials.

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

  2. 在 Azure 门户的左侧菜单中,选择“所有资源”。In the left menu in the Azure portal, select All resources. 搜索所创建的服务器(例如 mydemoserver)。Search for the server you created (such as mydemoserver).

  3. 选择服务器名称。Select the server name.

  4. 在服务器的“概览”页中记下“服务器名称”和“服务器管理员登录名”的值。 On the server's Overview page, make a note of the values for Server name and Server admin login name. 如果忘记了密码,也可在此页上重置密码。If you forgot your password, you can also reset the password on this page.

    Azure Database for MariaDB 服务器名称和服务器管理员登录名

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

若要使用 MySQL Workbench 连接到 Azure Database for MariaDB 服务器,请执行以下操作:To connect to an Azure Database for MariaDB server by using MySQL Workbench:

  1. 打开计算机上的 MySQL Workbench。Open MySQL Workbench on your computer.

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

    设置Setting 建议的值Suggested value 字段说明Field description
    连接名称Connection Name 演示连接Demo connection 指定此连接的标签。Specify a label for this connection.
    连接方法Connection Method 标准 (TCP/IP)Standard (TCP/IP) 标准 (TCP/IP) 就足够了。Standard (TCP/IP) is sufficient.
    主机名Hostname 服务器名称server name 指定在创建 Azure Database for MariaDB 实例时使用的服务器名称值。Specify the server name value that you used to create the Azure Database for MariaDB instance. 示例服务器为 mydemoserver.mariadb.database.chinacloudapi.cnOur example server is mydemoserver.mariadb.database.chinacloudapi.cn. 请使用完全限定域名 (*.mariadb.database.chinacloudapi.cn),如示例中所示。Use the fully qualified domain name (*.mariadb.database.chinacloudapi.cn) as shown in the example. 如果不记得服务器名称,请完成上一部分的步骤,以便获取连接信息。If you don't remember your server name, complete the steps in the preceding section to get the connection information.
    端口Port 33063306 在连接到 Azure Database for MariaDB 时,始终使用端口 3306。Always use port 3306 when you connect to Azure Database for MariaDB.
    用户名Username 服务器管理员登录名server admin login name 输入在创建 Azure Database for MariaDB 实例时使用的服务器管理员登录用户名。Enter the server admin login user name you used to create the Azure Database for MariaDB instance. 示例用户名是 myadmin@mydemoserver。Our example user name is myadmin@mydemoserver. 如果不记得服务器管理员登录名,请完成上一部分的步骤,以便获取连接信息。If you don't remember the server admin login name, complete the steps in the preceding section to get the connection information. 格式为 username@servername。The format is username@servername.
    密码Password 你的密码your password 若要保存密码,请选择“在保管库中存储”。To save the password, select Store in Vault.

    设置新连接

  3. 若要检查所有参数是否已正确配置,请选择“测试连接”。To check that all parameters are configured correctly, select Test Connection.

  4. 选择“确定”以保存连接。Select OK to save the connection.

  5. 在“MySQL 连接”下,选择与服务器相对应的磁贴。Under MySQL Connections, select the tile that corresponds to your server. 等待连接建立。Wait for the connection to be established.

    将打开一个新的 SQL 选项卡,该选项卡包含一个可在其中键入查询的空白编辑器。A new SQL tab opens with a blank editor where you can type your queries.

    备注

    默认情况下,SSL 连接安全性是必需的,并且在 Azure Database for MariaDB 服务器上强制执行。By default, SSL connection security is required and is enforced on your Azure Database for MariaDB server. 虽然通常不需要对 SSL 证书进行额外的配置,MySQL Workbench 即可连接到服务器,但建议将 SSL CA 证书与 MySQL Workbench 绑定。Although typically no additional configuration for SSL certificates is required for MySQL Workbench to connect to your server, we recommend binding the SSL CA certification with MySQL Workbench. 如需禁用 SSL,请在 Azure 门户的服务器概览页的菜单中选择“连接安全性”。If you need to disable SSL, on the server overview page in the Azure portal, select Connection security from the menu. 对于“强制实施 SSL 连接”,请选择“禁用”。 For Enforce SSL connection, select Disabled.

创建表,然后插入、读取、更新和删除数据Create table and insert, read, update, and delete data

  1. 将以下示例 SQL 代码复制并粘贴到空白 SQL 选项卡所在的页中,以阐释一些示例数据。Copy and paste the following sample SQL code into the page of a blank SQL tab to illustrate some sample data.

    此代码将创建名为 quickstartdb 的空数据库,This code creates an empty database named quickstartdb. 然后创建名为清单的示例表。Then, it creates a sample table named inventory. 代码会插入一些行,然后读取这些行。The code inserts some rows, and then reads the rows. 它通过更新语句更改数据,然后再次读取这些行。It changes the data with an update statement, and then reads the rows again. 最后,代码删除一个行,然后再次读取这些行。Finally, the code deletes a row, and then reads the rows again.

    -- Create a database
    -- DROP DATABASE IF EXISTS quickstartdb;
    CREATE DATABASE quickstartdb;
    USE quickstartdb;
    
    -- Create a table and insert rows
    DROP TABLE IF EXISTS inventory;
    CREATE TABLE inventory (id serial PRIMARY KEY, name VARCHAR(50), quantity INTEGER);
    INSERT INTO inventory (name, quantity) VALUES ('banana', 150);
    INSERT INTO inventory (name, quantity) VALUES ('orange', 154);
    INSERT INTO inventory (name, quantity) VALUES ('apple', 100);
    
    -- Read
    SELECT * FROM inventory;
    
    -- Update
    UPDATE inventory SET quantity = 200 WHERE id = 1;
    SELECT * FROM inventory;
    
    -- Delete
    DELETE FROM inventory WHERE id = 2;
    SELECT * FROM inventory;
    

    此屏幕快照显示 MySQL Workbench 中的一个 SQL 代码示例以及运行该示例代码后的输出:The screenshot shows an example of the SQL code in MySQL Workbench and the output after it runs:

    选择运行示例 SQL 代码的 MySQL Workbench SQL 选项卡

  2. 若要运行示例 SQL 代码,请在“SQL 文件”选项卡的工具栏中选择闪电图标。To run the sample SQL code, on the SQL File tab, select the lightening bolt icon on the toolbar.

  3. 注意页面中间“结果网格”部分中的三个选项卡式结果。Note the three tabbed results in the Result Grid section in the middle of the page.

  4. 注意页面底部的“输出”列表。Note the Output list at the bottom of the page. 显示有每个命令的状态。The status of each command is shown.

在本快速入门中,现已使用 MySQL Workbench 连接到 Azure Database for MariaDB,并已使用 SQL 语言查询数据。In this quickstart, you connected to Azure Database for MariaDB by using MySQL Workbench, and you queried data by using the SQL language.