分享方式:


快速入門:使用 Azure CLI 與適用於 MySQL 的 Azure 資料庫 - 彈性伺服器連線

適用於:適用於 MySQL 的 Azure 資料庫 - 彈性伺服器

本快速入門示範如何使用 Azure CLI 搭配 az mysql flexible-server connect 連線至適用於 MySQL 的 Azure 資料庫彈性伺服器,並使用 az mysql flexible-server execute 命令執行單一查詢或 SQL 檔案。 此命令可讓您測試資料庫伺服器的連線並執行查詢。 您也可以使用互動式模式執行多個查詢。

必要條件

  • 具有有效訂用帳戶的 Azure 帳戶。

    如果您沒有 Azure 訂閱,請在開始之前先建立 Azure 免費帳戶。 目前,Azure 免費帳戶可讓您免費試用「適用於 MySQL 的 Azure 資料庫 - 彈性伺服器」12 個月。 如需詳細資訊,請參閱免費試用適用於 MySQL 的 Azure 資料庫 - 彈性伺服器

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

  • 使用 Azure CLI 搭配 az login 命令來登入

  • 使用 az config param-persist on 開啟參數持續性。 參數持續性可協助您使用本地內容,而不需要重複許多引數,例如資源群組或位置。

建立 MySQL 彈性伺服器

您要建立的第一個項目是受控的「適用於 MySQL 的 Azure 資料庫」彈性伺服器執行個體。 在 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 殼層體驗,如下所示:

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!

執行單一查詢

執行下列命令,以執行單一查詢,使用--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
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.

後續步驟