这篇文章给大家分享的是重写启动脚本的方法,相信大部分人都还没学会这个技能,为了让大家学会,给大家总结了以下内容,话不多说,一起往下看吧。
user=rootport=3306CmdPath=/opt/mysql/binpassword=xxxxxxxxxxbase_dir=/opt/mysqlinst_dir=/data/3306alive="00"sMysqld=0sMysqldSafe=0fSock=0fPID=0check_alive(){echo "running check_alive..." if [ ps -ef | grep $port | grep -v grep | grep 'mysqld ' |awk '{print $2}' ];thenecho "Process mysqld is running!"sMysqld=1elseecho "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 ];then
echo "socked file exists!"
fSock=1
else
echo "socked file not exists!"
fSock=0
fi
if [ -e ${inst_dir}/mysql.pid ];then
echo "pid file exists!"
fPID=1
else
echo "pid file not exists!"
fPID=0
fi
if [ $sMysqld -eq 1 ];then
if [ $fPID -eq 0 ];then
echo "PID is missing, tring to recreate..."
ps -ef | grep $port | grep -v grep | grep 'mysqld ' |awk '{print $2}' > ${inst_dir}/mysql.pid
if [ -e ${inst_dir}/mysql.pid ]; then
echo "pid file is created!"
else
echo "can not create PID file, please have a check!"
fi
fi
if [ $sMysqldSafe -eq 0 ];then
echo "Process mysqld_safe is missing, please have a check! May cause mysqld crash!"
fi
if [ $fSock -eq 1 ];then
echo "MySQL running fine!!!"
alive="11"
else
echo "socket file is missing, you may have to login without socket!"
alive="10"
fi
else
echo "process mysqld is missing, mysql is stopped!"
if [ $fSock -eq 1 ] | [ $fPID -eq 1 ];then
echo "mysql was stopped abnormality!"
echo "cleaning socket file and pid file"
alive="01"
else
alive="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 &elseif [ $alive == "11" ]; thenecho "MySQL is runnig!"elseif [ $alive == "01" ]; thenecho "MySQL was stopped but pid or sock file still exits!"echo "Cleanning pid or sock files!"if [ $fSock -eq 1 ];thenrm -f ${inst_dir}/mysql.sockfiif [ $fPID -eq 1 ];thenrm -f ${inst_dir}/mysql.pidfiecho "Tring to start MySQL again!!!"${CmdPath}/mysqld_safe --defaults-file=${inst_dir}/my.cnf --user=${user} 2>&1 >/dev/null &fififi}stop_mysql(){check_aliveprintf "MySQL is Stopping...\n" if [ $alive == "11" ]; then${CmdPath}/mysqladmin -u root -p${password} -S ${inst_dir}/mysql.sock shutdown 2>&1 >/dev/null & elseif [ $alive == "10" ]; then${CmdPath}/mysqladmin -u root -p${password} -h $HOSTNAME -P $port --protocol tcp shutdown 2>&1 >/dev/null &elseecho "MySQL is not running, no need stop!" fi fi}restart_mysql(){stop_mysqlsleep 5if [ $? == 0 ];thenstart_mysqlelseecho "stop mysql failed!"fi}case $1 instart)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}
以上就是重写启动脚本的方法,代码示例简单明了,如果在日常工作遇到此问题。通过这篇文章,希望你能有所收获,更多详情敬请关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。