查看MySQL数据库的SQL语句的执行记录日志

查看MySQL数据库的执行记录日志

SQL语句的执行记录日志

方法1

可以使用processlist查看SQL执行语句,但是有个弊端,就是只能查看正在执行的sql语句,无法查看历史执行的语句。

use information_schema;
show processlist;

或者

select * from information_schema.PROCESSLISTwhere info is not null;

方法2:开启数据库的日志模式

通过日志查看历史执行记录

  • 查看当前配置

show variables like '%log_output%';

show variables like '%general_log%';

  • 开启&关闭日志模式(可选择输出到表或文件中):
sql 复制代码
SET GLOBAL log_output = 'FILE';  SET GLOBAL general_log = 'ON';   //日志开启(日志输出到文件)

SET GLOBAL log_output = 'FILE';  SET GLOBAL general_log = 'OFF';  //日志关闭

或者

sql 复制代码
SET GLOBAL log_output = 'TABLE'; SET GLOBAL general_log = 'ON';   //日志开启(日志输出到表:mysql.general_log)

SET GLOBAL log_output = 'TABLE'; SET GLOBAL general_log = 'OFF';  //日志关闭
  • 查看日志文件中的执行记录:
    #tail -f /var/log/mariadb/mariadb.log

表查询执行记录:

sql 复制代码
SELECT * from mysql.general_log ORDER BY event_time DESC;
  • 清空表

truncate table mysql.general_log; //该表仅支持truncate不支持delete

相关推荐
golang学习记8 分钟前
rust开发,选VS Code还是RustRover?
开发语言·后端·rust
打工仔折腾 AI35 分钟前
从零写一个CAD 05:以鼠标为中心的滚轮缩放,招法能复用但顺序不能反
开发语言·后端·python·性能优化·计算机外设·swift·ai agent 实战
geovindu2 小时前
rust: Facade Pattern
开发语言·后端·设计模式·rust·外观模式
颜进强3 小时前
20 · NestJs循环依赖与 forwardRef:容器为什么在环面前会死,"占位再补齐"怎么救
前端·后端·ai编程
颜进强3 小时前
19 · NestJs @Global 落地账:装饰器与 `isGlobal` 参数,各在什么场景上岗
前端·后端·ai编程
她的男孩4 小时前
分片上传的大文件人人可下载:文件模块 isPrivate 在合并时被抹成 false,另有 4 个静默坑
java·后端·架构
颜进强4 小时前
18 · NestJS动态模块与 forRoot:`imports: [ConfigModule.forRoot({...})]` 到底在 import 什么
前端·后端·ai编程
xn71334 小时前
Personal AI Agent 架构实战:Memory、权限、跨 App 与本地/云端设计
人工智能·后端·agent
谢亮_vipxieliang4 小时前
Go defer、panic与recover核心知识点
开发语言·后端·golang
颜进强4 小时前
17 · 自定义 Provider 四形态:useValue / useClass / useFactory / useExisting 到底在选什么
前端·后端·ai编程