查看慢查询是不是开启{默许是关闭的}方法2:修改my.cnf{加入以下命令}
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)
#cat my.cnf
long_query_time = 3
log-slow-queries = /mnt/mydata/tkudb-slow.log
重启mysql服务后,设置生效;
系统默许值查看{默许慢查询的时间为10s}3.日志分析与处理
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)
1.利用文本处理工具如notepad+,处理tkudb-slow.log
2.使用mysqldumpslow工具
[root@tkudb mydata]# mysqldumpslow --help
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 (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
例子:
查询最后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:集群数据库,所有主机的慢日志集中写到某1个监控数据库的表,由php统1调用显示!
4.如何定期安全清算slow.log
1.先履行备份,然后对备份文件紧缩打包,最后清除slow.log