mysql查看正在执行的sql与sql的历史记录

参考链接:https://www.cnblogs.com/jhin-wxy/p/8965888.html

经常会遇到此问题,每次都要百度,过于麻烦,遂记录。

一、查看正在执行的SQL

应用场景:通过用于查找执行时间巨长的SQL

select * from information_schema.`PROCESSLIST` where info is not null;

show processlist;

二、查看历史执行的SQL

应用场景:生产环境出现错误,临时看下执行了哪些sql

-- 日志开启

SET GLOBAL log_output = 'TABLE';SET GLOBAL general_log = 'ON';  
-- 查询
SELECT * from mysql.general_log ORDER BY event_time DESC;
-- 清空表
truncate table mysql.general_log;
-- 日志关闭
SET GLOBAL log_output = 'TABLE'; SET GLOBAL general_log = 'OFF';