CPU使用率过高
问题排查
1)通过top命令确认是否是mysqld进程占用过高cpu
# top
2)查看CPU飙高的mysql线程,
# top -H -p <mysqld进程id>
3)根据具体PID,定位问题SQL
SELECT a.THREAD_OS_ID,b.id,b.user,b.host,b.db,b.command,b.time,b.state,b.info
FROM performance_schema.threads a,information_schema.processlist b
WHERE b.id = a.processlist_id and a.THREAD_OS_ID=<具体pid>;
常见场景问题处理方案
SQL执行成本高,SQL运行时间长,大事务
show processlist:命令的输出结果显示了有哪些线程在运行,可以帮助识别出有问题的SQL语句
实时运行中的SQL里面超过10秒的按时间倒序列出,定位运行时间长SQL:
select * from information_schema.processlist where command != 'Sleep' and time >10 order by time desc;
这些运行时间长的SQL需要优化,比如适当建立某字段的索引。
线上环境,紧急时候,可以 kill 会话:
通过information_schema.processlist表中的连接信息生成需要处理掉的MySQL连接的语句临时文件,然后执行临时文件中生成的指令
select concat('KILL ',id,';') from information_schema.processlist where time>10 and db is not null and command!='sleep' into outfile '/tmp/a.txt';
跑批任务,并发高
联系应用人员,看这些会话都是在干啥的,问他们能不能杀或者停掉任务。
杀掉指定用户运行的连接,例如这里为usera
#mysqladmin -uroot -p processlist|awk -F "|" '{if($3 == "usera")print $2}'|xargs -n 1 mysqladmin -uroot -p kill