linux下开启mysql慢查询,分析查询语句

启用MySQL慢查询

一,到配置文件里面添加代码


  1. vim /etc/my.cnf
  2. [mysqld]
  3. slow-query-log = on # 开启慢查询功能
  4. slow_query_log_file = /usr/local/mysql/data/slow-query.log # 慢查询日志存放路径与名称
  5. long_query_time = 5 # 查询时间超过5s的查询语句
  6. ### log-queries-not-using-indexes = on # 列出没有使用索引的查询语句

二,,用命令开启慢查询


  1. mysql> show variables like "%long%"; //查看一下默认为慢查询的时间10秒
  2. +-----------------+-----------+
  3. | Variable_name | Value |
  4. +-----------------+-----------+
  5. | long_query_time | 10.000000 |
  6. +-----------------+-----------+
  7. 1 row in set (0.00 sec)
  8. mysql> set global long_query_time=2; //设置成2秒,加上global,下次进mysql已然生效 Query OK, 0 rows affected (0.00 sec)
  9. mysql> show variables like "%slow%"; //查看一下慢查询是不是已经开启
  10. +---------------------+---------------------------------+
  11. | Variable_name | Value |
  12. +---------------------+---------------------------------+
  13. | log_slow_queries | OFF |
  14. | slow_launch_time | 2 |
  15. | slow_query_log | OFF |
  16. | slow_query_log_file | /usr/local/mysql/mysql-slow.log |
  17. +---------------------+---------------------------------+
  18. 4 rows in set (0.00 sec)
  19. mysql> set slow_query_log='ON'; //加上global,不然会报错的。
  20. ERROR 1229 (HY000): Variable 'slow_query_log' is a GLOBAL variable and should be set with SET GLOBAL
  21. mysql> set global slow_query_log='ON'; //启用慢查询
  22. Query OK, 0 rows affected (0.28 sec)
  23. mysql> show variables like "%slow%"; //查看是否已经开启
  24. +---------------------+---------------------------------+
  25. | Variable_name | Value |
  26. +---------------------+---------------------------------+
  27. | log_slow_queries | ON |
  28. | slow_launch_time | 2 |
  29. | slow_query_log | ON |
  30. | slow_query_log_file | /usr/local/mysql/mysql-slow.log |
  31. +---------------------+---------------------------------+
  32. 4 rows in set (0.00 sec)

linux下开启mysql慢查询,分析查询语句

相关推荐