使用 Azure CLI 啟用並下載「適用於 MariaDB 的 Azure 資料庫」伺服器的伺服器慢速查詢記錄Enable and download server slow query logs of an Azure Database for MariaDB server using Azure CLI

此範例 CLI 指令碼會啟用並下載單一「適用於 MariaDB 的 Azure 資料庫」伺服器的慢速查詢記錄。This sample CLI script enables and downloads the slow query logs of a single Azure Database for MariaDB server.

如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶If you don't have an Azure subscription, create a free account before you begin.

必要條件Prerequisites

  • Azure Cloud Shell中使用 Bash 環境。Use the Bash environment in Azure Cloud Shell.

    在新視窗中啟動 Cloud ShellLaunch Cloud Shell in a new window

  • 如果想要,請安裝 Azure CLI 以執行 CLI 參考命令。If you prefer, install the Azure CLI to run CLI reference commands.

    • 如果您使用的是本機安裝,請使用 az login 命令,透過 Azure CLI 來登入。If you're using a local installation, sign in to the Azure CLI by using the az login command. 請遵循您終端機上顯示的步驟,完成驗證程序。To finish the authentication process, follow the steps displayed in your terminal. 如需其他登入選項,請參閱使用 Azure CLI 登入For additional sign-in options, see Sign in with the Azure CLI.

    • 出現提示時,請在第一次使用時安裝 Azure CLI 擴充功能。When you're prompted, install Azure CLI extensions on first use. 如需擴充功能詳細資訊,請參閱使用 Azure CLI 擴充功能For more information about extensions, see Use extensions with the Azure CLI.

    • 執行 az version 以尋找已安裝的版本和相依程式庫。Run az version to find the version and dependent libraries that are installed. 若要升級至最新版本,請執行 az upgradeTo upgrade to the latest version, run az upgrade.

  • 本文需要 2.0 版或更新版本的 Azure CLI。This article requires version 2.0 or later of the Azure CLI. 如果您是使用 Azure Cloud Shell,就已安裝最新版本。If using Azure Cloud Shell, the latest version is already installed.

範例指令碼Sample script

在此範例指令碼中,請編輯醒目提示的命令列,將系統管理員使用者名稱和密碼更新為您自己的使用者名稱和密碼。In this sample script, edit the highlighted lines to update the admin username and password to your own. 請以您自己的伺服器記錄檔名稱取代 az monitor 命令中的 <log_file_name>。Replace the <log_file_name> in the az monitor commands with your own server log file name.

#!/bin/bash

# Create a resource group
az group create \
--name myresourcegroup  \
--location westus

# 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
# Substitute the <server_admin_password> with your own value
az mariadb server create \
--name mydemoserver \
--resource-group myresourcegroup \
--location westus \
--admin-user myadmin \
--admin-password <server_admin_password> \
--sku-name GP_Gen5_2 \

# List the configuration options for review
az mariadb server configuration list \
--resource-group myresourcegroup  \
--server mydemoserver

# Turn on statement level log
az mariadb server configuration set \
--name log_statement \
--resource-group myresourcegroup \
--server mydemoserver \
--value all

# Set log_min_duration_statement time to 10 sec
az mariadb server configuration set \
--name log_min_duration_statement \
--resource-group myresourcegroup \
--server mydemoserver \
--value 10000

# List the available log files and direct to a text file
az mariadb server-logs list \
--resource-group myresourcegroup \
--server mydemoserver > log_files_list.txt

# Download log file from Azure 
# Review log_files_list.txt to find the server log file name for the desired timeframe
# Substitute the <log_file_name> with your server log file name
# Creates the postgresql-<date>_000000.log file in the current command line path
az mariadb server-logs download \
--name <log_file_name> \
--resource-group myresourcegroup \
--server mydemoserver

清除部署Clean up deployment

執行指令碼之後,請使用下列命令來移除資源群組及其相關聯的所有資源。Use the following command to remove the resource group and all resources associated with it after the script has been run.

#!/bin/bash
az group delete --name myresourcegroup

指令碼說明Script explanation

此指令碼會使用下表中簡述的命令:This script uses the commands outlined in the following table:

命令Command 注意事項Notes
az group createaz group create 建立用來存放所有資源的資源群組。Creates a resource group in which all resources are stored.
az mariadb server createaz mariadb server create 建立託管資料庫的 MariaDB 伺服器。Creates a MariaDB server that hosts the databases.
az mariadb server configuration listaz mariadb server configuration list 列出伺服器的設定值。List the configuration values for a server.
az mariadb server configuration setaz mariadb server configuration set 更新伺服器的設定。Update the configuration of a server.
az mariadb server-logs listaz mariadb server-logs list 列出伺服器的記錄檔。List log files for a server.
az mariadb server-logs downloadaz mariadb server-logs download 下載記錄檔。Download log files.
az group deleteaz group delete 刪除資源群組,包括所有的巢狀資源。Deletes a resource group including all nested resources.

後續步驟Next steps