时间:2021-07-01 10:21:17 帮助过:18人阅读
开启慢查询日志,可以让MySQL记录下查询超过指定时间的语句,通过定位分析性能的瓶颈,才能更好的优化数据库系统的性能。
[root@localhost lib]# mysql –uroot
因为没有设置设置密码,有密码的在 mysql –uroot –p 接密码
mysql> show variables like ‘slow_query%‘;
+---------------------+--------------------------------------------+
| Variable_name | Value |
+---------------------+--------------------------------------------+
| slow_query_log | OFF |
| slow_query_log_file | /application/mysql/data/localhost-slow.log |
+---------------------+--------------------------------------------+
2 rows in set (0.00 sec)
参数说明:
slow_query_log 慢查询开启状态 OFF 未开启 ON 为开启
slow_query_log_file 慢查询日志存放的位置(这个目录需要MySQL的运行帐号的可写权限,一般设置为MySQL的数据存放目录)
mysql> show variables like ‘long%‘;
+-----------------+-----------+
| Variable_name | Value |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
1 row in set (0.00 sec)
long_query_time 查询超过多少秒才记录 默认10秒 修改为1秒
方法一:优点临时开启慢查询,不需要重启数据库 缺点:MySql 重启慢查询失效
推荐:根据业务需求,建议使用第二种,临时可以用第一种
默认情况下slow_query_log的值为OFF,表示慢查询日志是禁用的,可以通过设置slow_query_log的值来开启,如下所示::是否开启慢查询日志,1表示开启,0表示关闭。
mysql> show variables like ‘%slow_query_log%‘;
+---------------------+--------------------------------------------+
| Variable_name | Value |
+---------------------+--------------------------------------------+
| slow_query_log | OFF |
| slow_query_log_file | /application/mysql/data/localhost-slow.log |
+---------------------+--------------------------------------------+
2 rows in set (0.01 sec)
输入 语句修改(重启后失效,建议在/etc/my.cnf中修改永久生效)
mysql> set global slow_query_log=1;
Query OK, 0 rows affected (0.11 sec)
mysql> show variables like ‘%slow_query_log%‘;
+---------------------+--------------------------------------------+
| Variable_name | Value |
+---------------------+--------------------------------------------+
| slow_query_log | ON |
| slow_query_log_file | /application/mysql/data/localhost-slow.log |
+---------------------+--------------------------------------------+
2 rows in set (0.00 sec)
修改 MySql 慢查询,好多人不知道my.cnf 路径,可以用 find 查找
备注:我的MySQL 是编译的 路径为 /etc/my.cnf (一般都是这里)
[root@localhost log]# find / -type f -name "my.cnf"
/application/mysql-5.5.51/mysql-test/suite/rpl/my.cnf
/application/mysql-5.5.51/mysql-test/suite/federated/my.cnf
/application/mysql-5.5.51/mysql-5.5.51-linux2.6-x86_64/mysql-test/suite/rpl/my.cnf
/application/mysql-5.5.51/mysql-5.5.51-linux2.6-x86_64/mysql-test/suite/federated/my.cnf
/etc/my.cnf ###(一般都是这里)
[root@localhost log]# vim /etc/my.cnf
找到 [mysqld] 下面添加
slow_query_log =1
slow_query_log_file=/application/mysql/data/localhost-slow.log
long_query_time = 1
参数说明:
slow_query_log 慢查询开启状态 1 为开启
slow_query_log_file 慢查询日志存放的位置
long_query_time 查询超过多少秒才记录 默认10秒 修改为1秒
修改完重启MySQL
mysql> select sleep(2);
+----------+
| sleep(2) |
+----------+
| 0 |
+----------+
1 row in set (2.00 sec)
[root@localhost data]# cat /application/mysql/data/localhost-slow.log
/application/mysql/bin/mysqld, Version: 5.5.51-log (MySQL Community Server (GPL)). started with:
Tcp port: 3306 Unix socket: /tmp/mysql.sock
Time Id Command Argument
/application/mysql/bin/mysqld, Version: 5.5.51-log (MySQL Community Server (GPL)). started with:
Tcp port: 3306 Unix socket: /tmp/mysql.sock
Time Id Command Argument
/application/mysql/bin/mysqld, Version: 5.5.51-log (MySQL Community Server (GPL)). started with:
Tcp port: 3306 Unix socket: /tmp/mysql.sock
Time Id Command Argument
# Time: 170605 6:37:00
# User@Host: root[root] @ localhost []
# Query_time: 2.000835 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0
SET timestamp=1496615820;
select sleep(2);
mysql> show global status like ‘%Slow_queries%‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Slow_queries | 1 |
+---------------+-------+
1 row in set (0.00 sec)
在生产环境中,如果要手工分析日志,查找、分析SQL,显然是个体力活,MySQL提供了日志分析工具mysqldumpslow
MySQL 开启慢查询日志
标签:sql 表示 lob dump UI test 好的 sel host