使用 MySQL Shell 获取 MySQL 诊断信息(译)

收集全面的诊断信息可能会让人望而却步。知道要运行哪些查询以获取所需数据更像是一种艺术形式,而非其他什么。幸运的是,对于那些不太擅长艺术的人来说,MySQL Shell 使得获取这些信息变得更加容易。让我们来看一下。

设置
在我们开始之前,我们需要连接到一个 MySQL 实例。在本演示中,我正在使用一个生产 MySQL 数据库,用于我编写的帮助我管理高尔夫联赛的 Web 应用程序。

当我首次尝试获取诊断信息时,我收到一条消息,告诉我需要更改两个全局变量:slow_query_log 需要设置为 ON,log_output 需要设置为 TABLE。 

将slow_query_log配置为ON:
mysql> set global slow_query_log=on;
Query OK, 0 rows affected (0.02 sec)

mysql> show variables like '%slow_query_log%';
+---------------------+---------------------------------+
| Variable_name       | Value          |
+---------------------+---------------------------------+
| slow_query_log      | ON            |
| slow_query_log_file | /var/lib/mysql/centos7-slow.log |
+---------------------+---------------------------------+
2 rows in set (0.00 sec)

确认log_output参数是否为'TABLE':
mysql> set global log_output='TABLE';
Query OK, 0 rows affected (0.01 sec)
mysql> show variables like '%log_output%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output    | TABLE |
+---------------+-------+
1 row in set (0.00 sec)

命令
我们运行的 MySQL Shell 命令以收集诊断信息是 util.debug.collectDiagnostics()。该方法接受两个参数。

数据文件将保存的路径。
这个参数是必需的。
这是运行 MySQL Shell 的机器上的路径,而不是我们连接到的服务器上的路径。
如果提供路径,将创建一个类似于以下名称的文件:mysql-diagnostics-<时间戳信息>.zip。
在这里您需要使用绝对路径。如果使用类似于 ~/path/to/folder 的路径,您将收到错误提示。
这是一个已知问题,并已报告。

一个选项 JSON 对象。
这个参数是可选的。
我针对我的 MySQL 实例运行的命令是:

需要将mysql shell切换到js模式下:
 PROD  MySQL:9.0.1 localhost:3306 ssl  SQL > \js
 PROD  MySQL:9.0.1 localhost:3306 ssl  JS > util.debug.collectDiagnostics("/root/", {slowQueries: true, });

完成后,此命令将在我的用户主目录下的 diag 文件夹中创建一个名为 mysql-diagnostics-<时间戳信息>.zip 的文件。收集的数据还将包括关于运行缓慢的查询的信息。

在完成此命令后,控制台的输出如下:
Collecting diagnostics information from mysql://root@localhost:3306...
Copying shell log file...
 - Gathering schema tables without a PK...
 - Gathering schema routine size...
 - Gathering schema table count...
 - Gathering schema unused indexes...
 - Copying MySQL error log file (/var/log/mysqld.log)
 - Gathering slow queries in 95 pctile...
 - Gathering slow queries summary by rows examined...
 - Gathering slow_log...
 - Gathering performance_schema.host_cache...
 - Gathering performance_schema.persisted_variables...
 - Gathering performance_schema.replication_applier_configuration...
 - Gathering performance_schema.replication_applier_filters...
 - Gathering performance_schema.replication_applier_global_filters...
 - Gathering performance_schema.replication_applier_status...
 - Gathering performance_schema.replication_applier_status_by_coordinator...
 - Gathering performance_schema.replication_applier_status_by_worker...
 - Gathering performance_schema.replication_asynchronous_connection_failover...
 - Gathering performance_schema.replication_asynchronous_connection_failover_managed...
 - Gathering performance_schema.replication_connection_configuration...
 - Gathering performance_schema.replication_connection_status...
 - Gathering performance_schema.replication_group_member_stats...
 - Gathering performance_schema.replication_group_members...
 - Gathering global variables...
 - Gathering XA RECOVER CONVERT xid...
 - Gathering SHOW BINARY LOGS...
 - Gathering SHOW REPLICAS...
 - Gathering SHOW BINARY LOG STATUS...
 - Gathering SHOW REPLICA STATUS...
 - Gathering replication master_info...
 - Gathering replication relay_log_info...
 - Gathering pfs actors...
 - Gathering pfs objects...
 - Gathering pfs consumers...
 - Gathering pfs instruments...
 - Gathering pfs threads...
 - Gathering performance_schema.metadata_locks...
 - Gathering performance_schema.threads...
 - Gathering sys.schema_table_lock_waits...
 - Gathering sys.session_ssl_status...
 - Gathering sys.session...
 - Gathering sys.processlist...
 - Gathering performance_schema.events_waits_current...
 - Gathering information_schema.innodb_trx...
 - Gathering information_schema.innodb_metrics...
 - Gathering sys.memory_by_host_by_current_bytes...
 - Gathering sys.memory_by_thread_by_current_bytes...
 - Gathering sys.memory_by_user_by_current_bytes...
 - Gathering sys.memory_global_by_current_bytes...
 - Gathering SHOW GLOBAL STATUS...
 - Gathering SHOW ENGINE INNODB STATUS...
 - Gathering SHOW ENGINE PERFORMANCE_SCHEMA STATUS...
 - Gathering SHOW FULL PROCESSLIST...
 - Gathering SHOW OPEN TABLES...
Collecting system information for centos7.9 (linux)
 -> Executing date
 -> Executing uname -a
 -> Executing getenforce
 -> Executing free -m
 -> Executing swapon -s
 ->

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值