时间:2021-07-01 10:21:17 帮助过:4人阅读
mysql> show variables like ‘slow_query%‘; +---------------------------+----------------------------------+ | Variable_name | Value | +---------------------------+----------------------------------+ | slow_query_log | OFF | | slow_query_log_file | /mysql/data/localhost-slow.log | +---------------------------+----------------------------------+ mysql> show variables like ‘long_query_time‘; +-----------------+-----------+ | Variable_name | Value | +-----------------+-----------+ | long_query_time | 10.000000 | +-----------------+-----------+
2.设置方法
方法一:全局变量设置
将 slow_query_log 全局变量设置为“ON”状态
mysql> set global slow_query_log=‘ON‘;
设置慢查询日志存放的位置
mysql> set global slow_query_log_file=‘/usr/local/mysql/data/slow.log‘;
查询超过1秒就记录
mysql> set global long_query_time=1;
方法二:配置文件设置
修改配置文件my.cnf,在[mysqld]下的下方加入
[mysqld] slow_query_log = ON slow_query_log_file = /usr/local/mysql/data/slow.log long_query_time = 1
3.重启MySQL服务
service mysqld restart
4.查看设置后的参数
mysql> show variables like ‘slow_query%‘; +---------------------+--------------------------------+ | Variable_name | Value | +---------------------+--------------------------------+ | slow_query_log | ON | | slow_query_log_file | /usr/local/mysql/data/slow.log | +---------------------+--------------------------------+ mysql> show variables like ‘long_query_time‘; +-----------------+----------+ | Variable_name | Value | +-----------------+----------+ | long_query_time | 1.000000 | +-----------------+----------+
1.执行一条慢查询SQL语句
mysql> select sleep(2);
2.查看是否生成慢查询日志
ls /usr/local/mysql/data/slow.log
如果日志存在,MySQL开启慢查询设置成功!
MySQL慢查询【一】 - 开启慢查询
标签:var set 数据库 查询 select variables mysql value show