mysql脚本-mysql自动检测宕机重启并邮件通知

1、mysql检测运行状态:

### -ppassword  其中-p 代表密码参数  后面紧跟的是 password 代表数据库密码
mysqladmin -u root -ppassword ping

2、检测脚本:

#!/bin/bash

#  `  斜引号,代表 将命令行结果 返回值 传给 result 变量
 result=`mysqladmin -u root -pKing123456! ping`

# result=`/usr/bin/mysqladmin ping`

expected='mysqld is alive'

if [[ "$result" = "$expected" ]]

then

echo "mysqld is alive"

else

echo "It's dead - restart mysql"

# email subject

SUBJECT="[MYSQL ERROR] - Attempting to restart service"

# Email To ?

EMAIL="[email protected]"

# Email text/message

EMAILMESSAGE="/tmp/emailmessage.txt"

echo "$result was received"> $EMAILMESSAGE

echo "when we were expected $expected" >>$EMAILMESSAGE

# send an email using /bin/mail

mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE

sudo vncserver -kill :1

sudo systemctl restart mysqld

fi

3、将检测脚本放入定时器中:

### 编辑 用户 定时器文件
crontab -e
### 定义 每隔 半小时 检测 mysql 运行状态
*/30 * * * *  sh  /root/alarm/mysql-watch.sh

4、脚本文件中 发邮件功能解析

mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE

1、安装 sendmail 和 mailx
2、配置smtp文件
3、运行 脚本

参看sendmail配置smtp发邮件

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments