mysql5.5慢日志设置和查询

mysql> showvariables like '%version%';

+-------------------------+---------------------+

| Variable_name           | Value               |

+-------------------------+---------------------+

|innodb_version          | 5.5.31              |

|protocol_version        | 10                  |

|slave_type_conversions  |                     |

| version                 | 5.5.31-log          |

|version_comment         | Sourcedistribution |

|version_compile_machine | x86_64             |

|version_compile_os      | Linux               |

+-------------------------+---------------------+

7 rows in set (0.00sec)

 

开启慢查询日志

mysql的配置文件my.cnf 中的mysqld下方添加以下参数:

log_slow_queries=ON
long_query_time=1
slow_launch_time=1
slow_query_log=ON
slow_query_log_file=/log/mysql/slow_queries.log

 

注:

long_query_time=1 表示记录查询时间超过1秒的sql

slow_query_log_file 慢日志文件

slow_launch_time : 表示如果建立线程花费了比这个值更长的时间,slow_launch_threads 计数器将增加

 

 

重启mysql后检查结果

mysql> showvariables like '%slow%';

+---------------------+-----------------------------+

| Variable_name       | Value                       |

+---------------------+-----------------------------+

|log_slow_queries    | ON                          |

|slow_launch_time    | 1                           |

|slow_query_log      | ON                          |

|slow_query_log_file | /log/mysql/slow_queries.log |

+---------------------+-----------------------------+

4 rows in set (0.00sec)

 

mysql> showvariables like '%long%';

+---------------------------------------------------+----------+

| Variable_name                                     |Value    |

+---------------------------------------------------+----------+

|long_query_time                                   | 1.000000 |

|max_long_data_size                                | 1048576  |

|performance_schema_events_waits_history_long_size | 10000    |

+---------------------------------------------------+----------+

3 rows in set (0.00sec)

 

 

测试

mysql> selectsleep(2);

+----------+

| sleep(2) |

+----------+

|        0 |

+----------+

1 row in set (2.00sec)

 

查看日志是否记录

[root@HE1 /]# tail-f /log/mysql/slow_queries.log

# Time: 16021722:26:54

# User@Host:root[root] @ localhost []

# Query_time:2.000661  Lock_time: 0.000000 Rows_sent:1  Rows_examined: 0

SETtimestamp=1455776814;

select sleep(2);

 

 

设置好参数后,以后可通过

mysql>set GLOBAL slow_query_log=1;开启慢日志
mysql> set GLOBAL slow_query_log=0;关闭慢日志


 

是否记录没用到索引的SQL语句

mysql> showvariables like '%using%';

+-------------------------------+-------+

| Variable_name                 | Value |

+-------------------------------+-------+

|log_queries_not_using_indexes | OFF   |

+-------------------------------+-------+

1 row in set (0.00sec)

 

mysql> set globallog_queries_not_using_indexes=1;

Query OK, 0 rowsaffected (0.00 sec)

 

mysql> showvariables like '%using%';

+-------------------------------+-------+

| Variable_name                 | Value |

+-------------------------------+-------+

|log_queries_not_using_indexes | ON    |

+-------------------------------+-------+

1 row in set (0.00sec)

 

通过mysqldumpslow工具查询慢日志

 

[root@HE1 bin]#mysqldumpslow  --help

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

 

Parse and summarizethe 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

 

 

 

 

[root@HE1 bin]#mysqldumpslow -s at -n 3 /log/mysql/slow_queries.log
 
Reading mysql slowquery log from /log/mysql/slow_queries.log
Count: 3  Time=2.33s (7s)  Lock=0.00s (0s)  Rows=1.0 (3), root[root]@localhost
  select sleep(N)
 
Count: 1  Time=0.37s (0s)  Lock=0.44s (0s)  Rows=0.0 (0), root[root]@localhost
  create table helei as select * frommysql.help_keyword
 
Count: 2  Time=0.11s (0s)  Lock=0.03s (0s)  Rows=0.0 (0), root[root]@localhost
  insert into helei  select * from mysql.help_keyword
 
Count: 2  Time=0.04s (0s)  Lock=0.00s (0s)  Rows=1.0 (2), root[root]@localhost
  select count(*) from helei

 

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值