• ADADADADAD

    重写启动脚本[ mysql数据库 ]

    mysql数据库 时间:2024-12-03 10:27:55

    作者:文/会员上传

    简介:

    !/bin/bashuser=rootport=3306CmdPath=/opt/mysql/binpassword=xxxxxxxxxxbase_dir=/opt/mysqlinst_dir=/data/3306alive="00"sMysqld=0sMysqldSafe=0fSock=0fPID=0check_al

    以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。

    !/bin/bash

    user=root
    port=3306
    CmdPath=/opt/mysql/bin
    password=xxxxxxxxxx

    base_dir=/opt/mysql
    inst_dir=/data/3306
    alive="00"
    sMysqld=0
    sMysqldSafe=0
    fSock=0
    fPID=0

    check_alive()
    {
    echo "running check_alive..."
    if [ ps -ef | grep $port | grep -v grep | grep 'mysqld ' |awk '{print $2}' ];then
    echo "Process mysqld is running!"
    sMysqld=1
    else
    echo "Process mysqld is not running!"
    sMysqld=0
    fi

     if [`ps -ef | grep 3306 | grep -v grep | grep 'mysqld_safe' |awk '{print $2}'` ];then echo "Process mysqld_safe is running!"sMysqldSafe=1 else echo "Process mysqld_safe is not running!"sMysqldSafe=0 fi if [ -e${inst_dir}/mysql.sock ];thenecho "socked file exists!"fSock=1 else echo "socked file not exists!"fSock=0 fi if [ -e${inst_dir}/mysql.pid ];thenecho "pid file exists!"fPID=1 else echo "pid filenot exists!"fPID=0 fi if [ $sMysqld -eq 1 ];thenif [ $fPID -eq 0 ];thenecho "PID is missing, tring to recreate..."ps -ef | grep $port | grep -v grep | grep 'mysqld ' |awk '{print $2}' > ${inst_dir}/mysql.pidif [ -e ${inst_dir}/mysql.pid ]; thenecho "pid file is created!"elseecho "can not create PID file, please have a check!"fifiif [ $sMysqldSafe -eq 0 ];thenecho "Process mysqld_safe is missing, please have a check! May cause mysqld crash!"fiif [ $fSock -eq 1 ];thenecho "MySQL running fine!!!"alive="11"elseecho "socket file is missing, you may have to login without socket!"alive="10"fi elseecho "process mysqld is missing, mysql is stopped!"if [ $fSock -eq 1 ] | [ $fPID -eq 1 ];thenecho "mysql was stopped abnormality!"echo "cleaning socket file and pid file"alive="01"elsealive="00"fi fi

    }

    start_mysql()
    {
    check_alive
    printf "MySQL is starting ... \n"
    if [ $alive == "00" ]; then
    ${CmdPath}/mysqld_safe --defaults-file=${inst_dir}/my.cnf --user=${user} 2>&1 >/dev/null &
    else
    if [$alive == "11" ]; then
    echo "MySQL is runnig!"
    else
    if [ $alive == "01" ]; then
    echo "MySQL was stopped but pid or sock file still exits!"
    echo "Cleanning pid or sock files!"
    if [ $fSock -eq 1 ];then
    rm -f ${inst_dir}/mysql.sock
    fi
    if [ $fPID -eq 1 ];then
    rm -f ${inst_dir}/mysql.pid
    fi
    echo "Tring to start MySQL again!!!"
    ${CmdPath}/mysqld_safe --defaults-file=${inst_dir}/my.cnf --user=${user} 2>&1 >/dev/null &
    fi
    fi
    fi
    }

    stop_mysql()
    {
    check_alive
    printf "MySQL is Stopping...\n"
    if [ $alive == "11" ]; then
    ${CmdPath}/mysqladmin -u root -p${password} -S ${inst_dir}/mysql.sock shutdown 2>&1 >/dev/null &
    else
    if [ $alive == "10" ]; then
    ${CmdPath}/mysqladmin -u root -p${password} -h $HOSTNAME -P $port --protocol tcp shutdown 2>&1 >/dev/null &
    else
    echo "MySQL is not running, no need stop!"
    fi
    fi
    }

    restart_mysql()
    {
    stop_mysql
    sleep 5
    if [ $? == 0 ];then
    start_mysql
    else
    echo "stop mysql failed!"
    fi
    }

    case $1 in

    start)
    start_mysql
    ;;
    stop)
    stop_mysql
    ;;
    restart)
    restart_mysql
    ;;
    status)
    status_mysql
    ;;
    alive)
    check_alive
    ;;
    *)
    printf "Please in put start|stop|restart|status as input"
    ;;
    esac

    重写启动脚本.docx

    将本文的Word文档下载到电脑

    推荐度:

    下载
    热门标签: 重写启动脚本