Share via


使用 Azure CLI 在 VNet 中建立 適用於 MySQL 的 Azure 資料庫 - 彈性伺服器資料庫

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

此範例 CLI 腳本會在 VNet 中建立 適用於 MySQL 的 Azure 資料庫 - 彈性伺服器(私人存取連線方法),並從 VNet 內的 VM 連線到伺服器。

注意

建立伺服器之後,無法變更連線方法。 例如,如果您使用私人存取建立伺服器(VNet 整合),則無法在建立后變更為公用存取(允許的IP位址)。 若要深入瞭解連線方法,請參閱 網路概念

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

必要條件

範例指令碼

啟動 Azure Cloud Shell

Azure Cloud Shell 是免費的互動式 Shell,可讓您用來執行本文中的步驟。 它具有預先安裝和設定的共用 Azure 工具,可與您的帳戶搭配使用。

若要開啟 Cloud Shell,只要選取程式碼區塊右上角的 [試試看] 即可。 您也可以移至 https://shell.azure.com ,從另一個瀏覽器索引標籤啟動 Cloud Shell。

當開啟 Cloud Shell 時,請確認已為您的環境選取 Bash。 後續的工作階段將會在 Bash 環境中使用 Azure CLI,請選取 [複製] 以複製程式碼區塊,並將其貼到 Cloud Shell 中,然後按 Enter 鍵加以執行。

登入 Azure

系統會在登入的初始帳戶下自動驗證 Cloud Shell。 使用下列指令碼透過不同的訂閱登入,並將 <Subscription ID> 取代為您的 Azure 訂用帳戶識別碼。 如果您沒有 Azure 訂用帳戶,請在開始之前先建立 Azure 免費帳戶

subscription="<subscriptionId>" # add subscription here

az account set -s $subscription # ...or use 'az login'

如需詳細資訊,請參閱設定使用中訂閱以互動方式登入

執行指令碼

# Create an Azure Database for MySQL - Flexible Server in a VNet

# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="East US"
resourceGroup="msdocs-mysql-rg-$randomIdentifier"
tag="create-connect-server-in-vnet-mysql"
server="msdocs-mysql-server-$randomIdentifier"
sku="Standard_D2ds_v4"
tier="GeneralPurpose"
storageSize="64"
storageAutoGrow="Enabled"
vNet="vNet-$randomIdentifier"
vNetAddressPrefix="155.5.0.0/24"
mySqlSubnet="msdocs-subnet-mysql-$randomIdentifier"
mySqlSubnetAddressPrefix="155.5.0.0/28"
rule="msdocs-rule-$randomIdentifier"
login="azureuser"
password="Pa$$w0rD-$randomIdentifier"
image="Ubuntu2204"
vm="msdocs-vm-$randomIdentifier"
vmSubnet="msdocs-subnet-vm-$randomIdentifier"
vmSubnetAddressPrefix="155.5.0.48/28"
dns="msdocsDNS.private.mysql.database.azure.com"
ipSku="basic"

echo "Using resource group $resourceGroup with login: $login, password: $password..."

# Create MySQL server in a VNET 

# Create a resource group
echo "Creating $resourceGroup in $location..."
az group create --name $resourceGroup --location "$location" --tags $tag

# Get available service endpoints for Azure region output in JSON
echo "List of available service endpoints for $location"
az network vnet list-endpoint-services --location "$location"

# Create the virtual network
echo "Creating $vNet"
az network vnet create --resource-group $resourceGroup --name $vNet --address-prefixes $vNetAddressPrefix --location "$location"

# Creates the mySqlSubnet
echo "Creating $mySqlSubnet in $vNet"
az network vnet subnet create --resource-group $resourceGroup --name $mySqlSubnet --vnet-name $vNet --address-prefix $mySqlSubnetAddressPrefix --service-endpoints Microsoft.SQL

# View service endpoints configured on a subnet
echo "Viewing the service endpoint to $mySqlSubnet in $vNet"
az network vnet subnet show --resource-group $resourceGroup --name $mySqlSubnet --vnet-name $vNet

# Create private DNS zone
echo "Creating $dns"
az network private-dns zone create -g $resourceGroup    -n $dns

# OPTIONAL : View all SKUs for Flexible Server
# az mysql flexible-server list-skus --location "$location"

# Name of a server maps to DNS name and is thus required to be globally unique in Azure.
# Create a MySQL Flexible server in the resource group
echo "Creating $server within $mySqlSubnet"
az mysql flexible-server create --name $server --resource-group $resourceGroup --location "$location" --sku-name $sku --tier $tier --storage-size $storageSize --storage-auto-grow $storageAutoGrow --admin-user $login --admin-password $password --vnet $vNet --subnet $mySqlSubnet --private-dns-zone $dns

# Connect to the MySQL server from a VM in the same VNET 

# Create a subnet for the virtual machine within the virtual network
echo "Creating $vmSubnet within $vNet"
az network vnet subnet create --resource-group $resourceGroup --vnet-name $vNet --name $vmSubnet --address-prefixes $vmSubnetAddressPrefix

# Create a VM within the VNET to connect to MySQL Flex Server
echo "Creating $vm within $vmSubnet"
az vm create --resource-group $resourceGroup --name $vm --location "$location" --image $image --admin-username $login --generate-ssh-keys --vnet-name $vNet --subnet $vmSubnet --public-ip-sku $ipSku

# Open port 80 for web traffic
echo "Opening port 80 for web traffic"
az vm open-port --port 80 --resource-group $resourceGroup --name $vm

# Follow steps in the parent article to test connectivity to the MySQL server from the VM

從 VM 測試 MySQL 伺服器的連線能力

使用下列步驟來測試從 VM 連線到 MySQL 伺服器的連線能力,方法是使用 SSH、下載 MySQL 工具,然後使用它們連線到 MySQL 伺服器。

  1. 若要透過 SSH 連線到 VM,請從取得公用 IP 位址開始,然後使用 MySQL 工具來連線

    PUBLIC_IP=$(az vm list-ip-addresses --resource-group $RESOURCE_GROUP --name $VM --query "[].virtualMachine.network.publicIpAddresses[0].ipAddress" --output tsv)
    
    ssh azureuser@$PUBLIC_IP
    
  2. 下載 MySQL 工具並連線到伺服器。 以您的值取代 <server_name> 和 <admin_user> 。

    sudo apt-get update
    sudo apt-get install mysql-client
    
    wget --no-check-certificate https://dl.cacerts.digicert.com/DigiCertGlobalRootCA.crt.pem
    
    mysql -h <replace_with_server_name>.mysql.database.azure.com -u mysqladmin -p --ssl-mode=REQUIRED --ssl-ca=DigiCertGlobalRootCA.crt.pem
    

清除資源

您可以使用下列命令來移除資源群組及所有與其相關聯的資源,除非您仍持續需要這些資源,否則請使用 az group delete 命令。 某些資源可能需要一些時間才能建立或刪除。

az group delete --name $RESOURCE_GROUP

範例參考

此指令碼會使用下列命令。 下表中的每個命令都會連結至命令特定的文件。

命令 注意事項
az group create 建立儲存所有資源的資源群組
az mysql flexible-server create 建立裝載資料庫的彈性伺服器。
az network vnet subnet create 在 VNet 內建立子網。
az vm create 建立 Azure 虛擬機。
az vm open-port 開啟 VM 至指定埠上的輸入流量。
az group delete 刪除資源群組,包括所有的巢狀資源。

下一步