Home > Database > Mysql Tutorial > body text

mysql慢查询日志开启和存储格式_MySQL

WBOY
Release: 2016-06-01 13:01:39
Original
1168 people have browsed it

mysql版本是mysql5.6.22,安装环境windows7。

1.使用该查询日志可以找到有效率问题的sql语句,并记录下来,进行监控。

可以使用如下语句查询和设置慢查询日志

(1) 查看慢查询日志是否开启
SHOW VARIABLES LIKE '%show_query_log%'; 或是 SHOW VARIABLES LIKE '%show_query_log%'\G (mysql命令行中使用)
查询结果如下
Variable_name: slow_query_log
Value: OFF
上边Value表示未开启
Variable_name: slow_query_log_file
Value: D:\ProgramFiles\MySQL5.6.22\mysql_master\data\LHY-slow.log

上边Value表示日志存放的路径

\

设置开启状态:

set global slow_query_log = on;

(2) 查看未使用索引的查询日志是否开启
SHOW VARIABLES LIKE '%log_queries_not_using_indexes%'; 或是 SHOW VARIABLES LIKE '%log_queries_not_using_indexes%'\G (mysql命令行中使用)
查询结果如下
Variable_name: log_queries_not_using_indexes
Value: OFF

上边Value表示未开启

\

设置开启状态:

set global log_queries_not_using_indexes = on;

(3) 查看超过设定时间的查询日志
SHOW VARIABLES LIKE '%long_query_time%'; 或是 SHOW VARIABLES LIKE '%long_query_time%'\G (mysql命令行中使用)
查询结果如下:
Variable_name: long_query_time
Value: 10.000000

上边value:10s表示记录执行时间超过10秒的sql

\

设置执行时间为1s
set global long_query_time = 1;

exit;

注意1:修改执行之间之后要退出重新登录才会起作用.
注意2:设置时间过短导致日志记录过多会很快将磁盘空间沾满,所以应该定期执行磁盘清理,这里设置为1是为了查看执行效果,生产环境中需要自己设置.



以上三个步骤执行完成之后,从数据库中执行任何sql语句就都会被记录到日志中,可以到第一步中那个日志查看日志信息.


以上设置是在控制台处理的,当数据库重启之后,该设置即失效;
长期有效的方式是在mysql安装目录下找到my.ini文件,若是没有该文件,只有mysql-default.ini文件
那么就将该文件备份,然后改名为mysql.ini,然后在该文件中[mysqld]下边添加如下的配置信息即可。


slow_query_log=on
slow_query_log_file=D:/ProgramFiles/MySQL5.6.22/mysql_master/data/LHY-slow.log
log_queries_not_using_indexes=on
long_query_time=1


2.存储格式

 

# Time: 150401 11:24:27
# User@Host: root[root] @ localhost [127.0.0.1] Id: 7
# Query_time: 0.034002 Lock_time: 0.000000 Rows_sent: 3 Rows_examined: 3
use libu;
SET timestamp=1427858667;
select * from aaa;


分析如下:
(1) Time: 执行时间
(2) User@Host: 执行sql的主机信息
(3) Query_time: sql的执行信息,Lock_time: 锁定时间, Rows_sent: 发送(结果)行数, Rows_examined:扫描的行数
(4) timestamp: 执行时间

(5) select * from aaa; : 查询语句内容

3.慢查询日志分析工具

5种工具: mysqldumpslow,mysqlsla,myprofi,mysql-explain-slow-log,mysqllogfilter

mysqldumpslow mysql自带的分析工具

致歉:本人目前还未找到这些工具在window上使用的方式,若是哪位大神有相关的教程,请@我,谢谢!!!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!