同大多数关系型数据库一样,日志文件是MySQL数据库的重要组成部分。mysql有几种不同的日志文件,通常包括错误日志文件,二进制日志,通用日志,慢查询日志,等等。这些日志可以帮助我们定位mysqld内部发生的事件,数据库性能故障,记录数据的变更历史,用户恢复数据库等等。本文主要描述通用查询日志。

 

1、MySQL日志文件系统的组成
   a、错误日志:记录启动、运行或停止mysqld时出现的问题。
   b、通用日志:记录建立的客户端连接和执行的语句。
   c、更新日志:记录更改数据的语句。该日志在MySQL 5.1中已不再使用。
   d、二进制日志:记录所有更改数据的语句。还用于复制。
   e、慢查询日志:记录所有执行时间超过long_query_time秒的所有查询或不使用索引的查询。
   f、Innodb日志:innodb redo log
   
   缺省情况下,所有日志创建于mysqld数据目录中。
   可以通过刷新日志,来强制mysqld来关闭和重新打开日志文件(或者在某些情况下切换到一个新的日志)。
   当你执行一个FLUSH LOGS语句或执行mysqladmin flush-logs或mysqladmin refresh时,则日志被老化。
   对于存在MySQL复制的情形下,从复制服务器将维护更多日志文件,被称为接替日志。

 

2、慢查询日志
   慢查询日志是将mysql服务器中影响数据库性能的相关SQL语句记录到日志文件,通过对这些特殊的SQL语句分析,改进以达到提高数据库性能的目的。
   通过使用--slow_query_log[={0|1}]选项来启用慢查询日志。所有执行时间超过long_query_time秒的SQL语句都会被记录到慢查询日志。
   缺省情况下hostname-slow.log为慢查询日志文件安名,存放到数据目录,同时缺省情况下未开启慢查询日志。
   缺省情况下数据库相关管理型SQL(比如OPTIMIZE TABLE、ANALYZE TABLE和ALTER TABLE)不会被记录到日志。
   对于管理型SQL可以通过--log-slow-admin-statements开启记录管理型慢SQL。
   mysqld在语句执行完并且所有锁释放后记入慢查询日志。记录顺序可以与执行顺序不相同。获得初使表锁定的时间不算作执行时间。
   
   可以使用mysqldumpslow命令获得日志中显示的查询摘要来处理慢查询日志。
   用查询缓存处理的查询不加到慢查询日志中,表有零行或一行而不能从索引中受益的查询也不写入慢查询日志。
   MySQL服务器按以下顺序记录SQL是否写入到慢查询日志
     a. The query must either not be an administrative statement, or --log-slow-adminstatements must have been specified.
     b. The query must have taken at least long_query_time seconds, or log_queries_not_using_indexes must be enabled and the query used no indexes for row lookups.
     c. The query must have examined at least min_examined_row_limit rows.
     d. The query must not be suppressed according to the log_throttle_queries_not_using_indexes setting.

 

3、慢查询日志演示

[sql] view plain copy print?

  1. long_query_time     :  设定慢查询的阀值,超出次设定值的SQL即被记录到慢查询日志,缺省值为10s  

  2. slow_query_log      :  指定是否开启慢查询日志  

  3. log_slow_queries    :  指定是否开启慢查询日志(该参数要被slow_query_log取代,做兼容性保留)  

  4. slow_query_log_file :  指定慢日志文件存放位置,可以为空,系统会给一个缺省的文件host_name-slow.log  

  5. min_examined_row_limit:查询检查返回少于该参数指定行的SQL不被记录到慢查询日志  

  6. log_queries_not_using_indexes: 不使用索引的慢查询日志是否记录到索引  

  7.       

  8. --当前版本  

  9. root@localhost[(none)]> show variables like 'version';  

  10. +---------------+------------+  

  11. | Variable_name | Value      |  

  12. +---------------+------------+  

  13. | version       | 5.5.39-log |  

  14. +---------------+------------+  

  15.   

  16. root@localhost[(none)]> show variables like '%slow%';  

  17. +---------------------+---------------------------------+  

  18. | Variable_name       | Value                           |  

  19. +---------------------+---------------------------------+  

  20. | log_slow_queries    | OFF                             |  

  21. | slow_launch_time    | 2                               |  

  22. | slow_query_log      | OFF                             |  

  23. | slow_query_log_file | /var/lib/mysql/suse11b-slow.log |  

  24. +---------------------+---------------------------------+  

  25.   

  26. root@localhost[tempdb]> set global log_slow_queries=1;  

  27. Query OK, 0 rows affected, 1 warning (0.00 sec)  

  28.   

  29. root@localhost[(none)]> show warnings;  

  30. +---------+------+-------------------------------------------------------------------------------------------------------------------+  

  31. Level   | Code | Message                                                                                                           |  

  32. +---------+------+-------------------------------------------------------------------------------------------------------------------+  

  33. | Warning | 1287 | '@@log_slow_queries' is deprecated and will be removed in a future release. Please use '@@slow_query_log' instead |  

  34. +---------+------+-------------------------------------------------------------------------------------------------------------------+  

  35.   

  36. --从下面的查询中可知,2个系统变量log_slow_queries,slow_query_log同时被置为on  

  37. root@localhost[(none)]> show variables like '%slow%';  

  38. +---------------------+---------------------------------+  

  39. | Variable_name       | Value                           |  

  40. +---------------------+---------------------------------+  

  41. | log_slow_queries    | ON                              |  

  42. | slow_launch_time    | 2                               |  

  43. | slow_query_log      | ON                              |  

  44. | slow_query_log_file | /var/lib/mysql/suse11b-slow.log |  

  45. +---------------------+---------------------------------+  

  46.   

  47. root@localhost[tempdb]> show variables like '%long_query_time%';  

  48. +-----------------+-----------+  

  49. | Variable_name   | Value     |  

  50. +-----------------+-----------+  

  51. | long_query_time | 10.000000 |  

  52. +-----------------+-----------+  

  53.   

  54. --为便于演示,我们将全局和session级别long_query_time设置为1  

  55. root@localhost[tempdb]> set global long_query_time=1;  

  56. Query OK, 0 rows affected (0.00 sec)  

  57.   

  58. root@localhost[tempdb]> set session long_query_time=1;  

  59. Query OK, 0 rows affected (0.00 sec)  

  60.   

  61. --Author : Leshami  

  62. --Blog   : http://blog.csdn.net/leshami  

  63.   

  64. root@localhost[tempdb]> create table tb_slow as select * from information_schema.columns;  

  65. Query OK, 829 rows affected (0.10 sec)  

  66. Records: 829  Duplicates: 0  Warnings: 0  

  67.   

  68. root@localhost[tempdb]> insert into tb_slow select * from tb_slow;  

  69. Query OK, 829 rows affected (0.05 sec)  

  70. Records: 829  Duplicates: 0  Warnings: 0  

  71.        .....为便于演示,我们插入一些数据,中间重复过程省略  

  72. root@localhost[tempdb]> insert into tb_slow select * from tb_slow;  

  73. Query OK, 26528 rows affected (4.40 sec)  

  74. Records: 26528  Duplicates: 0  Warnings: 0  

  75.   

  76. root@localhost[tempdb]> system tail  /var/lib/mysql/suse11b-slow.log  

  77. /usr/sbin/mysqld, Version: 5.5.39-log (MySQL Community Server (GPL)). started with:  

  78. Tcp port: 3306  Unix socket: /var/lib/mysql/mysql.sock  

  79. Time                 Id Command    Argument  

  80. Time: 141004 22:05:48  

  81. User@Host: root[root] @ localhost []  

  82. # Query_time: 4.396858  Lock_time: 0.000140 Rows_sent: 0  Rows_examined: 53056  

  83. use tempdb;  

  84. SET timestamp=1412431548;  

  85. insert into tb_slow select * from tb_slow;  

  86.   

  87.     ....再次插入一些记录....  

  88. root@localhost[tempdb]> insert into tb_slow select * from tb_slow;  

  89. Query OK, 212224 rows affected (37.51 sec)  

  90. Records: 212224  Duplicates: 0  Warnings: 0  

  91.   

  92. root@localhost[tempdb]> select table_schema,table_name,count(*) from tb_slow  

  93.     -> group by table_schema,table_name order by 3,2;  

  94. +--------------------+----------------------------------------------+----------+  

  95. | table_schema       | table_name                                   | count(*) |  

  96. +--------------------+----------------------------------------------+----------+  

  97. | information_schema | COLLATION_CHARACTER_SET_APPLICABILITY        |     1024 |  

  98. | performance_schema | cond_instances                               |     1024 |  

  99.                   ...........  

  100. | mysql              | user                                         |    21504 |  

  101. +--------------------+----------------------------------------------+----------+  

  102. 83 rows in set (1.58 sec)                    

  103.   

  104. root@localhost[tempdb]> system tail  /var/lib/mysql/suse11b-slow.log  

  105. User@Host: root[root] @ localhost []  

  106. # Query_time: 37.514172  Lock_time: 0.000123 Rows_sent: 0  Rows_examined: 424448  

  107. SET timestamp=1412431806;  

  108. insert into tb_slow select * from tb_slow;  

  109. Time: 141004 22:10:47  

  110. User@Host: root[root] @ localhost []  

  111. # Query_time: 1.573293  Lock_time: 0.000183 Rows_sent: 83  Rows_examined: 424614  

  112. SET timestamp=1412431847;  

  113. select table_schema,table_name,count(*) from tb_slow  --这条SQL被记录下来了,其查询时间为1.573293s  

  114. group by table_schema,table_name order by 3,2;  

  115.   

  116. root@localhost[tempdb]> show variables like '%log_queries_not_using_indexes';  

  117. +-------------------------------+-------+  

  118. | Variable_name                 | Value |  

  119. +-------------------------------+-------+  

  120. | log_queries_not_using_indexes | OFF   |  

  121. +-------------------------------+-------+  

  122.   

  123. root@localhost[tempdb]> set global log_queries_not_using_indexes=1;  

  124. Query OK, 0 rows affected (0.00 sec)  

  125.   

  126. --查看表tb_slow索引信息,表tb_slow无任何索引  

  127. root@localhost[tempdb]> show index from tb_slow;  

  128. Empty set (0.00 sec)  

  129.   

  130. root@localhost[tempdb]> select count(*) from tb_slow;  

  131. +----------+  

  132. count(*) |  

  133. +----------+  

  134. |   424448 |  

  135. +----------+  

  136. 1 row in set (0.20 sec)  

  137.   

  138. root@localhost[tempdb]> system tail -n3 /var/lib/mysql/suse11b-slow.log  

  139. # Query_time: 0.199840  Lock_time: 0.000152 Rows_sent: 1  Rows_examined: 424448  

  140. SET timestamp=1412432188;  

  141. select count(*) from tb_slow;   --此次查询时间为0.199840,被记录的原因是因为没有走索引,因为表本身没有索引  

4、格式化慢查询日志

[sql] view plain copy print?

  1. 结构化慢查询日志就是把慢查询日志中的重要信息按照便于阅读以及按照特定的排序方式来提取SQL。  

  2. 这种方式有点类似于Oracle中有个tkprof来格式化oracle的trace文件。  

  3. 对于前面的慢查询日志我们使用mysqldumpslow来提取如下:  

  4.   

  5. suse11b:~ # mysqldumpslow -s at,al /var/lib/mysql/suse11b-slow.log  

  6. Reading mysql slow query log from /var/lib/mysql/suse11b-slow.log  

  7. Count: 4  Time=16.87s (67s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost  

  8.   insert into tb_slow select * from tb_slow  

  9.   

  10. Count: 1  Time=0.20s (0s)  Lock=0.00s (0s)  Rows=1.0 (1), root[root]@localhost  

  11.   select count(*) from tb_slow  

  12.   

  13. Count: 1  Time=1.57s (1s)  Lock=0.00s (0s)  Rows=83.0 (83), root[root]@localhost  

  14.   select table_schema,table_name,count(*) from tb_slow  

  15.   group by table_schema,table_name order by N,N  

  16.   

  17. #以下是按照最大耗用时间排最后,只显示2条的方式格式化日志文件  

  18. suse11b:~ # mysqldumpslow -r -t 2 /var/lib/mysql/suse11b-slow.log  

  19. Reading mysql slow query log from /var/lib/mysql/suse11b-slow.log  

  20. Count: 1  Time=1.57s (1s)  Lock=0.00s (0s)  Rows=83.0 (83), root[root]@localhost  

  21.   select table_schema,table_name,count(*) from tb_slow  

  22.   group by table_schema,table_name order by N,N  

  23.   

  24. Count: 4  Time=16.87s (67s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost  

  25.   insert into tb_slow select * from tb_slow  

  26.     

  27. #获取mysqldumpslow的帮助信息  

  28. suse11b:~ # mysqldumpslow --help  

  29. Usage: mysqldumpslow [ OPTS... ] [ LOGS... ]  

  30.   

  31. Parse and summarize the MySQL slow query log. Options are  

  32.   

  33.   --verbose    verbose  

  34.   --debug      debug  

  35.   --help       write this text to standard output  

  36.   

  37.   -v           verbose  

  38.   -d           debug  

  39.   -s ORDER     what to sort by (al, at, ar, c, l, r, t), 'at' is default  

  40.                 al: average lock time  

  41.                 ar: average rows sent  

  42.                 at: average query time  

  43.                  c: count        #query的次数  

  44.                  l: lock time  

  45.                  r: rows sent    #返回的记录数  

  46.                  t: query time    

  47.   -r           reverse the sort order (largest last instead of first)  

  48.   -t NUM       just show the top n queries  

  49.   -a           don't abstract all numbers to N and strings to 'S'  

  50.   -n NUM       abstract numbers with at least n digits within names  

  51.   -g PATTERN   grep: only consider stmts that include this string  

  52.   -h HOSTNAME  hostname of db server for *-slow.log filename (can be wildcard),  

  53.                default is '*', i.e. match all  

  54.   -i NAME      name of server instance (if using mysql.server startup script)  

  55.   -l           don't subtract lock time from total time