mail工具发送mysql 慢查邮件

mail工具发送mysql 慢查邮件

开启慢日志查询
[root@controller scripts]# grep "slow" /etc/my.cnf
slow_query_log=ON
设置慢查询时间为0.1s
mysql> show variables  like '%long_query_time%';
+-----------------+----------+
| Variable_name   | Value    |
+-----------------+----------+
| long_query_time | 0.100000 |
+-----------------+----------+
1 row in set (0.01 sec)
编辑脚本
[root@controller scripts]# cat mail.sh 
#!/bin/bash
slowfile="/data/mysql/data/controller-slow.log"
tmpslow="/data/mysql/data/tmp.log"
# create tmp-slow
if [ ! -e ${tmpslow}  ];then
        touch ${tmpslow}
else
        >${tmpslow}
fi

# slow-file copy to tmp-slow
if [ -s ${slowfile} ];then
        cat ${slowfile}  > ${tmpslow} 

fi
# send mail
        user="root"
        pass=" "
        host="127.0.0.1"
info() {
        time_first=`mysql -u$user -p$pass -h$host -e "select now();" |awk 'END{print}'`
        time_second=`mysql -u$user -p$pass -h$host -e "select ADDTIME(sysdate(),'0:10:0');" |awk 'END{print}'`
        StartTime=`grep -E "Time" /data/mysql/data/controller-slow.log | awk '{print $3}'`
        DBname=`mysql -u$user -p$pass -h$host  -e "select database();"|awk 'END{print}' `
        SQLtext=`awk 'END{print}' /data/mysql/data/controller-slow.log`
        repeat=`awk '/Rows_examined/{print $NF}' /data/mysql/data/controller-slow.log`
        QueryTimes=` awk '/Query_time/{print $3}' /data/mysql/data/controller-slow.log`
        HostAdress=`awk -F "[: ]+" '/User@Host/{print $3,$4,$5}' /data/mysql/data/controller-slow.log`
        LockTime=`awk '/Lock_time/{print $5}' /data/mysql/data/controller-slow.log`
        echo "time_range: " $time_first "-" $time_second
        echo -e "\n"
        echo "StartTime: " $StartTime
        echo "DBname: "$DBname
        echo "SQLtext: "$SQLtext
        echo "repeat: "$repeat
        echo "QueryTimes: "$QueryTimes
        echo "HostAdress: "$HostAdress
        echo "LockTime: "$LockTime
}
# 调用函数且发送邮件
if [ -s ${tmpslow} ];then
        main(){
        info >/data/mysql/data/info.log
        echo "mysql slow log already sending!" | mail -s "mysql slow log" 1987631089@qq.com < /data/mysql/data/info.log
}
main
fi
if [ -s ${slowfile} ];then
        >${slowfile}
fi

安装mailx软件
# yum install mailx -y 
配置mailx
[root@controller scripts]# tail /etc/mail.rc 

set from=1987631089@qq.com

set smtp=smtp.qq.com                 

set smtp-auth-user=1987631089@qq.com

set smtp-auth-password=wdvoujxnowowbgid

set smtp-auth=login
邮件展示

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值