クイック スタート: 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 無料アカウントがあれば、Azure Database for MySQL - フレキシブル サーバーを 12 か月間無料でお試しいただけます。 詳しくは、Azure Database for MySQL - フレキシブル サーバーの無料試用に関する記事をご覧ください。

  • Azure CLI の最新バージョン (2.20.0 以降) をインストールする

  • Azure CLI で az login コマンドを使ってログインする

  • 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 が開放されているかどうかを確認します。
  • サーバー管理者のユーザー名とパスワードが正しいかどうかを確認します。
  • クライアント マシンにファイアウォール規則が構成されているかどうかを確認します。
  • 仮想ネットワークにプライベート アクセスを使用してサーバーを構成した場合、クライアント マシンが同じ仮想ネットワークに存在することを確認します。

対話モードを使用して複数のクエリを実行する

対話 (interactive) モードを使用して、複数のクエリを実行することができます。 対話モードを有効にするには、次のコマンドを実行します。

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 シェルのエクスペリエンスが表示されます。

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\<username>. 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 ファイルを実行する

SQL ファイルを実行するには、コマンドで --file-path 引数 (-q) を使用します。

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.

次のステップ