Share via


使用 Azure CLI 建立 MariaDB 伺服器並設定防火牆規則

重要

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

此範例 CLI 腳本會建立 適用於 MariaDB 的 Azure 資料庫 伺服器,並設定伺服器層級防火牆規則。 腳本成功執行之後,所有 Azure 服務和設定的 IP 位址都可以存取 MariaDB 伺服器。

如果您沒有 Azure 訂閱,請在開始之前,先建立 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 and firewall rule

# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="East US"
resourceGroup="msdocs-mariadb-rg-$randomIdentifier"
tag="create-mariadb-server-and-firewall-rule"
server="msdocs-mariadb-server-$randomIdentifier"
sku="GP_Gen5_2"
login="azureuser"
password="Pa$$w0rD-$randomIdentifier"
# Specify appropriate IP address values for your environment
# to limit / allow access to the MariaDB server
startIp=0.0.0.0
endIp=0.0.0.0

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

# Configure a firewall rule for the server 
echo "Configuring a firewall rule for $server for the IP address range of $startIp to $endIp"
az mariadb server firewall-rule create --resource-group $resourceGroup --server $server --name AllowIps --start-ip-address $startIp --end-ip-address $endIp

清除資源

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

az group delete --name $resourceGroup

範例參考

此文稿會使用下表中所述的命令:

命令 注意事項
az group create 建立用來存放所有資源的資源群組。
az mariadb server create 建立裝載資料庫的 MariaDB 伺服器。
az mariadb server firewall create 建立防火牆規則,以允許從輸入的IP位址範圍存取伺服器和資料庫。
az group delete 刪除資源群組,包括所有的巢狀資源。

下一步