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

启用MySQL慢查询

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

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

二,,用命令开启慢查询

mysql> show variables like "%long%";         //查看一下默认为慢查询的时间10秒  
+-----------------+-----------+  
| Variable_name   | Value     |  
+-----------------+-----------+  
| long_query_time | 10.000000 |  
+-----------------+-----------+  
1 row in set (0.00 sec)  
  
mysql> set global long_query_time=2;          //设置成2秒,加上global,下次进mysql已然生效  Query OK, 0 rows affected (0.00 sec)  
  mysql> show variables like "%slow%";          //查看一下慢查询是不是已经开启  
+---------------------+---------------------------------+  
| Variable_name       | Value                           |  
+---------------------+---------------------------------+  
| log_slow_queries    | OFF                             |  
| slow_launch_time    | 2                               |  
| slow_query_log      | OFF                             |  
| slow_query_log_file | /usr/local/mysql/mysql-slow.log |  
+---------------------+---------------------------------+  
4 rows in set (0.00 sec)  
  
mysql> set slow_query_log='ON';                        //加上global,不然会报错的。  
ERROR 1229 (HY000): Variable 'slow_query_log' is a GLOBAL variable and should be set with SET GLOBAL  
mysql> set global slow_query_log='ON';            //启用慢查询  
Query OK, 0 rows affected (0.28 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 | /usr/local/mysql/mysql-slow.log |  
+---------------------+---------------------------------+  
4 rows in set (0.00 sec)  


 

你可能感兴趣的:(数据库)