监控长事务的脚本
#!/bin/bash
mysql -N -uroot -p'密码' -e "select now(),(UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(a.trx_started)) diff_sec,b.id,b.user,b.host,b.db,d.SQL_TEXT from information_schema.innodb_trx a inner join
information_schema.PROCESSLIST b
on a.TRX_MYSQL_THREAD_ID=b.id and b.command = 'Sleep'
inner join performance_schema.threads c ON b.id = c.PROCESSLIST_ID
inner join performance_schema.events_statements_current d ON d.THREAD_ID = c.THREAD_ID;" | while read A B C D E F G H
do
#echo $C
if [ "$C" -gt 5 ]
then
echo date "+%Y-%m-%d %H:%M:%S"
echo "processid[$D] $E@$F in db[$G] hold transaction time $C SQL:$H"
fi
done >> /tmp/longtransaction.txt
简单说明一下,这里的-gt 5是5秒钟的意思,只要超过了5秒钟就认定是长事务,可以根据实际需要自定义。用法就是定义个定时任务中每分钟执行一次。