一、前言
通过配置打开mysql操作日志开关,监控追踪查看所有用户对于数据库的变更、查询及结构授权等所有操作日志记录。
二、操作步骤
1、查看日志开关是否打开
show variables where Variable_name = 'general_log';
或
show variables like 'general_log';
2. 打开或关闭开关(ON-打开;OFF为关闭)
set global general_log=on;@b@set global general_log=off;
3. 查看生成日志位置
show variables where Variable_name='general_log_file';
或
show variables like 'general_log_file';
更多命令,参考常用命令文章
三、日志示例说明
对mysql进行查询或删除测试,如下图查看日志文件进行浏览
DESKTOP-SIJ47KQ.log内容示例
show variables where Variable_name='general_log_file'@b@ 25 Query SHOW STATUS@b@ 25 Query SELECT QUERY_ID, SUM(DURATION) AS SUM_DURATION FROM INFORMATION_SCHEMA.PROFILING GROUP BY QUERY_ID@b@ 25 Query SELECT STATE AS 'Status', ROUND(SUM(DURATION),7) AS 'Duration', CONCAT(ROUND(SUM(DURATION)/0.000740*100,3), '%') AS 'Percentage' FROM INFORMATION_SCHEMA.PROFILING WHERE QUERY_ID=54 GROUP BY STATE@b@220624 10:50:31 27 Connect root@localhost on @b@ 27 Query SET NAMES utf8@b@ 27 Init DB demo@b@ 22 Query SHOW CREATE TABLE 'yyyy'@b@ 27 Query SELECT * FROM 'yyyy' LIMIT 0, 0@b@ 27 Query SELECT * FROM 'yyyy' LIMIT 0, 1000@b@ 27 Query SHOW COLUMNS FROM 'demo'.'yyyy'@b@220624 10:50:40 27 Query INSERT INTO 'yyyy' ('num', 'address') VALUES ('22', '2222')@b@ 27 Query SELECT * FROM 'yyyy' WHERE ('num'='22') AND ('address'='2222') LIMIT 1@b@220624 10:50:50 27 Query DELETE FROM 'yyyy' WHERE ('num'='11') AND ('address'='11111') LIMIT 1@b@220624 10:50:53 27 Query SELECT * FROM 'yyyy' LIMIT 0, 0@b@ 27 Query SELECT * FROM 'yyyy' LIMIT 0, 1000@b@ 27 Query SHOW COLUMNS FROM 'demo'.'yyyy'@b@220624 11:01:07 25 Query SET PROFILING=1@b@ 25 Query SHOW STATUS@b@ 25 Query SHOW STATUS@b@ 25 Query show variables where Variable_name = 'general_log'@b@ 25 Query #show variables like 'general_log';@b@@b@#set global general_log=on;