1.慢查询简介
顾名思义,慢查询日志中记录的是执行时间较长的查询的SQL语句,默认文件名为hostname-slow.log,默认目录也是数据目录。
慢查询日志采用的是简单的文本格式,可以通过各种文本编辑器查看其中的内容。
其中记录了语句执行的时刻,执行所消耗的时间,执行用户,连接主机等相关信息。
MySQL提供了专门的工具mysqlslowdump来分析慢查询日志,DBA可借此工具定位数据库可能存在的性能问题。
2.开启方法
步骤1:开启慢查询功能的设置
方法1:修改全局参数开启
查看慢查询是否开启{默认是关闭的}mysql> show variables like "%slow%";+---------------------+----------------------------------+| Variable_name | Value |+---------------------+----------------------------------+| log_slow_queries | OFF || slow_launch_time | 2 || slow_query_log | OFF || slow_query_log_file | /mnt/mydata/tkudb-slow.log |+---------------------+----------------------------------+4 rows in set (0.00 sec)开启慢查询mysql> set global slow_query_log=‘ON‘;Query OK, 0 rows affected (0.01 sec)验证慢查询mysql> show variables like "%slow%";+---------------------+----------------------------------+| Variable_name | Value |+---------------------+----------------------------------+| log_slow_queries | ON || slow_launch_time | 2 || slow_query_log | ON || slow_query_log_file | /mnt/mydata/tkudb-slow.log |+---------------------+----------------------------------+4 rows in set (0.00 sec)
方法2:修改my.cnf{加入如下命令}
#cat my.cnflong_query_time = 3log-slow-queries = /mnt/mydata/tkudb-slow.log重启mysql服务后,设置生效;
步骤2:设置慢查询的时间
系统默认值查看{默认慢查询的时间为10s}mysql> show variables like "%long%";+---------------------------------------------------+-----------+| Variable_name | Value |+---------------------------------------------------+-----------+| long_query_time | 10.000000 || max_long_data_size | 1048576 || performance_schema_events_waits_history_long_size | 10000 |+---------------------------------------------------+-----------+3 rows in set (0.00 sec)mysql> set global long_query_time=3;Query OK, 0 rows affected (0.00 sec)
3.日志分析与处理
1.利用文本处理工具如notepad+,处理tkudb-slow.log2.使用mysqldumpslow工具[root@tkudb mydata]# mysqldumpslow --helpUsage: 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 (al, at, ar, c, l, r, t), ‘at‘ is defaultal: average lock timear: average rows sentat: average query timec: countl: lock timer: rows sentt: 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例子:查询最后10次log[root@tkudb mydata]# mysqldumpslow -s r -t 10 /mnt/mydata/tkudb-slow.log查询带有select关键词的最后10次log[root@tkudb mydata]# mysqldumpslow -s r -t 10 -g ‘select‘ /mnt/mydata/tkudb-slow.log方法3:集群数据库,所有主机的慢日志集中写到某一个监控数据库的表,由php统一调用显示!
4.如何定期安全清理slow.log
1.先执行备份,然后对备份文件压缩打包,最后清除slow.log
MySQL慢查询整理
标签:linux下开启mysql慢查询 mysql慢查询
本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉
本文系统来源:http://blog.csdn.net/jacson_bai/article/details/45394343