## 开启慢查询
mysql> set global slow_query_log = 'ON';
Query OK, 0 rows affected (0.78 sec)

## 检查变量值
mysql> show global variables like '%slow%';
+---------------------------+------------------------------------------------------+
| Variable_name             | Value                                                |
+---------------------------+------------------------------------------------------+
| log_slow_admin_statements | OFF                                                  |
| log_slow_slave_statements | OFF                                                  |
| slow_launch_time          | 2                                                    |
| slow_query_log            | ON                                                   |
| slow_query_log_file       | /home/username/mysql/data/db/logs/myql-slow.log      |
+---------------------------+------------------------------------------------------+
5 rows in set (0.00 sec)

## 记录不使用索引的语句
mysql> set global log_queries_not_using_indexes = 'ON';
Query OK, 0 rows affected (0.00 sec)

mysql> show global variables like '%indexes%';
+----------------------------------------+-------+
| Variable_name                          | Value |
+----------------------------------------+-------+
| log_queries_not_using_indexes          | ON    |
| log_throttle_queries_not_using_indexes | 0     |
+----------------------------------------+-------+
2 rows in set (0.00 sec)

## 设置慢查询日志的位置

mysql> set global slow_query_log_file ='/home/username/mysql/data/db/logs/myql-slow.log';
Query OK, 0 rows affected (0.01 sec)

mysql> show variables like '%slow%';
+---------------------------+------------------------------------------------------+
| Variable_name             | Value                                                |
+---------------------------+------------------------------------------------------+
| log_slow_admin_statements | OFF                                                  |
| log_slow_slave_statements | OFF                                                  |
| slow_launch_time          | 2                                                    |
| slow_query_log            | ON                                                   |
| slow_query_log_file       | /home/username/mysql/data/db/logs/myql-slow.log |
+---------------------------+------------------------------------------------------+
5 rows in set (0.00 sec)

## 按需要修改记录慢于该时间的语句
mysql> show variables like '%query_time%';
+-----------------+----------+
| Variable_name   | Value    |
+-----------------+----------+
| long_query_time | 1.000000 |
+-----------------+----------+
1 row in set (0.00 sec)

## 刷新即可生效

mysql> FLUSH LOGS;
Query OK, 0 rows affected (0.22 sec)

mysql>