使用 Azure CLI 配置和访问慢查询日志Configure and access slow query logs by 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.

可以使用 Azure CLI(Azure 命令行实用工具)下载 Azure Database for MySQL 慢查询日志。You can download the Azure Database for MySQL slow query logs by using Azure CLI, the Azure command-line utility.

先决条件Prerequisites

若要逐步执行本操作方法指南,需要:To step through this how-to guide, you need:

配置日志记录Configure logging

通过执行下列步骤,可以对服务器进行配置以访问 MySQL 慢查询日志:You can configure the server to access the MySQL slow query log by taking the following steps:

  1. 通过将 slow_query_log 参数设置为 ON 启用慢查询日志记录。Turn on slow query logging by setting the slow_query_log parameter to ON.
  2. 使用 log_output 选择要将日志输出到的位置。Select where to output the logs to using log_output. 若要将日志同时发送到本地存储和 Azure Monitor 诊断日志,请选择“File” 。To send logs to both local storage and Azure Monitor Diagnostic Logs, select File. 若要仅将日志发送到 Azure Monitor 日志,请选择“None” To send logs only to Azure Monitor Logs, select None
  3. 调整其他参数,例如 long_query_time 和 log_slow_admin_statements 。Adjust other parameters, such as long_query_time and log_slow_admin_statements.

若要了解如何通过 Azure CLI 设置这些参数的值,请参阅如何配置服务器参数To learn how to set the value of these parameters through Azure CLI, see How to configure server parameters.

例如,以下 CLI 命令将启用慢查询日志、将长查询时间设置为 10 秒并禁用慢管理语句的日志记录。For example, the following CLI command turns on the slow query log, sets the long query time to 10 seconds, and then turns off the logging of the slow admin statement. 最后,它将列出配置选项供复查。Finally, it lists the configuration options for your review.

az mysql server configuration set --name slow_query_log --resource-group myresourcegroup --server mydemoserver --value ON
az mysql server configuration set --name log_output --resource-group myresourcegroup --server mydemoserver --value FILE
az mysql server configuration set --name long_query_time --resource-group myresourcegroup --server mydemoserver --value 10
az mysql server configuration set --name log_slow_admin_statements --resource-group myresourcegroup --server mydemoserver --value OFF
az mysql server configuration list --resource-group myresourcegroup --server mydemoserver

列出 Azure Database for MySQL 服务器的日志List logs for Azure Database for MySQL server

如果“log_output” 配置为“File”,则可以直接从服务器的本地存储访问日志。If log_output is configured to "File", you can access logs directly from the server's local storage. 若要列出服务器的可用慢查询日志文件,请运行 az mysql server-logs list 命令。To list the available slow query log files for your server, run the az mysql server-logs list command.

可以列出资源组“myresourcegroup” 下的服务器 mydemoserver.mysql.database.chinacloudapi.cn 的日志文件。You can list the log files for server mydemoserver.mysql.database.chinacloudapi.cn under the resource group myresourcegroup. 然后在日志文件列表中找到名为“log_files_list.txt”的文本文件 。Then direct the list of log files to a text file called log_files_list.txt.

az mysql server-logs list --resource-group myresourcegroup --server mydemoserver > log_files_list.txt

从服务器下载日志Download logs from the server

如果“log_output” 配置为“File”,则可以使用 az mysql server-logs download 命令从服务器下载单个日志文件。If log_output is configured to "File", you can download individual log files from your server with the az mysql server-logs download command.

使用以下示例,可以将资源组“myresourcegroup”下服务器 mydemoserver.mysql.database.chinacloudapi.cn 的特定日志文件下载到本地环境 。Use the following example to download the specific log file for the server mydemoserver.mysql.database.chinacloudapi.cn under the resource group myresourcegroup to your local environment.

az mysql server-logs download --name 20170414-mydemoserver-mysql.log --resource-group myresourcegroup --server mydemoserver

后续步骤Next steps