慢查询日志会将查询过程中超出你设置的时间的查询记录下来,以便供开发者进行分析和优化。

1. 开启慢查询

1.1 查看当前设置

mysql> show variables like "%query%";

输出

+------------------------------+-----------------------------------------+
| Variable_name | Value |
+------------------------------+-----------------------------------------+
| binlog_rows_query_log_events | OFF |
| ft_query_expansion_limit | 20 |
| have_query_cache | YES |
| long_query_time | 10.000000 |
| query_alloc_block_size | 8192 |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 16777216 |
| query_cache_type | OFF |
| query_cache_wlock_invalidate | OFF |
| query_prealloc_size | 8192 |
| slow_query_log | OFF |
| slow_query_log_file | /var/lib/mysql/lgj-Lenovo-G470-slow.log |
+------------------------------+-----------------------------------------+

三个参数

  • slow_query_log   ON/OFF ,使能开关
  • slow_query_log_file   慢查询日志目录和文件名称
  • long_query_time    超过该时间则进行记录,5.1之前只设置到秒,5.1开始支持毫秒。

注意,开启慢查询会影响性能,因此应当在某一段时间内开启,记录一段时间后关闭掉。

1.2 配置

lgj@lgj-Lenovo-G470:~/db-analysis$ whereis mysql
mysql: /usr/bin/mysql /usr/lib/mysql /etc/mysql /usr/local/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz

我的配置文件在/etc/mysql目录下的my.cnf

配置

[mysqld]
port=3307
skip-grant-tables
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

#打开慢查询
slow_query_log = ON
#设置超时时间为0,也就是记录所有的查询
long_query_time = 0

设置完后保存,重新启动mysql

service mysql  restart

重新查看参数,已经更改。

mysql> show variables like "%query%";
+------------------------------+-----------------------------------------+
| Variable_name | Value |
+------------------------------+-----------------------------------------+
| binlog_rows_query_log_events | OFF |
| ft_query_expansion_limit | 20 |
| have_query_cache | YES |
| long_query_time | 0.000000 |
| query_alloc_block_size | 8192 |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 16777216 |
| query_cache_type | OFF |
| query_cache_wlock_invalidate | OFF |
| query_prealloc_size | 8192 |
| slow_query_log | ON |
| slow_query_log_file | /var/lib/mysql/lgj-Lenovo-G470-slow.log |
+------------------------------+-----------------------------------------+

1.3 查询

随便执行一条查询语句,然后查看慢查询日志。

/usr/sbin/mysqld, Version: 5.7.25-0ubuntu0.18.04.2 ((Ubuntu)). started with:
Tcp port: 3306 Unix socket: /var/run/mysqld/mysqld.sock
Time Id Command Argument
/usr/sbin/mysqld, Version: 5.7.25-0ubuntu0.18.04.2-log ((Ubuntu)). started with:
Tcp port: 3306 Unix socket: /var/run/mysqld/mysqld.sock
Time Id Command Argument
/usr/sbin/mysqld, Version: 5.7.25-0ubuntu0.18.04.2-log ((Ubuntu)). started with:
Tcp port: 3306 Unix socket: /var/run/mysqld/mysqld.sock
Time Id Command Argument
;
# Time: 2019-02-27T14:07:42.841770Z
# User@Host: skip-grants user[lgj] @ localhost [] Id: 2
# Query_time: 0.016232 Lock_time: 0.000000 Rows_sent: 0 Rows_examined: 0
SET timestamp=1551276462;
;
# Time: 2019-02-27T14:07:51.774192Z
# User@Host: skip-grants user[lgj] @ localhost [] Id: 2
# Query_time: 0.000485 Lock_time: 0.000221 Rows_sent: 1 Rows_examined: 1
SET timestamp=1551276471;
select * from user;
# Time: 2019-02-27T14:07:55.839223Z
# User@Host: skip-grants user[lgj] @ localhost [] Id: 2
# Query_time: 0.000564 Lock_time: 0.000259 Rows_sent: 1 Rows_examined: 1
SET timestamp=1551276475;
# 查询语句
select * from user;
# 记录时间
# Time: 2019-02-27T14:08:07.404666Z
# User@Host: skip-grants user[lgj] @ localhost [] Id: 2
#查询时间 锁表时间
# Query_time: 0.006318 Lock_time: 0.000435 Rows_sent: 13 Rows_examined: 1026
SET timestamp=1551276487;

从上面可以获知查询时间和锁表时间,但是如果文件比较大,查找时间最长的查询将会非常麻烦,需要使用相关的工具来进行分析。

《高性能mysql》推荐使用 qt-query-digest 工具

2. qt-query-digest的使用

2.1 安装

  • 创建目录: mkdir db-analysis && cd db-analysis
  • 下载 pt-query-digest: curl -LO https://percona.com/get/pt-query-digest
  • 设置执行权限:chmod +x pt-query-digest
  • 将慢查询日志的文件复制到当前的目录下
  • 执行分析:  ./pt-query-digest lgj-Lenovo-G470-slow.log

结果

# 220ms user time, 10ms system time, 33.67M rss, 90.27M vsz
# Current date: Wed Feb 27 22:55:05 2019
# Hostname: lgj-Lenovo-G470
# Files: lgj-Lenovo-G470-slow.log
# Overall: 9 total, 7 unique, 0.24 QPS, 0.00x concurrency ________________
# Time range: 2019-02-27T14:07:29 to 2019-02-27T14:08:07
# Attribute total min max avg 95% stddev median
# ============ ======= ======= ======= ======= ======= ======= =======
# Exec time 185ms 225us 122ms 21ms 122ms 37ms 6ms
# Lock time 2ms 0 485us 186us 467us 166us 159us
# Rows sent 50 0 13 5.56 12.54 5.25 0.99
# Rows examine 2.03k 0 1.00k 230.44 1012.63 419.72 0.99
# Query size 198 11 32 22 31.70 7.35 17.65 # Profile
# Rank Query ID Response time Calls R/Call V/M
# ==== ================================= ============= ===== ====== =====
# 1 0x751417D45B8E80EE5CBA2034458B... 0.1223 66.1% 1 0.1223 0.00 SHOW DATABASES
# 2 0xA11944C87A6A5C16FB38455BF703... 0.0320 17.3% 1 0.0320 0.00 SELECT
# 3 0x898255B1BE4F8C3044AE35A18286... 0.0155 8.4% 1 0.0155 0.00 ADMIN INIT DB
# 4 0xE77769C62EF669AA7DD5F6760F2D... 0.0134 7.3% 2 0.0067 0.00 SHOW VARIABLES
# MISC 0xMISC 0.0018 1.0% 4 0.0004 0.0 <3 ITEMS> # Query 1: 0 QPS, 0x concurrency, ID 0x751417D45B8E80EE5CBA2034458B5BC9 at byte 1471
# This item is included in the report because it matches --limit.
# Scores: V/M = 0.00
# Time range: all events occurred at 2019-02-27T14:07:42
# Attribute pct total min max avg 95% stddev median
# ============ === ======= ======= ======= ======= ======= ======= =======
# Count 11 1
# Exec time 66 122ms 122ms 122ms 122ms 122ms 0 122ms
# Lock time 6 110us 110us 110us 110us 110us 0 110us
# Rows sent 20 10 10 10 10 10 0 10
# Rows examine 0 10 10 10 10 10 0 10
# Query size 7 14 14 14 14 14 0 14
# String:
# Databases microblog
# Hosts localhost
# Users skip-grants user
# Query_time distribution
# 1us
# 10us
# 100us
# 1ms
# 10ms
# 100ms ################################################################
# 1s
# 10s+
show databases\G # Query 2: 0 QPS, 0x concurrency, ID 0xA11944C87A6A5C16FB38455BF7035609 at byte 1008
# This item is included in the report because it matches --limit.
# Scores: V/M = 0.00
# Time range: all events occurred at 2019-02-27T14:07:42
# Attribute pct total min max avg 95% stddev median
# ============ === ======= ======= ======= ======= ======= ======= =======
# Count 11 1
# Exec time 17 32ms 32ms 32ms 32ms 32ms 0 32ms
# Lock time 0 0 0 0 0 0 0 0
# Rows sent 2 1 1 1 1 1 0 1
# Rows examine 0 0 0 0 0 0 0 0
# Query size 8 17 17 17 17 17 0 17
# String:
# Hosts localhost
# Users skip-grants user
# Query_time distribution
# 1us
# 10us
# 100us
# 1ms
# 10ms ################################################################
# 100ms
# 1s
# 10s+
# EXPLAIN /*!50100 PARTITIONS*/
SELECT DATABASE()\G # Query 3: 0 QPS, 0x concurrency, ID 0x898255B1BE4F8C3044AE35A182869033 at byte 1225
# This item is included in the report because it matches --limit.
# Scores: V/M = 0.00
# Time range: all events occurred at 2019-02-27T14:07:42
# Attribute pct total min max avg 95% stddev median
# ============ === ======= ======= ======= ======= ======= ======= =======
# Count 11 1
# Exec time 8 15ms 15ms 15ms 15ms 15ms 0 15ms
# Lock time 0 0 0 0 0 0 0 0
# Rows sent 0 0 0 0 0 0 0 0
# Rows examine 0 0 0 0 0 0 0 0
# Query size 15 30 30 30 30 30 0 30
# String:
# Databases microblog
# Hosts localhost
# Users skip-grants user
# Query_time distribution
# 1us
# 10us
# 100us
# 1ms
# 10ms ################################################################
# 100ms
# 1s
# 10s+
administrator command: Init DB\G # Query 4: 0.05 QPS, 0.00x concurrency, ID 0xE77769C62EF669AA7DD5F6760F2D2EBB at byte 775
# This item is included in the report because it matches --limit.
# Scores: V/M = 0.00
# Time range: 2019-02-27T14:07:30 to 2019-02-27T14:08:07
# Attribute pct total min max avg 95% stddev median
# ============ === ======= ======= ======= ======= ======= ======= =======
# Count 22 2
# Exec time 7 13ms 6ms 7ms 7ms 7ms 559us 7ms
# Lock time 54 920us 435us 485us 460us 485us 35us 460us
# Rows sent 52 26 13 13 13 13 0 13
# Rows examine 98 2.00k 1.00k 1.00k 1.00k 1.00k 0 1.00k
# Query size 29 58 29 29 29 29 0 29
# String:
# Databases microblog
# Hosts localhost
# Users skip-grants user
# Query_time distribution
# 1us
# 10us
# 100us
# 1ms ################################################################
# 10ms
# 100ms
# 1s
# 10s+
show variables like "%query%"\G

MySql开启慢查询日志并使用pt-query-digest 分析的更多相关文章

  1. MySQL 开启慢查询日志

    1.1 简介 开启慢查询日志,可以让MySQL记录下查询超过指定时间的语句,通过定位分析性能的瓶颈,才能更好的优化数据库系统的性能. 1.2 登录数据库查看 [root@localhost lib]# ...

  2. MySQL开启慢查询日志时报Errcode: 13 的解决方法

    开启慢查询日志时会出现(Errcode: 13 - Permission denied)文件找不到的错误,但文件明明是存在的并且有读写的权限. mysql> set global slow_qu ...

  3. MySQL 开启慢查询日志与普通日志

    一.开启满查询日志 1.查看慢查询日志 SHOW VARIABLES LIKE '%slow%'; 2.开启慢查询日志 set GLOBAL slow_query_log =on; 3.设置慢查询日志 ...

  4. aws mysql 开启慢查询日志, 并利用mysqlsla 分析

    1.开启慢查询日志服务 (a) sql 查询配置 # 查看慢日志是否开启,开启为ON show variables like 'slow_query%'; show variables like 'l ...

  5. mysql开启慢查询日志及查询--windows

    MySQL慢查询配置 1. 慢查询有什么用? 它能记录下所有执行超过long_query_time时间的SQL语句, 帮你找到执行慢的SQL, 方便我们对这些SQL进行优化. 2. 如何开启慢查询? ...

  6. MYSQL开启慢查询日志实施

    查看当前服务器是否开启慢查询:1.快速办法,运行sql语句show VARIABLES like "%slow%" 2.直接去my.conf中查看.my.conf中的配置(放在[m ...

  7. 操作3 mongodb和mysql 开启慢查询日志 ,以及mongodb从配置文件启动

    1. mongodb从配置文件启动 创建配置文件:/usr/local/mongodb/etc/mongodb.conf 配置文件的内容为: #Directory and relavent set d ...

  8. mysql开启慢查询日志

    5.1版本之前,在 my.cnf添加如下信息, long_query_time=1 log_slow_queries=/data/mysql/slow.log 5.1版本之后,在 my.cnf添加如下 ...

  9. Mysql: 开启慢查询日志[ERROR] unknown variable 'log-slow-queries'处理办法

    参考: http://www.dataguru.cn/thread-305503-1-1.html # slow query log qjp 20160921 # mysql5.6版本以上,取消了参数 ...

随机推荐

  1. 使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境

    前言 但凡一个略有规模的项目都需要一个持续集成环境的支撑,为什么需要持续集成环境,我们来看一个例子.假如一个项目,由A.B两位程序员来协作开发,A负责前端模块,B负责后端模块,前端依赖后端.A和B都习 ...

  2. Python 字典(Dictionary) has_key()方法

    描述 Python 字典(Dictionary) has_key() 函数用于判断键是否存在于字典中,如果键在字典dict里返回true,否则返回false. 语法 has_key()方法语法:dic ...

  3. python使用@property

    在绑定属性时,如果我们直接把属性暴露出去,虽然写起来很简单,但是,没办法检查参数,导致可以把成绩随便改: s = Student() s.score = 9999 这显然不合逻辑.为了限制score的 ...

  4. [论文解读]CNN网络可视化——Visualizing and Understanding Convolutional Networks

    概述 虽然CNN深度卷积网络在图像识别等领域取得的效果显著,但是目前为止人们对于CNN为什么能取得如此好的效果却无法解释,也无法提出有效的网络提升策略.利用本文的反卷积可视化方法,作者发现了AlexN ...

  5. kv.go

    package clientv3 import (     pb "github.com/coreos/etcd/etcdserver/etcdserverpb" //protob ...

  6. 记录一波由会话堵塞导致tomcat应用故障事件

    一.故障基本信息 发生时间 消除时间 故障历时 故障类别 影响 2018-5-17 18:14:30 2018-05-18 08:58:15 16小时 应用故障 业务瘫痪,用户投诉 二.故障现象 AP ...

  7. 使用ASIFormDataRequest完成用户的登录操作

    ASIFormDataRequest是用于向表单post数据或get数据,可以用于用户向服务器端发送请求完成登录注册,上传下载数据的操作. 之前写过一篇文章是介绍使用ios内置的功能完成登录操作(NS ...

  8. linux yum命令 使用

    yum -y install 包名(支持*) :自动选择y,全自动 yum install 包名(支持*) :手动选择y or n yum remove 包名(不支持*) rpm -ivh 包名(支持 ...

  9. 记一次logback传输日志到logstash根据自定义设置动态创建ElasticSearch索引

    先说背景,由于本人工作需要创建很多小应用程序,而且在微服务的大环境下,服务越来越多,然后就导致日志四分五裂,到处都有,然后就有的elk,那么问题来了 不能每个小应用都配置一个 logstash 服务来 ...

  10. java可用与串口通信的一些库

    java原生对串口的支持只有javax.comm,javax.comm比较老了,而且不支持64位系统,我在看jlibmodbus(一个java实现的modbus协议栈)的时候发现了几个可供使用的jav ...