mysql> show variables like "slow_query_log";
+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| slow_query_log | OFF | -- 默认为 OFF
+----------------+-------+
1 row in set (0.01 sec)
mysql> show variables like "slow_query_log_file";
+---------------------+---------------------------------------------------------------------------+
| Variable_name | Value |
+---------------------+---------------------------------------------------------------------------+
| slow_query_log_file | /home/soft/mysql/mysql-5.7.30-linux-glibc2.12-x86_64/mysql/host.log |
+---------------------+---------------------------------------------------------------------------+
1 row in set (0.01 sec)
mysql> set global slow_query_log_file = '/home/soft/mysql/mysql-5.7.30-linux-glibc2.12-x86_64/mysql/2022-12-06.log';
Query OK, 0 rows affected (0.00 sec)
-- 自己尝试修改文件,需要手动创建出文件,也可能是需要手动创建文件夹,不知道和权限是否有关
-- 记得给文件夹和文件赋权,修改用户组
报错信息
Access denied; you need (at least one of) the SUPER privilege(s) for this operation
原因是用户没有 PROCESS 权限
解决方案
mysql> grant process on *.* to archser_hywd;
Query OK, 0 rows affected (0.00 sec)
-- 我测试没效果,后面直接用 root 操作
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
在MySQL中,日志输出格式有支持:FILE(默认),TABLE 两种,可进行组合使用。如下所示:
mysql> set global log_output = "FILE,TABLE";
Query OK, 0 rows affected (0.00 sec)
这样设置会同时在 FILE, mysql 库中的 slow_log
表中同时写入。
在 MySQL 中,慢查询日志中默认不记录管理语句,如:
alter table, analyze table,check table
不过可通过以下属性进行设置:
mysql> set global log_slow_admin_statements = "ON";
Query OK, 0 rows affected (0.00 sec)
mysql> set global slow_query_log = "ON";
Query OK, 0 rows affected (0.00 sec)
mysql> set global slow_query_log = "OFF";
Query OK, 0 rows affected (0.00 sec)
在 MySQL 中,还可以设置将未走索引的SQL语句记录在慢日志查询文件中(默认为关闭状态)。通过下述属性即可进行设置:
mysql> set global log_queries_not_using_indexes = "ON";
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like "long_query_time";
+-----------------+----------+
| Variable_name | Value |
+-----------------+----------+
| long_query_time | 1.000000 |
+-----------------+----------+
1 row in set (0.00 sec)
mysql> set global long_query_time = 5;
Query OK, 0 rows affected (0.00 sec)
select sleep(11);
-- 使用这种方式可以让数据库记录当前语句,因为执行时间超过了设定的 5
SELECT db, query_time, lock_time, start_time, CAST(sql_text AS char) FROM mysql.slow_log
# Time: 200310 13:30:57
# User@Host: root[root] @ localhost [] Id: 21528
# Query_time: 6.000164 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0
SET timestamp=1583818257;
select sleep(6);
初看上面的慢查询日志,甚是有些陌生。不急,现在就来好好认识下:
采用表重命名、创建新表、删除旧表的方式清空日志信息
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> SET GLOBAL slow_query_log = 'OFF';
Query OK, 0 rows affected (0.00 sec)
mysql> ALTER TABLE slow_log RENAME slow_log_drop;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE slow_log LIKE slow_log_drop;
Query OK, 0 rows affected (0.01 sec)
mysql> DROP TABLE slow_log_drop;
Query OK, 0 rows affected (0.00 sec)
mysql>