1. 程式人生 > >mysql慢查詢日誌功能的使用

mysql慢查詢日誌功能的使用

-h ted variable 自帶 mine 查看 time oba nta

作用:mysql慢查詢日誌可監控有效率問題的SQL 。、

一、開啟mysql慢查詢日誌功能

1、查看是否開啟 未使用索引的SQL記錄日誌查詢

mysql> show variables like log_queries_not_using_indexes;
+-------------------------------+-------+
| Variable_name                 | Value |
+-------------------------------+-------+
| log_queries_not_using_indexes |
OFF | +-------------------------------+-------+ 1 row in set (0.00 sec)

開啟 未使用索引的SQL記錄日誌查詢

mysql> set global log_queries_not_using_indexes=on;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like log_queries_not_using_indexes;
+-------------------------------+-------+
| Variable_name                 |
Value | +-------------------------------+-------+ | log_queries_not_using_indexes | ON | +-------------------------------+-------+ 1 row in set (0.00 sec)

2、查看超過多長時間的查詢記入慢查詢日誌中

mysql> show variables like long_query_time;
+-----------------+-----------+
| Variable_name   | Value     |
+-----------------+-----------+ | long_query_time | 10.000000 | +-----------------+-----------+ 1 row in set (0.00 sec)

默認10s,為做測試,修改為0,即記錄所有。

mysql> set global long_query_time=0;
Query OK, 0 rows affected (0.00 sec)

ps:需要斷開重新連接,才會查看到更新的狀態

mysql> show variables like long_query_time;
+-----------------+----------+
| Variable_name   | Value    |
+-----------------+----------+
| long_query_time | 0.000000 |
+-----------------+----------+
1 row in set (0.00 sec)

3、查看是否開啟 mysql慢查詢日誌功能

mysql> show variables like slow_query_log;
+----------------+-------+
| Variable_name  | Value |
+----------------+-------+
| slow_query_log | OFF   |
+----------------+-------+
1 row in set (0.01 sec)

開啟慢查詢日誌功能

mysql> set global slow_query_log=on;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like slow_query_log;
+----------------+-------+
| Variable_name  | Value |
+----------------+-------+
| slow_query_log | ON    |
+----------------+-------+
1 row in set (0.01 sec)

4、查看日誌記錄位置

mysql> show variables like slow_query_log_file;
+---------------------+--------------------------------------+
| Variable_name       | Value                                |
+---------------------+--------------------------------------+
| slow_query_log_file | /usr/local/mysql/data/bogon-slow.log |
+---------------------+--------------------------------------+
1 row in set (0.00 sec)

5、執行若幹SQL語句,看是否記入日誌

查看最後20條記錄如下,成功記錄了最後20條SQL執行語句。

[root@bogon ~]# tail -20 /usr/local/mysql/data/bogon-slow.log 
# Time: 190115 17:46:21
# User@Host: root[root] @ localhost []
# Query_time: 0.000329  Lock_time: 0.000166 Rows_sent: 40  Rows_examined: 40
SET timestamp=1547545581;
show tables;
# Time: 190115 17:46:36
# User@Host: root[root] @ localhost []
# Query_time: 0.054437  Lock_time: 0.000115 Rows_sent: 104  Rows_examined: 104
SET timestamp=1547545596;
select * from TABLES;
# Time: 190115 17:49:12
# User@Host: root[root] @ localhost []
# Query_time: 0.001611  Lock_time: 0.000705 Rows_sent: 21  Rows_examined: 21
SET timestamp=1547545752;
desc TAbles;
# Time: 190115 17:49:41
# User@Host: root[root] @ localhost []
# Query_time: 0.002142  Lock_time: 0.000056 Rows_sent: 104  Rows_examined: 104
SET timestamp=1547545781;
select VERSION from Tables;

6、慢查日誌的存儲格式

主要有五部分組成

# Time: 190115 17:54:44                                                      執行時間
# User@Host: root[root] @ localhost []                                       執行主機        
# Query_time: 0.000073  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0   執行信息
SET timestamp=1547546084;                                                    時間戳
select database();                                                           執行語句

二、慢查詢日誌分析工具

1、mysql自帶的 mysqldumpslow工具

使用mysqldumpslow查看最近6條記錄

[root@bogon ~]# mysqldumpslow -t 6 /usr/local/mysql/data/bogon-slow.log 

Reading mysql slow query log from /usr/local/mysql/data/bogon-slow.log
Count: 1  Time=0.05s (0s)  Lock=0.00s (0s)  Rows=104.0 (104), root[root]@localhost
  select * from TABLES

Count: 1  Time=0.01s (0s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
  select databas()

Count: 1  Time=0.00s (0s)  Lock=0.00s (0s)  Rows=104.0 (104), root[root]@localhost
  select VERSION from Tables

Count: 1  Time=0.00s (0s)  Lock=0.00s (0s)  Rows=21.0 (21), root[root]@localhost
  desc TAbles

Count: 2  Time=0.00s (0s)  Lock=0.00s (0s)  Rows=1.0 (2), root[root]@localhost
  show variables like S

Count: 2  Time=0.00s (0s)  Lock=0.00s (0s)  Rows=40.0 (80), root[root]@localhost
  show tables

2、pt-query-digest分析工具

1)安裝

[root@bogon ~]# wget wget percona.com/get/pt-query-digest
[root@bogon ~]# chmod u+x pt-query-digest
[root@bogon ~]# mv /root/pt-query-digest /usr/bin/

2)啟動出現錯誤提示

[root@bogon bin]# pt-query-digest --help
Cant locate Digest/MD5.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl 
/usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /usr/bin/pt-query-digest line 2470. BEGIN failed--compilation aborted at /usr/bin/pt-query-digest line 2470.

安裝perl-Digest-MD5即可

[root@bogon bin]# yum install perl-Digest-MD5

mysql慢查詢日誌功能的使用