查看慢查詢是不是開啟{默許是關(guān)閉的}方法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服務(wù)后,設(shè)置生效;
系統(tǒng)默許值查看{默許慢查詢的時間為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關(guān)鍵詞的最后10次log
[root@tkudb mydata]# mysqldumpslow -s r -t 10 -g 'select' /mnt/mydata/tkudb-slow.log
方法3:集群數(shù)據(jù)庫,所有主機的慢日志集中寫到某1個監(jiān)控數(shù)據(jù)庫的表,由php統(tǒng)1調(diào)用顯示!
4.如何定期安全清算slow.log
1.先履行備份,然后對備份文件緊縮打包,最后清除slow.log