返回顶部

收藏

监控mysql启动情况并检测表错误修复

更多

给开发部同事用的测试服务器上MYSQL经常挂掉。。。都懒得理了。。写了个脚本自己检测错误修复表继续运行。。邮件报警功能等有空再补上去。。。

#!/bin/env bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
clear

username=
passwd=
mysqlhost=""

mysqllive(){
    num=0
    while [ `pidof mysqld` == "" ] ; do
        echo "$(date +%Y%m%d%H%m),MYSQL IS DOWN" >> /var/log/mysqlstat.log
        service mysql start
        num=`expr $num + 1`
            if [ $num -gt 11 ] ; then
                exit 1
            fi
        sleep 10
    done
    if [ $num -gt 0 ] ; then
        mysqlsamchk
    fi
}
mysqlsamchk(){
    mysqldata=`mysql -h$mysqlhost -u$username -p$passwd -e"show databases"|grep -vE "mysql|information_schema|performance_schema|Database"`
    for i in ${mysqldata[@]} ; do
        mytables=`mysql -h$mysqlhost -u$username -p$passwd -e"use $i;show tables;"|grep -vE "Tables_in_"`
        for j in ${mytables[@]} ; do
            table_status=`mysql -h$mysqlhost -u$username -p$passwd -e"check table $i.$j"|grep "error"`
            if [ ! "$table_status" == "" ] ; then
                mysql -h$mysqlhost -u$username -p$passwd -e"repair table $i.$j"
                echo "$(date +%Y%m%d%H%m),$i.$j be repair" >> /var/log/mysqlstat.log
            fi
        done

    done
#   day="$(date +%Y%m%d|cut -b 3-8) $(date +%H)"
#   grep "is marked as crashed and should be repaired" /var/log/mysql.err   
}

mysqllive
#该片段来自于http://outofmemory.cn

标签:shell,数据库

收藏

0人收藏

支持

0

反对

0

发表评论