使用 Azure CLI 建立和管理 適用於 MariaDB 的 Azure 資料庫 VNet 服務端點

重要

適用於 MariaDB 的 Azure 資料庫位於淘汰路徑上。 強烈建議您移轉至適用於 MySQL 的 Azure 資料庫。 如需移轉至適用於 MySQL 的 Azure 資料庫的詳細資訊,請參閱適用於 MariaDB 的 Azure 資料庫會發生什麼事?(部份機器翻譯)。

虛擬網路 (VNet) 服務端點和規則會將虛擬網路的私人位址空間延伸到您適用於 MariaDB 的 Azure 資料庫伺服器。 您可以使用方便使用的 Azure CLI 命令來建立、更新、刪除、列出和顯示 VNet 服務端點和規則,以管理您的伺服器。 如需 適用於 MariaDB 的 Azure 資料庫 VNet 服務端點的概觀,包括限制,請參閱 適用於 MariaDB 的 Azure 資料庫 伺服器 VNet 服務端點。 VNet 服務端點可在所有支援的區域中取得 適用於 MariaDB 的 Azure 資料庫。

如果您沒有 Azure 訂閱,請在開始之前,先建立 Azure 免費帳戶

必要條件

注意

VNet 服務端點的支援僅適用於一般用途伺服器和記憶體最佳化伺服器。

設定 VNet 服務端點

az network vnet \(英文\) 命令會用來設定虛擬網路。 擁有虛擬網路寫入權的使用者可以任意地在虛擬網路上設定服務端點。

若要將 Azure 服務資源放到 VNet 保護,使用者必須擁有所要新增之子網路的 "Microsoft.Network/virtualNetworks/subnets/joinViaServiceEndpoint/" 權限。 此權限預設會隨附在內建的服務管理員角色中,可藉由建立自訂角色加以修改。

深入了解內建角色以及如何將特定權限指派給自訂角色

VNet 和 Azure 服務資源不一定要位於相同訂用帳戶中。 如果 VNet 和 Azure 服務資源位於不同的訂用帳戶中,則資源應該位於相同的 Active Directory (AD) 租用戶底下。 請確定這兩個訂用帳戶都已註冊 Microsoft.Sql 資源提供者。 如需詳細資訊,請參閱 resource-manager-registration

重要

強烈建議您在設定服務端點之前,先閱讀這篇有關服務端點設定和考量的文章。 虛擬網路服務端點:虛擬網路服務端點是一個子網路,其屬性值包含一或多個正式的 Azure 服務類型名稱。 VNet 服務端點使用 Microsoft.Sql 服務類型名稱,它參考名為 SQL Database 的 Azure 服務。 此服務標籤也適用於 Azure SQL 資料庫、適用於 MariaDB 的 Azure 資料庫、PostgreSQL 和 MySQL 服務。 請務必注意,將 Microsoft.Sql 服務標籤套用至 VNet 服務端點時,它會設定所有 Azure 資料庫服務的服務端點流量,包括 Azure SQL 資料庫、適用於 PostgreSQL 的 Azure 資料庫、適用於 MariaDB 的 Azure 資料庫 和適用於 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 MariaDB server in vNet

# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="East US"
resourceGroup="msdocs-mariadb-rg-$randomIdentifier"
tag="create-mariadb-server"
server="msdocs-mariadb-server-$randomIdentifier"
sku="GP_Gen5_2"
vNet="vNet-$randomIdentifier"
vNetAddressPrefix="10.0.0.0/16"
subnet="subnet-$randomIdentifier"
subnetAddressPrefix="10.0.1.0/24"
rule="rule-$randomIdentifier"
login="azureuser"
password="Pa$$w0rD-$randomIdentifier"

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

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

# Create a MariaDB server in the resource group
# Name of a server maps to DNS name and is thus required to be globally unique in Azure.
echo "Creating $server in $location..."
az mariadb server create --name $server --resource-group $resourceGroup --location "$location" --admin-user $login --admin-password $password --sku-name $sku

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

# Add Azure SQL service endpoint to a subnet while creating the virtual network
echo "Adding service endpoint to $subnet in $vNet"
az network vnet create --resource-group $resourceGroup --name $vNet --address-prefixes $vNetAddressPrefix --location "$location"

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

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

# Create a VNet rule on the server to secure it to the subnet
# Note: resource group (-g) parameter is where the database exists.
# VNet resource group if different should be specified using subnet id (URI) instead of subnet, VNet pair.
echo "Creating a VNet rule on $server to secure it to $subnet in $vNet"
az mariadb server vnet-rule create --name $rule --resource-group $resourceGroup --server $server --vnet-name $vNet --subnet $subnet

清除部署

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

echo "Cleaning up resources by removing the resource group..."
az group delete --name $resourceGroup -y