在適用於 MySQL 的 Azure 資料庫 - 彈性伺服器上,此範例 CLI 指令碼會啟用稽核記錄。
如果您沒有 Azure 訂閱,請在開始之前先建立 Azure 免費帳戶。 目前,Azure 免費帳戶可讓您免費試用「適用於 MySQL 的 Azure 資料庫 - 彈性伺服器」12 個月。 如需詳細資訊,請參閱使用 Azure 免費帳戶免費試用適用於 MySQL 的 Azure 資料庫 - 彈性伺服器。
先決條件
在 Azure Cloud Shell 中使用 Bash 環境。 如需詳細資訊,請參閱開始使用 Azure Cloud Shell。
若要在本機執行 CLI 參考命令,請安裝 (部分機器翻譯) Azure CLI。 若您在 Windows 或 macOS 上執行,請考慮在 Docker 容器中執行 Azure CLI。 如需詳細資訊,請參閱如何在 Docker 容器中執行 Azure CLI。
如果您使用的是本機安裝,請使用 az login 命令,透過 Azure CLI 來登入。 請遵循您終端機上顯示的步驟,以完成驗證流程。 如需其他登入選項,請參閱 使用 Azure CLI 向 Azure 進行驗證。
出現提示時,請在第一次使用時安裝 Azure CLI 延伸模組。 如需擴充功能的詳細資訊,請參閱 使用和管理 Azure CLI 的擴充功能。
執行 az version (部分機器翻譯) 以尋找已安裝的版本和相依程式庫。 若要升級至最新版本,請執行 az upgrade。
- 本文需要 2.0 版或更新版本的 Azure CLI。 如果您是使用 Azure Cloud Shell,就已安裝最新版本。
範例指令碼
啟動 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。 使用下列指令碼使用不同的訂用帳戶登入,並將 subscriptionId 取代為您的 Azure 訂用帳戶識別碼。
如果您沒有 Azure 帳戶,請在開始之前建立 免費帳戶 。
subscription="subscriptionId" # Set Azure subscription ID here
az account set -s $subscription # ...or use 'az login'
如需詳細資訊,請參閱設定使用中訂用帳戶 (部分機器翻譯) 或以互動方式登入 (部分機器翻譯)。
執行指令碼
# Configure audit logs on Azure Database for MySQL - Flexible Server
# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="East US"
resourceGroup="msdocs-mysql-rg-$randomIdentifier"
tag="configure-audit-logs-mysql"
server="msdocs-mysql-server-$randomIdentifier"
login="azureuser"
password="Pa$$w0rD-$randomIdentifier"
ipAddress="None"
# Specifying an IP address of 0.0.0.0 allows public access from any resources
# deployed within Azure to access your server. Setting it to "None" sets the server
# in public access mode but does not create a firewall rule.
# For your public IP address, https://whatismyipaddress.com
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 MySQL Flexible server in the resource group
echo "Creating $server"
az mysql flexible-server create --name $server --resource-group $resourceGroup --location "$location" --admin-user $login --admin-password $password --public-access $ipAddress
# Optional: Add firewall rule to connect from all Azure services
# To limit to a specific IP address or address range, change start-ip-address and end-ip-address
echo "Adding firewall for IP address range"
az mysql flexible-server firewall-rule create --name $server --resource-group $resourceGroup --rule-name AllowAzureIPs --start-ip-address 0.0.0.0 --end-ip-address 0.0.0.0
# Enable audit logs
echo "Enabling audit logs"
az mysql flexible-server parameter set --resource-group $resourceGroup --server-name $server --name audit_log_enabled --value ON
清除資源
使用下列命令來移除資源群組及所有與其相關聯的資源,除非您持續需要這些資源,則請使用 az group delete 命令。 這其中某些資源可能需要一些時間才能建立和刪除。
az group delete --name $resourceGroup
範例參考
此指令碼會使用下列命令。 下表中的每個命令都會連結至命令特定的文件。
| 命令 | 注意事項 |
|---|---|
| 請輸入命令:az group create 來建立群組。 | 建立儲存所有資源的資源群組 |
| az mysql flexible-server create (用於建立 MySQL 彈性伺服器的命令) | 建立裝載資料庫的彈性伺服器。 |
| az mysql flexible-server 參數集 | 更新彈性伺服器的參數。 |
| az mysql flexible-server delete | 刪除彈性伺服器。 |
| az group delete | 刪除資源群組,包括所有的巢狀資源。 |