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

Note

将要查看的是 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.

本快速入门演示如何使用 MySQL Workbench 应用程序连接到 Azure Database for MySQL。This quickstart demonstrates how to connect to an Azure Database for MySQL using the MySQL Workbench application.

必备条件Prerequisites

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

安装 MySQL WorkbenchInstall MySQL Workbench

在计算机上从 MySQL 网站下载并安装 MySQL Workbench。Download and install MySQL Workbench on your computer from the MySQL website.

获取连接信息Get connection information

获取连接到 Azure Database for MySQL 所需的连接信息。Get the connection information needed to connect to the Azure Database for MySQL. 需要完全限定的服务器名称和登录凭据。You need the fully qualified server name and login credentials.

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

  2. 在 Azure 门户的左侧菜单中,单击“所有资源” ,然后搜索已创建的服务器(例如 mydemoserver )。From the left-hand menu in Azure portal, click All resources, and then search for the server you have created (such as mydemoserver).

  3. 单击服务器名称。Click the server name.

  4. 从服务器的“概览”面板中记下“服务器名称”和“服务器管理员登录名”。 From the server's Overview panel, make a note of the Server name and Server admin login name. 如果忘记了密码,也可通过此面板来重置密码。If you forget your password, you can also reset the password from this panel. Azure Database for MySQL 服务器名称Azure Database for MySQL server name

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

若要使用 GUI 工具 MySQL Workbench 连接到 Azure MySQL 服务器,请执行以下操作:To connect to Azure MySQL Server by using the GUI tool MySQL Workbench:

  1. 启动计算机上的 MySQL Workbench 应用程序。Launch the MySQL Workbench application on your computer.

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

    设置新连接

    设置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 MySQL 时使用过的服务器名称值。Specify the server name value that was used when you created the Azure Database for MySQL earlier. 显示的示例服务器为 mydemoserver.mysql.database.chinacloudapi.cn。Our example server shown is mydemoserver.mysql.database.chinacloudapi.cn. 请使用完全限定的域名 (*.mysql.database.chinacloudapi.cn),如示例中所示。Use the fully qualified domain name (*.mysql.database.chinacloudapi.cn) as shown in the example. 如果记不起服务器名称,请按上一部分的步骤操作,以便获取连接信息。Follow the steps in the previous section to get the connection information if you do not remember your server name.
    端口Port 33063306 在连接到 Azure Database for MySQL 时,始终使用端口 3306。Always use port 3306 when connecting to Azure Database for MySQL.
    用户名Username 服务器管理员登录名 server admin login name 键入此前在创建 Azure Database for MySQL 时提供的服务器管理员登录用户名。Type in the server admin login username supplied when you created the Azure Database for MySQL earlier. 示例用户名为 myadmin@mydemoserver。Our example username is myadmin@mydemoserver. 如果记不起用户名,请按上一部分的步骤操作,以便获取连接信息。Follow the steps in the previous section to get the connection information if you do not remember the username. 格式为“username@servername” 。The format is username@servername.
    密码Password 你的密码your password 单击“在保管库中存储...” 按钮来保存密码。Click Store in Vault... button to save the password.
  3. 单击“测试连接” 以测试是否所有参数均已正确配置。Click Test Connection to test if all parameters are correctly configured.

  4. 单击“确定” 保存连接。Click OK to save the connection.

  5. 在“MySQL 连接” 列表中,单击与服务器对应的磁贴并等待建立连接。In the listing of MySQL Connections, click the tile corresponding to your server, and then wait for the connection to be established.

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

    Note

    默认情况下,SSL 连接安全性是必需的,并且在 Azure Database for MySQL 服务器上强制执行。By default, SSL connection security is required and enforced on your Azure Database for MySQL server. 虽然通常不需要对 SSL 证书进行额外的配置,MySQL Workbench 即可连接到服务器,但建议将 SSL CA 证书与 MySQL Workbench 绑定。Although typically no additional configuration with SSL certificates is required for MySQL Workbench to connect to your server, we recommend binding the SSL CA certification with MySQL Workbench. 若要详细了解如何下载和绑定证书,请参阅配置应用程序中的 SSL 连接性以安全连接到 Azure Database for MySQLFor more information on how to download and bind the certification, see Configure SSL connectivity in your application to securely connect to Azure Database for MySQL. 如果需要禁用 SSL,请访问 Azure 门户,然后单击“连接安全性”页来禁用“强制实施 SSL 连接”切换按钮。If you need to disable SSL, visit the Azure portal and click the Connection security page to disable the Enforce SSL connection toggle button.

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

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

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

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

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

  2. 若要运行示例 SQL 代码,请单击“SQL 文件” 选项卡工具栏中的闪电图标。To run the sample SQL Code, click the lightening bolt icon in the toolbar of the SQL File tab.

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

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

现已使用 MySQL Workbench 连接到 Azure Database for MySQL,并已使用 SQL 语言查询数据。Now, you have connected to Azure Database for MySQL by using MySQL Workbench, and you have queried data using the SQL language.

后续步骤Next steps