标签:配置 for password 免密 死循环 mail 客户 add 数据库
1、配置MYSQL本地免密登录账号密码会信息会记录到用户家目录下的".mylogin.cnf"文件中,若你想重新输入密码登录的话则删除此文件即可。
[root@localhost ~]# mysql_config_editor set --user=root --host=localhost --port=3306 --password
Enter password:
我们需要安装邮件客户端软件"Mailx",然后配置公司公共邮箱用于邮件发送。
[root@localhost ~]# yum -y install mailx
[root@localhost ~]# vi /etc/mail.rc
在文件尾部追加内容
set from=xxxxx@126.com
set smtp=smtp.126.com
set smtp-auth=login
set smtp-auth-user=xxxx@126.com
set smtp-auth-password=xxxxx
[root@localhost ~]# echo ‘hello world!‘ | mail -s ‘你好‘ xxxxx@qq.com
编写思路:
脚本内容如下:
while :
do
#配置部分
ADMIN_MAILS=xxxx@qq.com
#设置管理员邮箱,多个邮箱以空格间隔
LOG_PATH=/var/log/mysqlmonitor.log
#设置日志文件路径
MYSQL_PORT=3306
#设置MYSQL数据库端口
IP_DEVICE=ens160
#设置主机网卡设备名
HOST_IPADDR=`ifconfig $IP_DEVICE |awk ‘NR==2{print $2}‘`
LOCAL_DATE=`date +%Y-%m-%d\ \%H:%M:%S`
TMP_FILE_PROCESS=/tmp/mysqlprocess.tmp
TMP_FILE_MASTER_AND_SLAVE=/tmp/mysqlms.tmp
if [ ! -e $TMP_FILE_PROCESS ] || [ ! -e TMP_FILE_MASTER_AND_SLAVE ]
then
touch $TMP_FILE_PROCESS
touch $TMP_FILE_MASTER_AND_SLAVE
fi
MYSQLMONITOR_TMP_ONE=`cat $TMP_FILE_PROCESS |grep ‘MYSQL服务运行正常!‘|wc -l`
MYSQLMONITOR_TMP_TWO=`cat $TMP_FILE_PROCESS |grep ‘MYSQL服务未运行!‘|wc -l`
MYSQLMONITOR_TMP_THREE=`cat $TMP_FILE_MASTER_AND_SLAVE |grep ‘MYSQL主从复制工作异常!‘|wc -l`
MYSQLMONITOR_TMP_FOUR=`cat $TMP_FILE_MASTER_AND_SLAVE |grep ‘MYSQL主从复制工作正常!‘|wc -l`
MYSQL_ACTIVE=`netstat -lnupt |grep $MYSQL_PORT|wc -l`
if [ $MYSQL_ACTIVE != 0 ]
then
if [ $MYSQLMONITOR_TMP_ONE = 0 ]
then
echo "$LOCAL_DATE $HOST_IPADDR MYSQL服务运行正常!">>$LOG_PATH
echo ‘MYSQL服务运行正常!‘>$TMP_FILE_PROCESS
for i in $ADMIN_MAILS
do
echo -e "MYSQL服务已恢复正常运行!信息如下: \n发生时间:$LOCAL_DATE \n发生主机:$HOST_IPADDR" |mail -s "主机:$HOST_IPADDR MYSQL服务已恢复正常运行" $i
done
fi
Slave_IO_Running=`mysql -e "show slave status\G;" |grep "Slave_IO_Running:"|awk -F ‘:‘ ‘{print $2}‘`
Slave_SQL_Running=`mysql -e "show slave status\G;" |grep "Slave_SQL_Running:"|awk -F ‘:‘ ‘{print $2}‘`
Slave_SQL_Running_State=`mysql -e "show slave status\G;" |grep "Slave_SQL_Running_State:"|awk -F ‘:‘ ‘{print $2}‘`
if [ $Slave_IO_Running = No ] || [ $Slave_SQL_Running = No ]
then
if [ $MYSQLMONITOR_TMP_THREE = 0 ]
then
echo "$LOCAL_DATE $HOST_IPADDR MYSQL主从复制异常!">>$LOG_PATH
echo ‘MYSQL主从复制工作异常!‘>$TMP_FILE_MASTER_AND_SLAVE
for i in $ADMIN_MAILS
do
echo -e "MYSQL主从复制工作异常!请及时处理!信息如下:\nIO线程:$Slave_IO_Running \nSQL线程:$Slave_SQL_Running \n错误信息:$Slave_SQL_Running_State \n发生时间:$LOCAL_DATE \n发生主机:$HOST_IPADDR" |mail -s "主机:$HOST_IPADDR MYSQL主从复制工作异常" $i
done
fi
else
if [ $MYSQLMONITOR_TMP_FOUR = 0 ]
then
echo "$LOCAL_DATE $HOST_IPADDR MYSQL主从复制正常!">>$LOG_PATH
echo ‘MYSQL主从复制工作正常!‘>$TMP_FILE_MASTER_AND_SLAVE
for i in $ADMIN_MAILS
do
echo -e "MYSQL主从复制工作已恢复正常!信息如下:\nIO线程:$Slave_IO_Running \nSQL线程:$Slave_SQL_Running \n发生时间:$LOCAL_DATE \n发生主机:$HOST_IPADDR" |mail -s "主机:$HOST_IPADDR MYSQL主从复制工
作已恢复正常" $i
done
fi
fi
else
if [ $MYSQLMONITOR_TMP_TWO = 0 ]
then
echo "$LOCAL_DATE $HOST_IPADDR MYSQL服务未运行!">>$LOG_PATH
echo ‘MYSQL服务未运行!‘>$TMP_FILE_PROCESS
for i in $ADMIN_MAILS
do
echo -e "MYSQL服务未运行!请及时处理!信息如下: \n发生时间:$LOCAL_DATE \n发生主机:$HOST_IPADDR" |mail -s "主机:$HOST_IPADDR MYSQL服务未运行" $i
done
fi
fi
sleep 10;
done
作为守护进程运行。
[root@localhost ~]# nohup sh mysqlmonitor.sh >/dev/null &
标签:配置 for password 免密 死循环 mail 客户 add 数据库
原文地址:http://blog.51cto.com/10978134/2149268