Menu
mysql在线保持脚本
一、mysql不定时宕机问题接口
1.写一脚本检测mysql状态,若宕机则重启
#!/bin/bash
pgrep -x mysqld &> /dev/null
if [ $? -ne 0 ]
then
echo "At time: `date` :MySQL is stop .">> /var/log/mysql_messages
source /etc/profile
service mysqld restart
else
echo "MySQL server is running ."
fi
 
2.加入定时任务,定时监听
*/5 * * * * /usr/local/shell-gmq-bash/mysql_monitor.sh >>/usr/local/shell-gmq-bash/log/log_status_$(date +%Y-%m-%d).log 2>&1
二、mysql不定时读取不到数据源(数据库文件存在但加载不到,所以找不到数据库)
1.编写脚本链接数据库读取该数据库,若该数据库不存在则重启并重新加载,脚本内容如下
STNAME="127.0.0.1"
PORT="3306"
USERNAME="root"
PASSWORD="xxxxx"
DBNAME="xxxxxxxxx"
TABLENAME="test"
use_db_sql="use ${DBNAME}"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} -e "${use_db_sql}"
if [ $? -eq 0 ];then
echo mysql wh_expo is ok
else
echo "At time: `date` :MySQL is stop .">> /var/log/mysql_dbcheck.log
source /etc/profile
service mysqld restart
fi
2.设置定时任务,定时检测并执行脚本
*/2 * * * * /usr/local/shell-gmq-bash/mysql_checkdb.sh >>/usr/local/shell-gmq-bash/log/log_dbcheck_$(date +%Y-%m-%d).log 2>&1