-- 开启查看所有查询日志,使用后立即关闭
SET GLOBAL general_log = 'ON';
SET GLOBAL general_log = 'OFF';
-- 查看慢查询日志路径
show variables like '%general_log%';
2.2 慢查询日志
2.2.1 开启日志
方式一:修改配置文件my.cnf,在[mysqld]段落中加入如上参数开启,需要重启MySQL
shell复制代码
# 开启慢查询日志
[mysqld]
slow_query_log = ON
log_output = 'FILE,TABLE'
long_query_time = 2
# 重启MySQL
service mysqld restart
方式二:通过全局变量设置,这种方式无需重启即可生效,但一旦重启,配置又会丢失
sql复制代码
# 开启慢查询日志
set global slow_query_log = 'ON';
# 修改多慢算慢查询的定义long_query_time,需要切换session才能生效
set global long_query_time = 2;
# 将慢查询日志同时记录到文件以及mysql.slow_log表中
set global log_output = 'FILE,TABLE';
2.2.1 查看与分析慢查询日志
sql复制代码
-- 查看TABLE中的记录
select * from mysql.slow_log;
-- 查看slow FILE文件,查看slow file路径,然后查看文本文件
show variables like '%slow_query_log_file%';
shell复制代码
# 分析慢查询日志文件工具:mysqldumpslow
mysqldumpslow -s r -t 10 -a /var/lib/mysql/node3-26-slow.log
# 分析慢查询日志文件工具:pt-query-digest
pt-query-digest mysql-slow-2022-01-07.log > 0107.report
-- 查看当前正在运行的进程列表
SHOW FULL PROCESSLIST;
SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST;
-- 按照客户端IP分组,看哪个客户端的连接数最多
select client_ip, count(client_ip) as client_num from (select substring_index ( host, ':', 1 ) as client_ip from information_schema.processlist) as connect_info group by client_ip order by client_num desc;
-- 查看正在执行的线程,并按time倒排序,看看有没有执行时间特别长的线程
select * from information_schema.processlist where command != 'sleep' order by Time desc limit 10\G
-- 找出所有执行时间超过5分钟的线程,拼凑出kill语句,方便后面查杀
select concat ('kill', id, ';') from information_schema.processlist where Command != 'Sleep' and Time > 300 order by Time desc;
-- 查看状态
SHOW STATUS;
show global status like '%slow%';
SHOW VARIABLES;
SHOW VARIABLES like '%%';
SHOW TABLE STATUS;
SHOW INDEX FROM EMPLOYEES;
SHOW ENGINE INNODB STATUS\G
SHOW MASTER STATUS;
SHOW SLAVE STATUS;