使用 Azure CLI 啟用和下載 適用於 PostgreSQL 的 Azure 資料庫 的伺服器慢速查詢記錄 - 彈性伺服器實例
適用於:適用於 PostgreSQL 的 Azure 資料庫 - 單一伺服器 適用於 PostgreSQL 的 Azure 資料庫 - 彈性伺服器
此範例 CLI 文本會啟用並下載單一 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器實例的慢速查詢記錄。
如果您沒有 Azure 訂閱,請在開始之前,先建立 Azure 免費帳戶。
必要條件
在 Azure Cloud Shell 中使用 Bash 環境。 如需詳細資訊,請參閱 Azure Cloud Shell 中的 Bash 快速入門。
若要在本地執行 CLI 參考命令,請安裝 Azure CLI。 若您在 Windows 或 macOS 上執行,請考慮在 Docker 容器中執行 Azure CLI。 如需詳細資訊,請參閱〈如何在 Docker 容器中執行 Azure CLI〉。
如果您使用的是本機安裝,請使用 az login 命令,透過 Azure CLI 來登入。 請遵循您終端機上顯示的步驟,完成驗證程序。 如需其他登入選項,請參閱使用 Azure CLI 登入。
出現提示時,請在第一次使用時安裝 Azure CLI 延伸模組。 如需擴充功能詳細資訊,請參閱使用 Azure CLI 擴充功能。
執行 az version 以尋找已安裝的版本和相依程式庫。 若要升級至最新版本,請執行 az upgrade。
範例指令碼
啟動 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'
執行指令碼
# Enable and download server slow query logs of an Azure Database for PostgreSQL server
# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="East US"
resourceGroup="msdocs-postgresql-rg-$randomIdentifier"
tag="server-logs-postgresql"
server="msdocs-postgresql-server-$randomIdentifier"
sku="GP_Gen5_2"
login="azureuser"
password="Pa$$w0rD-$randomIdentifier"
configurationParameter="slow_query_log"
logValue="On"
durationStatementLog="durationStatementLog-$randomIdentifier"
logDuration="10000"
logFileList="logFileList"
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 PostgreSQL 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 postgres server create --name $server --resource-group $resourceGroup --location "$location" --admin-user $login --admin-password $password --sku-name $sku
# List the configuration options for review
echo "Returning the configuration options on $server"
az postgres server configuration list --resource-group $resourceGroup --server $server
# Turn on statement level log
echo "Enable the statement level log"
az postgres server configuration set --name log_statement --resource-group $resourceGroup --server $server --value all
# Set log_min_duration_statement time to 10 sec
echo "Setting log_min_duration_statement to 10 sec"
az postgres server configuration set --name log_min_duration_statement --resource-group $resourceGroup --server $server --value 10000
# List the available log files
echo "Returning the list of available log files on $server"
az postgres server-logs list --resource-group $resourceGroup --server $server
# To download log file from Azure, direct the output of the previous comment to a text file
# "> log_files_list.txt"
# Review the text file to find the server log file name for the desired timeframe
# Substitute the <log_file_name> in the script below with your server log file name
# Creates the log file in the current command line path
# az postgres server-logs download --name <log_file_name> $resourceGroup --server $server
清除部署
您可以使用下列命令來移除資源群組及所有與其相關聯的資源,除非您仍持續需要這些資源,否則請使用 az group delete 命令。 某些資源可能需要一些時間才能建立或刪除。
az group delete --name $resourceGroup
範例參考
此文稿會使用下表中所述的命令:
命令 | 注意事項 |
---|---|
az group create | 建立用來存放所有資源的資源群組。 |
az postgres server create | 建立裝載資料庫的 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器實例。 |
az postgres server configuration list | 列出伺服器的組態值。 |
az postgres server configuration set | 更新 伺服器的組態。 |
az postgres server-logs list | 列出伺服器的記錄檔。 |
az postgres server-logs download | 下載記錄檔。 |
az group delete | 刪除資源群組,包括所有的巢狀資源。 |
下一步
- 如需 Azure CLI 的詳細資訊: Azure CLI 檔。
- 嘗試其他腳本:適用於 適用於 PostgreSQL 的 Azure 資料庫 的 Azure CLI 範例 - 彈性伺服器實例
- 在 Azure 入口網站 中設定和存取伺服器記錄