3

使用mysqldumpslow分析mysql慢日志

 3 years ago
source link: https://www.bboy.app/2022/04/25/%E4%BD%BF%E7%94%A8mysqldumpslow%E5%88%86%E6%9E%90mysql%E6%85%A2%E6%97%A5%E5%BF%97/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

使用mysqldumpslow分析mysql慢日志

发表于 2022-04-25

分类于

linux

20220425-1.png

mysqldumpslow 是mysql自带的分析满查询日志的工具

首先开启mysql的满查询日志,mysql的满查询日志可以放在mysql的表中也可以放在文件中,下面是具体的参数

# 开启慢查询
slow_query_log = on
# 慢查询输出到表和文件中
log_output = TABLE,FILE
# 如果查询时间超过2s就定义为是慢查询
long_query_time = 2
# 慢查询文件的路径,如果不是指定绝对路径,比如就和我下面一样写,默认就在mysql的data文件夹中比如/var/lib/mysql
slow_query_log_file = slow.log

之后重启mysql

等一段时间有慢日志之后就可以使用mysqldumpslow分析了

其实mysqldumpslow的参数也很简单

Usage: mysqldumpslow [ OPTS... ] [ LOGS... ]

Parse and summarize the MySQL slow query log. Options are

--verbose verbose
--debug debug
--help write this text to standard output

-v verbose
-d debug
-s ORDER what to sort by (aa, ae, al, ar, at, a, c, e, l, r, t), 'at' is default
aa: average rows affected
ae: aggregated rows examined
al: average lock time
ar: average rows sent
at: average query time
a: rows affected
c: count
e: rows examined
l: lock time
r: rows sent
t: query time
-r reverse the sort order (largest last instead of first)
-t NUM just show the top n queries
-a don't abstract all numbers to N and strings to 'S'
-n NUM abstract numbers with at least n digits within names
-g PATTERN grep: only consider stmts that include this string
-h HOSTNAME hostname of db server for *-slow.log filename (can be wildcard),
default is '*', i.e. match all
-i NAME name of server instance (if using mysql.server startup script)
-l don't subtract lock time from total time

介绍两个常见用法

查询最慢的前3条

mysqldumpslow -s at -t 3 ./slow.log

查询次数最多的前3条

mysqldumpslow -s ac -t 3 slow.log

欢迎关注我的博客www.bboy.app

Have Fun


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK