使用 Azure CLI 启用和下载 Azure Database for MySQL 服务器的服务器慢查询日志Enable and download server slow query logs of an Azure Database for MySQL server using Azure CLI

备注

将要查看的是 Azure Database for MySQL 的新服务。You are viewing the new service of Azure Database for MySQL. 若要查看经典 MySQL Database for Azure 的文档,请访问此页To view the documentation for classic MySQL Database for Azure, please visit this page.

此示例 CLI 脚本可启用和下载单个 Azure Database for MySQL 服务器的慢查询日志。This sample CLI script enables and downloads the slow query logs of a single Azure Database for MySQL server.

本文需要 Azure CLI 2.0 或更高版本。This article requires Azure CLI version 2.0 or later. 通过运行 az --version 来查看版本。Check the version by running az --version. 请参阅安装 Azure CLI,了解如何安装或升级 Azure CLI 的版本。See Install Azure CLI to install or upgrade your version of Azure CLI.

示例脚本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 chinaeast2

# Create a MySQL 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 mysql server create \
--name mydemoserver \
--resource-group myresourcegroup \
--location chinaeast2 \
--admin-user myadmin \
--admin-password <server_admin_password> \
--sku-name GP_Gen5_2 \

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

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

# Set log_min_duration_statement time to 10 sec
az mysql 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 mysql 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 mysql 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 myresource

脚本说明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 mysql server createaz mysql server create 创建用于托管数据库的 MySQL 服务器。Creates a MySQL server that hosts the databases.
az mysql server configuration listaz mysql server configuration list 列出服务器的配置值。List the configuration values for a server.
az mysql server configuration setaz mysql server configuration set 更新服务器的配置。Update the configuration of a server.
az mysql server-logs listaz mysql server-logs list 列出服务器的日志文件。List log files for a server.
az mysql server-logs downloadaz mysql server-logs download 下载日志文件。Download log files.
az group deleteaz group delete 删除资源组,包括所有嵌套的资源。Deletes a resource group including all nested resources.

后续步骤Next steps