您好,欢迎来到创立网络工作室官方网站!ZJJCL.CN
新闻动态
您现在的位置:首页 > 技术文摘
监控mysql启动情况并检测表错误修复
来源:创立工作室 作者:冯军 发布时间:2024-07-23 11:50:07 阅读 623 次
#!/bin/env bash
###############################################################################
# NOTE:
# The test system is Ubuntu12.04
# This Scripts all rights reserved deserved by MickeyZZC
# Copyright  2013
#
###############################################################################
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
                echo "$(date +%Y%m%d%H%m),MYSQL NO UP" >> /var/log/mysqlstat.log
                exit 1
            fi
        sleep 10
    done
    if [[ $num -gt 0 ]] ; then
        mysqlsamchk
    fi
}
mysqlsamchk(){
    if [[ `which mysqlcheck` == "" ]] ;then
        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"|awk 'BEGIN{IFS='\t'}{print $3}'|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
    else
        mysqlcheck --all-databases --auto-repair -u$username -p$passwd |awk '!/OK/ {printf "datetime,%s\n",$1}'|sed "s/datetime/$(date +%Y%m%d%H%M)/g" >> /var/log/mysqlstat.log
#   day="$(date +%Y%m%d|cut -b 3-8) $(date +%H)"
#   grep "is marked as crashed and should be repaired" /var/log/mysql.err
    fi
}
 
if [[ $1 = 'check' ]] ; then
    mysqlsamchk
else
    mysqllive
fi