快速入門:使用 適用於 MySQL 的 Azure 資料庫 連線 - 使用 Azure CLI 彈性伺服器
適用於:適用於 MySQL 的 Azure 資料庫 - 彈性伺服器
本快速入門示範如何使用 Azure CLI 搭配 az mysql flexible-server connect
使用來連線到 適用於 MySQL 的 Azure 資料庫 彈性伺服器,並使用 命令執行單一查詢或 sql 檔案az mysql flexible-server execute
。 此命令可讓您測試資料庫伺服器的連線能力,並執行查詢。 您也可以使用互動式模式執行多個查詢。
必要條件
具有有效訂用帳戶的 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!
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 檔案
您可以使用 自變數 -q
,使用 --file-path
命令執行 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.