slow_query_log 慢查询开启状态,OFF:关闭;ON:开启
long_query_time 查询超过多少时间才记录(秒)
slow_query_log_file 慢查询日志存放目录,目录必须已经存在,且mysql有读写该目录的权限,否则会报错,如图:
![20200622185447085028.png](https://img7.php1.cn/3cdc5/cf6c/9f3/97eaba9adaf2f16a.jpeg)
2. 查看参数
mysql> show variables like ‘slow_query%‘;
![20200622185447265680.png](https://img7.php1.cn/3cdc5/cf6c/9f3/52e30e565f24578e.jpeg)
mysql> show variables like ‘long_query_time‘;
![20200622185447303764.png](https://img7.php1.cn/3cdc5/cf6c/9f3/7400e69e70597cc3.jpeg)
3. 1开启慢查询
3.1 方式一:全局变量设置(重启失效)
3.1.1 开启慢查询
mysql> set global slow_query_log=‘ON‘;
![20200622185447344777.png](https://img7.php1.cn/3cdc5/cf6c/9f3/9cc5840aff5172a6.jpeg)
3.1.2 设置慢查询日志存放位置
[root@qfx ~]# mkdir /opt/log/mysqlLog
![20200622185447383837.png](https://img7.php1.cn/3cdc5/cf6c/9f3/1bf0bfdeda3339a2.jpeg)
[root@qfx ~]# sudo chown mysql:mysql -R /opt/log/mysqlLog/
![20200622185447496134.png](https://img7.php1.cn/3cdc5/cf6c/9f3/aa7429cfb2fb39bf.jpeg)
mysql> set global long_query_time=5;
![20200622185447529335.png](https://img7.php1.cn/3cdc5/cf6c/9f3/ae5129bf434cdb92.jpeg)
3.1.3 重新连接客户端,查看设置后的参数
mysql> show variables like ‘slow_query%‘;
mysql> show variables like ‘long_query_time‘;
![20200622185447568395.png](https://img7.php1.cn/3cdc5/cf6c/9f3/9dbb90a52b2cafdd.jpeg)
3.2 方式二:配置文件
3.2.1 编辑配置文件MySql的my.cnf配置文件,添加以下内容:
[mysqld]
# 开启慢查询,OFF:关闭;ON:开启
slow_query_log = ON
# 设置慢查询日志存放位置
slow_query_log_file = /opt/log/mysqlLog/slow.log
# sql语句执行时间大于5秒的记录
long_query_time = 5
![20200622185447607455.png](https://img7.php1.cn/3cdc5/cf6c/9f3/c09f416bbb741c11.jpeg)
3.2.2 重启mysql
[root@qfx ~]# service mysqld restart
3.2.3 重新连接客户端,查看设置后的参数
mysql> show variables like ‘slow_query%‘;
mysql> show variables like ‘long_query_time‘;
![20200622185447568395.png](https://img7.php1.cn/3cdc5/cf6c/9f3/9dbb90a52b2cafdd.jpeg)