mysql 慢查询日志开启配置与mysqldumpslow工具使用

命令行查看慢查询配置。

注:命令行的配置会在重启后失效。一般建议配置在配置文件中。

以下命令操作在sqlyog完成。

// 显示慢查询是否开启,慢查询记录日志文件位置
SHOW VARIABLES LIKE 'slow_query%';
// 显示sql执行时间大于此时间会记录到日志中
SHOW VARIABLES LIKE 'long_query_time';

// 命令行设置慢查询
// 开启慢查询
set gloabl slow_query = 1;
// 设置文件位置
SET GLOBAL slow_query_log_file = '/var/lib/mysql/localhost-slow.log'
// 设置时间
SET GLOBAL long_query_time = 5;

找到mysql加载的配置文件。一般为 /etc/my.cnf,在nysqld标签下面添加以下语句重启即可

[mysqld]
slow_query_log=1
slow_query_log_file=/var/lib/mysql/localhost-slow.log
long_query_time=3

mysqldumpslow 是 mysql提供的慢日志分析工具。

命令如下:自行参考,用阿里云rds多了,有页面。哈哈哈

[root@localhost db01]# mysqldumpslow -help
// 可以使用多个命令组合
Usage: mysqldumpslow [ OPTS... ] [ LOGS... ]
// 分析和总结MySQL慢速查询日志。选项包括
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 (al, at, ar, c, l, r, t), 'at' is default
                al: average lock time
                ar: average rows sent
                at: average query time
                 c: count
                 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

[root@localhost db01]# 

猜你喜欢

转载自blog.csdn.net/now19930616/article/details/104339519
今日推荐