連線 適用於 MySQL 的 Azure 資料庫 - 具有私人存取連線方法的彈性伺服器

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

適用於 MySQL 的 Azure 資料庫 彈性伺服器是一項受控服務,可在雲端中執行、管理及調整高可用性 MySQL 伺服器。 本快速入門說明如何使用 Azure 入口網站,在虛擬網路中建立 適用於 MySQL 的 Azure 資料庫 彈性伺服器實例。

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

登入 Azure 入口網站

登入 Azure 入口網站。 輸入您的認證來登入此入口網站。 預設檢視是您的服務儀表板。

建立 適用於 MySQL 的 Azure 資料庫 彈性伺服器實例

您可以使用一組定義的計算和記憶體資源,建立 適用於 MySQL 的 Azure 資料庫 彈性伺服器實例。 您會在 Azure 資源群組建立伺服器。

完成下列步驟以建立 適用於 MySQL 的 Azure 資料庫 彈性伺服器實例:

  1. 在入口網站中搜尋並選取 適用於 MySQL 的 Azure 資料庫 伺服器

    Screenshot that shows a search for Azure Database for MySQL servers.

  2. 選取 [新增]。

  3. 在 [選取 適用於 MySQL 的 Azure 資料庫 部署選項] 頁面上,選取 [彈性伺服器] 作為部署選項:

    Screenshot that shows the Flexible server option.

  4. 在 [基本] 索引標籤上,輸入訂帳戶、資源群組區域系統管理員使用者名稱和系統管理員密碼。 使用預設值時,這會使用1個虛擬核心、2 GiB記憶體和32 GiB記憶體,布建 適用於 MySQL 的 Azure 資料庫具有高載Sku5.7版的彈性伺服器實例。 備份保留期為七天。 您可以變更設定。

    Screenshot that shows the Basics tab of the Flexible server page.

    提示

    若要在移轉期間更快速地載入數據,建議將IOPS增加為計算大小所支援的大小上限,稍後再相應縮小以節省成本。

  5. 移至 [ 網络] 索引 標籤,選取 [私人存取]。建立伺服器之後,您無法變更連線方法。 選取 [建立虛擬網络 ] 以建立新的虛擬網络 vnetenvironment1

    Screenshot that shows the Networking tab with new VNET.

  6. 提供虛擬網路名稱和子網資訊后,請選取 [ 確定 ]。

    Review VNET information

  7. 選取 [檢閱 + 建立] 以檢閱您的 適用於 MySQL 的 Azure 資料庫 彈性伺服器設定。

  8. 選取 [建立] 以佈建伺服器。 佈建這可能需要數分鐘的時間。

  9. 等候部署完成並成功。

    Screenshot that shows the Networking settings with new VNET.

  10. 選取 [移至資源 ] 以檢視伺服器的 [概觀 ] 頁面隨即開啟。

建立 Azure Linux 虛擬機

由於伺服器位於虛擬網路中,因此您只能從與伺服器相同的虛擬網路中的其他 Azure 服務連線到伺服器。 若要連線和管理伺服器,讓我們建立Linux虛擬機。 虛擬機必須在相同的區域相同的訂用帳戶建立。 Linux 虛擬機可用來作為 SSH 通道來管理資料庫伺服器。

  1. 移至建立伺服器所在的資源群組。 選取 [新增]。

  2. 選取 [Ubuntu Server 18.04 LTS]

  3. 在 [基本] 索引標籤中的 [專案詳細資料] 底下,確認已選取正確的訂用帳戶,然後選擇 [新建] 資源群組。 輸入 myResourceGroup 作為名稱。

    Screenshot of the Project details section showing where you select the Azure subscription and the resource group for the virtual machine.

  4. 在 [實例詳細數據] 底下,輸入 myVM 作為虛擬機名稱,選擇與資料庫伺服器相同的區域

    Screenshot of the Instance details section where you provide a name for the virtual machine and select its region, image and size.

  5. 在 [Administrator 帳戶] 底下,選取 [SSH 公開金鑰]

  6. 在 [使用者名稱] 中輸入「azureuser」

  7. 針對 [SSH 公開金鑰來源],保留預設值 [產生新的金鑰組],然後針對 [金鑰組名稱] 輸入「myKey」

    Screenshot of the Administrator account section where you select an authentication type and provide the administrator credentials.

  8. 在 [輸入連接埠規則]>[公用輸入連接埠] 下方,選擇 [允許選取的連接埠],然後從下拉式清單中選取 [SSH (22)] 和 [HTTP (80)]

    Screenshot of the inbound port rules section where you select what ports inbound connections are allowed on.

  9. 選取 [ 網络] 頁面以設定虛擬網路。 針對虛擬網路,選擇為 適用於 MySQL 的 Azure 資料庫 彈性伺服器實例建立的 vnetenvironment1

    Screenshot of the select existing virtual network of the Azure Database for MySQL flexible server instance.

  10. 選取 [ 管理子網組態 ] 來建立伺服器的新子網。

    Screenshot of manage subnet.

  11. 新增虛擬機的新子網。

    Screenshot of adding a new subnet for virtual machine

  12. 成功建立子網之後,請關閉頁面。

    Screenshot of success with adding a new subnet for virtual machine

  13. 選取 [檢閱 + 建立] 。

  14. 選取 建立。 當 [產生新的金鑰組] 視窗開啟時,請選取 [下載私密金鑰並建立資源]。 您的金鑰檔案會下載為 myKey.pem

    重要

    請確定您知道檔案的下載位置 .pem ,在下一個步驟中需要該檔案的路徑。

  15. 部署完成之後,請選取 [移至資源]

    Screenshot of deployment success.

  16. 在新 VM 的頁面上,選取公用 IP 位址,並將其複製到剪貼簿。

    Screenshot showing how to copy the IP address for the virtual machine.

安裝 MySQL 用戶端工具

使用 Bash 或 PowerShell 建立與 VM 的 SSH 連線。 在您的提示字元中,開啟與虛擬機器的 SSH 連線。 將 IP 位址取代為您 VM 的位址,並且以下載金鑰檔案的路徑取代 .pem 的路徑。

ssh -i .\Downloads\myKey1.pem azureuser@10.111.12.123

提示

下次您在 Azure 中建立 VM 時,可以使用您所建立的 SSH 金鑰。 選取下次建立 VM 時,針對 SSH 公鑰來源使用 Azure中儲存的金鑰。 您的電腦上已經有私密金鑰,所以您不需要下載任何項目。

您必須安裝 mysql-client 工具來連線到伺服器。

sudo apt-get update
sudo apt-get install mysql-client

使用 SSL 強制執行資料庫的 連線,因此您必須下載公用 SSL 憑證。

wget --no-check-certificate https://dl.cacerts.digicert.com/DigiCertGlobalRootCA.crt.pem

從 Azure Linux 虛擬機 連線 伺服器

安裝mysql.exe用戶端工具后,我們現在可以從本機環境連線到伺服器。

mysql -h mydemoserver.mysql.database.azure.com -u mydemouser -p --ssl-mode=REQUIRED --ssl-ca=DigiCertGlobalRootCA.crt.pem

清除資源

您已在資源群組中建立 適用於 MySQL 的 Azure 資料庫 彈性伺服器實例。 如果您預計未來不需要這些資源,您可以藉由刪除資源群組或 適用於 MySQL 的 Azure 資料庫 彈性伺服器實例來刪除這些資源。 若要刪除資源群組,請完成下列步驟:

  1. 在 Azure 入口網站 中搜尋並選取 [資源群組]。
  2. 在資源群組清單中,選取資源群組的名稱。
  3. 在資源群組的 [ 概觀] 頁面中,選取 [ 刪除資源群組]。
  4. 在確認對話框中,輸入資源群組的名稱,然後選取 [ 刪除]。

下一步