数据库服务器CPU高

2023年 8月 15日 43.6k 0

使用此脚本有如下四个前提

1、当使用top 可以看到mysqld占用CPU高达百分之好几百

2、使用top -Hp `pidof mysqld`可以看到每个线程使用的CPU高达百分之90以上

3、并且在慢日志设置为1秒都抓不到慢SQL(因为有的时候SQL语句通过消耗资源来达到快速执行的目的)

4、可以通过如下脚本抓取精确的SQL语句

cat /tmp/sql.log就可以看到SQL,但是有的执行很快的确实也不好抓到。$9>95的95是需要更改的,表示的是线程使用CPU大于95的才会被抓取。

这个脚本大佬给改了个不落地的,但是跟不上思路还是自己写的明确。

cat /tmp/highcpu.sh
#!/bin/bash

pidstat -t -p `pidof mysqld` 1 1| grep -v 100.00|grep -v CPU |awk '$9>95{print $5,$9}' > /tmp/pid.log

COUN=`cat /tmp/pid.log | wc -l`

if [[ COUN -ne 0 ]];then
for i in `cat /tmp/pid.log |awk '{print $1}'`
do
date >> /tmp/sql.log
PID=`echo $i`
#PID=`cat /tmp/pid.log |awk '{print $1}'|head -n 1`
sed -i "s/OSPID/${PID}/g" /tmp/highcpu.sql
mysql -uroot -pmima -e "source /tmp/highcpu.sql" >> /tmp/sql.log
cat /tmp/highcpubak.sql > /tmp/highcpu.sql
done
fi

cat /tmp/highcpu.sql
select id, db, user, host, command, time, state, info from information_schema.processlist where id = (select PROCESSLIST_ID from performance_schema.threads where THREAD_OS_ID = OSPID)\G;

cat /tmp/highcpubak.sql
select id, db, user, host, command, time, state, info from information_schema.processlist where id = (select PROCESSLIST_ID from performance_schema.threads where THREAD_OS_ID = OSPID)\G;

相关文章

Oracle如何使用授予和撤销权限的语法和示例
Awesome Project: 探索 MatrixOrigin 云原生分布式数据库
下载丨66页PDF,云和恩墨技术通讯(2024年7月刊)
社区版oceanbase安装
Oracle 导出CSV工具-sqluldr2
ETL数据集成丨快速将MySQL数据迁移至Doris数据库

发布评论