企业级使用shell是如何开发MySQL启动脚本的-创新互联
本文主要给大家介绍企业级使用shell是如何开发MySQL启动脚本的,文章内容都是笔者用心摘选和编辑的,具有一定的针对性,对大家的参考意义还是比较大的,下面跟笔者一起了解下企业级使用shell是如何开发MySQL启动脚本的吧。
创新互联公司是一家集网站建设,逊克企业网站建设,逊克品牌网站建设,网站定制,逊克网站建设报价,网络营销,网络优化,逊克网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。说明
MySQL启动命令为:
/bin/sh mysqld_safe --pid-file=$mysqld_pid_file_path 2>&1 > /dev/null &
停止命令为:
mysqld_pid=`cat "$mysqld_pid_file_path"`
if (kill -0 $mysqld_pid 2>/dev/null)
then
kill $mysqld_pid
sleep 2
fi
请完成MySQL启动脚本的编写
要求:用函数,case语句、if语句等实现。
解答:
[root@db02 scripts]# cat /etc/init.d/oldgirl
#!/bin/bash
# chkconfig: 2345 64 36
# description: MySQL startup
#Author:oldboy
#Blog:http://oldboy.blog.51cto.com
#Time:2017-07-07 09:24:34
#Name:mysqld.sh
#Version:V1.0
#Description:This is a test script.
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
Port=3306
User="root"
Bindir="/application/mysql/bin"
Datadir="/application/mysql/data"
mysqld_pid_file_path="/application/mysql/`hostname`.pid"
PATH="/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin"
export PATH
return_value=
# Lock directory.
lockdir='/var/lock/subsys'
lock_file_path="$lockdir/mysql"
log_success_msg(){
echo " SUCCESS! $@"
}
log_failure_msg(){
echo " ERROR! $@"
}
case "$1" in
start)
# Start daemon
echo "Starting MySQL"
if test -x $Bindir/mysqld_safe
then
$Bindir/mysqld_safe --datadir="$Datadir" --pid-file="$mysqld_pid_file_path" >/dev/null &
return_value=$?
sleep 2
# Make lock for CentOS
if test -w "$lockdir"
then
touch "$lock_file_path"
fi
exit $return_value
else
log_failure_msg "Couldn't find MySQL server ($bindir/mysqld_safe)"
fi
;;
stop)
if test -s "$mysqld_pid_file_path"
then
mysqld_pid=`cat "$mysqld_pid_file_path"`
if (kill -0 $mysqld_pid 2>/dev/null)
then
echo "Shutting down MySQL"
kill $mysqld_pid
return_value=$?
sleep 2
else
log_failure_msg "MySQL server process #$mysqld_pid is not running!"
rm -f "$mysqld_pid_file_path"
fi
# Delete lock for CentOS
if test -f "$lock_file_path"
then
rm -f "$lock_file_path"
fi
exit $return_value
else
log_failure_msg "MySQL server PID file could not be found!"
fi
;;
restart)
if $0 stop; then
$0 start
else
log_failure_msg "Failed to stop running server, so refusing to try to start."
exit 1
fi
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac
exit $return_value
函数版:
#!/bin/bash
# chkconfig: 2345 64 36
# description: MySQL startup
#Author:oldboy
#Blog:http://oldboy.blog.51cto.com
#Time:2017-07-07 09:24:34
#Name:mysqld.sh
#Version:V1.0
#Description:This is a test script.
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
port=3306
user="root"
bindir="/application/mysql/bin"
datadir="/application/mysql/data"
mysqld_pid_file_path="/application/mysql/`hostname`.pid"
PATH="/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin"
export PATH
return_value=
# Lock directory.
lockdir='/var/lock/subsys'
lock_file_path="$lockdir/mysql"
log_success_msg(){
echo " SUCCESS! $@"
}
log_failure_msg(){
echo " ERROR! $@"
}
start(){
# Start daemon
echo "Starting MySQL"
if test -x $bindir/mysqld_safe
then
$bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" >/dev/null &
return_value=$?
sleep 2
# Make lock for CentOS
if test -w "$lockdir"
then
touch "$lock_file_path"
fi
exit $return_value
else
log_failure_msg "Couldn't find MySQL server ($bindir/mysqld_safe)"
fi
}
stop(){
if test -s "$mysqld_pid_file_path"
then
mysqld_pid=`cat "$mysqld_pid_file_path"`
if (kill -0 $mysqld_pid 2>/dev/null)
then
echo "Shutting down MySQL"
kill $mysqld_pid
return_value=$?
sleep 2
else
log_failure_msg "MySQL server process #$mysqld_pid is not running!"
rm -f "$mysqld_pid_file_path"
fi
# Delete lock for CentOS
if test -f "$lock_file_path"
then
rm -f "$lock_file_path"
fi
exit $return_value
else
log_failure_msg "MySQL server PID file could not be found!"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
if $0 stop; then
$0 start
else
log_failure_msg "Failed to stop running server, so refusing to try to start."
exit 1
fi
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac
exit $return_value
看完以上关于企业级使用shell是如何开发MySQL启动脚本的,很多读者朋友肯定多少有一定的了解,如需获取更多的行业知识信息 ,可以持续关注我们的行业资讯栏目的。
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
当前题目:企业级使用shell是如何开发MySQL启动脚本的-创新互联
转载来源:http://ybzwz.com/article/dcpdph.html