你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

快速入门:使用 Azure CLI 连接 Azure Database for MySQL - 灵活服务器

适用于:Azure Database for MySQL - 灵活服务器

本快速入门演示了如何在 Azure CLI 中使用 az mysql flexible-server connect 连接到 Azure Database for MySQL 灵活服务器并使用 az mysql flexible-server execute 命令执行单个查询或 sql 文件。 通过此命令可测试与数据库服务器的连接并运行查询。 还可以使用交互模式运行多个查询。

先决条件

  • 具有活动订阅的 Azure 帐户。

    如果没有 Azure 订阅,请在开始之前创建一个 Azure 免费帐户。 目前,通过使用 Azure 免费帐户,你可以在 12 个月内免费试用 Azure Database for MySQL 灵活服务器。 有关详细信息,请参阅免费试用 Azure Database for MySQL 灵活服务器

  • 安装 Azure CLI 最新版本(2.20.0 或更高版本)

  • 通过 az login 命令使用 Azure CLI 登录

  • 使用 az config param-persist on 启用参数持久性。 参数暂留有助于使用本地上下文,而无需重复大量参数,如资源组或位置等。

创建 MySQL 灵活服务器

首先需要创建托管的 Azure Database for MySQL 灵活服务器实例。 在 Azure Cloud Shell 中,运行以下脚本,并记下该命令生成的“服务器名称”、“用户名”和“密码”。

az mysql flexible-server create --public-access <your-ip-address>

你可以为此命令提供更多参数来自定义它。 查看 az mysql flexible-server create 的所有参数。

创建数据库

如果尚未创建数据库,请运行以下命令来创建一个数据库 newdatabase

az mysql flexible-server db create -d newdatabase

查看所有参数

可以通过 --help 参数查看此命令的所有参数。

az mysql flexible-server connect --help

测试数据库服务器连接

运行以下脚本,在开发环境中测试和验证与数据库的连接。

az mysql flexible-server connect -n <servername> -u <username> -p <password> -d <databasename>

示例:

az mysql flexible-server connect -n mysqldemoserver1 -u dbuser -p "dbpassword" -d newdatabase

对于成功的连接,应看到以下输出:

Command group 'mysql flexible-server' is in preview and under development. Reference and support levels: https://aka.ms/CLI_refstatus
Connecting to newdatabase database.
Successfully connected to mysqldemoserver1.

如果连接失败,请尝试以下解决方案:

  • 检查是否已在客户端计算机上打开端口 3306。
  • 服务器管理员用户名和密码是否正确
  • 是否已为客户端计算机配置防火墙规则
  • 如果已在虚拟网络中为服务器配置了专用访问权限,请确保客户端计算机位于同一虚拟网络中。

使用交互模式运行多个查询

可以使用“交互”模式运行多个查询。 若要启用交互模式,请运行以下命令

az mysql flexible-server connect -n <server-name> -u <username> -p <password> --interactive

示例:

az mysql flexible-server connect -n mysqldemoserver1 -u dbuser -p "dbpassword" -d newdatabase --interactive

你可以看到 MySQL shell 体验,如下所示:

Command group 'mysql flexible-server' is in preview and under development. Reference and support levels: https://aka.ms/CLI_refstatus
Password:
mysql 5.7.29-log
mycli 1.22.2
Chat: https://gitter.im/dbcli/mycli
Mail: https://groups.google.com/forum/#!forum/mycli-users
Home: http://mycli.net
Thanks to the contributor - Martijn Engler
newdatabase> CREATE TABLE table1 (id int NOT NULL, val int,txt varchar(200));
Query OK, 0 rows affected
Time: 2.290s
newdatabase1> INSERT INTO table1 values (1,100,'text1');
Query OK, 1 row affected
Time: 0.199s
newdatabase1> SELECT * FROM table1;
+----+-----+-------+
| id | val | txt   |
+----+-----+-------+
| 1  | 100 | text1 |
+----+-----+-------+
1 row in set
Time: 0.149s
newdatabase>exit;
Goodbye!
Local context is turned on. Its information is saved in working directory C:\mydir. You can run `az local-context off` to turn it off.
Your preference of  are now saved to local context. To learn more, type in `az local-context --help`

运行单个查询

运行以下命令,以使用 --querytext 参数 -q 执行单个查询。

az mysql flexible-server execute -n <server-name> -u <username> -p "<password>" -d <database-name> --querytext "<query text>"

示例:

az mysql flexible-server execute -n mysqldemoserver1 -u dbuser -p "dbpassword" -d newdatabase -q "select * from table1;" --output table

你可以看到如下所示的输出:

Command group 'mysql flexible-server' is in preview and under development. Reference and support levels: https://aka.ms/CLI_refstatus
Successfully connected to mysqldemoserver1.
Ran Database Query: 'select * from table1;'
Retrieving first 30 rows of query output, if applicable.
Closed the connection to mysqldemoserver1
Local context is turned on. Its information is saved in working directory C:\Users\sumuth. You can run `az local-context off` to turn it off.
Your preference of  are now saved to local context. To learn more, type in `az local-context --help`
Txt    Val
-----  -----
test   200
test   200
test   200
test   200
test   200
test   200
test   200

运行 SQL 文件

可以通过 --file-path 参数 -q 使用命令执行 SQL 文件。

az mysql flexible-server execute -n <server-name> -u <username> -p "<password>" -d <database-name> --file-path "<file-path>"

示例:

az mysql flexible-server execute -n mysqldemoserver -u dbuser -p "dbpassword" -d flexibleserverdb -f "./test.sql"

你可以看到如下所示的输出:

Command group 'mysql flexible-server' is in preview and under development. Reference and support levels: https://aka.ms/CLI_refstatus
Running sql file '.\test.sql'...
Successfully executed the file.
Closed the connection to mysqldemoserver.

后续步骤