美文网首页Linux Bash
shell脚本监控数据库数据

shell脚本监控数据库数据

作者: 迷糊银儿 | 来源:发表于2018-04-20 15:02 被阅读21次

做测试时有会遇到线上回归不了的场景,只能等待真实用户触发了这个场景。那可以通过执行定时任务来轮询数据库,一旦有符合条件的数据就发邮件给对应的人员。

一、编辑定时任务

crontab -e
*/1 * * * * /Users/qfcomputer/Documents/workspace/LInuxTest/mysql.sh #每分钟执行1次这个脚本文件
保存

二、编辑监控数据库文件
\#!/bin/bash
source /Users/qfcomputer/.bash_profile
DBHOST="[127.0.0.1](127.0.0.1)"
DBPORT="3306"
DBUSER="root"
DBPW="qwe"
DBNAME="test"
sql0="select count(*) from user;"
sql1="select * from user"
sql2="select * from user where age<13"
sql3="insert into user(name,age,sex) values(1,1,'1')"
sql4="select count(*) from user where age=1"
function test()
{
 echo $1 $2
 result=`mysql -h"${DBHOST}" -P"${DBPORT}" -u"${DBUSER}" -p"${DBPW}" -D"$1" -e"$2"`
 if [ $result -ge '1' ]
 echo $result
 then echo "数据库监控" | mail -s "title can not be chinese" [12345@qq.com](mailto:qufang@qudian.com)
 count=$[ $count+1 ]
 fi
}
test "${DBNAME}" "${sql4}"

相关文章

网友评论

    本文标题:shell脚本监控数据库数据

    本文链接:https://www.haomeiwen.com/subject/uzaakftx.html