shell学习二十一--使用if编写mysql启动脚本

前端之家收集整理的这篇文章主要介绍了shell学习二十一--使用if编写mysql启动脚本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
例子:开发MysqL单实例或者多实例启动脚本 已知MysqL多实例启动命令为: MysqLd_safe --deafaults-file=/data/3306/my.cnf & 停止命令 MysqLadmin -u root -p123 -S /data/3306/MysqL.sock shutdown 请完成MysqL单实例或者多实例启动脚本编写。 要求:用函数、if语句等实现。 解答:单实例 1、启动:MysqL_safe --user=MysqL & 2、停止:MysqLadmin -u root -p123 shutdown [root@node01 day7]# MysqL -uroot -p123 Welcome to the MysqL monitor. Commands end with ; or \g. Your MysqL connection id is 2 Server version: 5.1.73 Source distribution Copyright (c) 2000,2013,Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MysqL> [root@node01 day7]# MysqLadmin -uroot -p123 shutdown [root@node01 day7]# ps -ef|grep MysqL root 6689 93648 0 02:18 pts/5 00:00:00 grep MysqL [root@node01 day7]# [root@node01 day7]# lsof -i :3306 实际脚本 [root@node01 day8]# cat start_db.sh #!/bin/sh function usage(){ echo "$0 {start|stop|restart}" exit 1 } [ $# -ne 1 ] && usage function start_MysqL(){ MysqLd_safe --user=MysqL & if [ $? -eq 0 ] then action "start MysqL" /bin/true else action "start MysqL" /bin/false fi } function stop_MysqL(){ MysqLadmin -u root -p123 shutdown if [ $? -eq 0 ] then action "stop MysqL" /bin/true else action "stop MysqL" /bin/false fi } if [ "$1" == "start" ] then start_MysqL elif [ "$1" == "stop" ] then stop_MysqL elif [ "$1" == "restart" ] then stop_MysqL start_MysqL else usage fi [root@node01 day8]# sh start_db.sh stop 170803 02:39:44 MysqLd_safe MysqLd from pid file /var/run/MysqLd/MysqLd.pid ended start_db.sh: line 23: action: command not found [1]+ Done MysqLd_safe --user=MysqL [root@node01 day8]# ps -ef|grep MysqL root 7017 93648 0 02:39 pts/5 00:00:00 grep MysqL [root@node01 day8]# lsof -i :3306 [root@node01 day8]# sh start_db.sh start start_db.sh: line 14: action: command not found [root@node01 day8]# 170803 02:40:02 MysqLd_safe Logging to '/var/log/MysqLd.log'. 170803 02:40:02 MysqLd_safe Starting MysqLd daemon with databases from /var/lib/MysqL [root@node01 day8]# [root@node01 day8]# [root@node01 day8]# [root@node01 day8]# [root@node01 day8]# [root@node01 day8]# lsof -i :3306 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME MysqLd 7107 MysqL 10u IPv4 962425 0t0 TCP *:MysqL (LISTEN) [root@node01 day8]# ps -ef|grep MysqL root 7022 1 0 02:40 pts/5 00:00:00 /bin/sh /usr/bin/MysqLd_safe --user=MysqL MysqL 7107 7022 0 02:40 pts/5 00:00:00 /usr/libexec/MysqLd --basedir=/usr --datadir=/var/lib/MysqL --user=MysqL --log-error=/var/log/MysqLd.log --pid-file=/var/run/MysqLd/MysqLd.pid --socket=/var/lib/MysqL/MysqL.sock root 7130 93648 0 02:40 pts/5 00:00:00 grep MysqL [root@node01 day8]# MysqL -uroot -p123 Welcome to the MysqL monitor. Commands end with ; or \g. Your MysqL connection id is 1 Server version: 5.1.73 Source distribution Copyright (c) 2000,Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MysqL> 带入路径(生产中带上路径) [root@node01 day8]# vi start_db.sh #!/bin/sh . /etc/init.d/functions path=/usr/bin/ function usage(){ echo "$0 {start|stop|restart}" exit 1 } [ $# -ne 1 ] && usage function start_MysqL(){ $path/MysqLd_safe --user=MysqL & if [ $? -eq 0 ] then action "start MysqL" /bin/true else action "start MysqL" /bin/false fi } function stop_MysqL(){ MysqLadmin -u root -p123 shutdown if [ $? -eq 0 ] then action "stop MysqL" /bin/true else action "stop MysqL" /bin/false fi } if [ "$1" == "start" ] then start_MysqL elif [ "$1" == "stop" ] then stop_MysqL elif [ "$1" == "restart" ] then stop_MysqL start_MysqL "start_db.sh" 43L,678C written You have new mail in /var/spool/mail/root [root@node01 day8]# sh start_db.sh stop 170803 02:44:23 MysqLd_safe MysqLd from pid file /var/run/MysqLd/MysqLd.pid ended stop MysqL [ OK ] [root@node01 day8]# ps -ef|grep MysqL root 7175 93648 0 02:44 pts/5 00:00:00 grep MysqL [root@node01 day8]# sh start_db.sh start start MysqL [ OK ] [root@node01 day8]# 170803 02:44:32 MysqLd_safe Logging to '/var/log/MysqLd.log'. 170803 02:44:32 MysqLd_safe Starting MysqLd daemon with databases from /var/lib/MysqL [root@node01 day8]# [root@node01 day8]# ps -ef|grep MysqL root 7179 1 0 02:44 pts/5 00:00:00 /bin/sh /usr/bin//MysqLd_safe --user=MysqL MysqL 7264 7179 0 02:44 pts/5 00:00:00 /usr/libexec/MysqLd --basedir=/usr --datadir=/var/lib/MysqL --user=MysqL --log-error=/var/log/MysqLd.log --pid-file=/var/run/MysqLd/MysqLd.pid --socket=/var/lib/MysqL/MysqL.sock root 7278 93648 0 02:44 pts/5 00:00:00 grep MysqL 优化脚本 [root@node01 day8]# vi start_db.sh sart 2 files to edit #!/bin/sh . /etc/init.d/functions path=/usr/bin/ pass=123 user=root function usage(){ echo "$0 {start|stop|restart}" exit 1 } [ $# -ne 1 ] && usage function start_MysqL(){ $path/MysqLd_safe --user=MysqL & >/dev/null 2>&1 if [ $? -eq 0 ] then action "start MysqL" /bin/true else action "start MysqL" /bin/false fi } function stop_MysqL(){ MysqLadmin -u$user -p$pass shutdown >/dev/null 2>&1 if [ $? -eq 0 ] then action "stop MysqL" /bin/true else action "stop MysqL" /bin/false fi } if [ "$1" == "start" ] then start_MysqL elif [ "$1" == "stop" ] then stop_MysqL elif [ "$1" == "restart" ] then stop_MysqL start_MysqL [root@node01 day8]# sh start_db.sh start start MysqL [ OK ] You have new mail in /var/spool/mail/root [root@node01 day8]# 170803 02:46:55 MysqLd_safe Logging to '/var/log/MysqLd.log'. 170803 02:46:55 MysqLd_safe Starting MysqLd daemon with databases from /var/lib/MysqL 去除启动时的log [root@node01 day8]# cat start_db.sh #!/bin/sh . /etc/init.d/functions path=/usr/bin/ pass=123 user=root function usage(){ echo "$0 {start|stop|restart}" exit 1 } [ $# -ne 1 ] && usage function start_MysqL(){ $path/MysqLd_safe --user=MysqL >/dev/null 2>&1 & if [ $? -eq 0 ] then action "start MysqL" /bin/true else action "start MysqL" /bin/false fi } function stop_MysqL(){ MysqLadmin -u$user -p$pass shutdown >/dev/null 2>&1 if [ $? -eq 0 ] then action "stop MysqL" /bin/true else action "stop MysqL" /bin/false fi } if [ "$1" == "start" ] then start_MysqL elif [ "$1" == "stop" ] then stop_MysqL elif [ "$1" == "restart" ] then stop_MysqL start_MysqL else usage fi 把脚本copy 到/etc/init.d/下 mv start_db.sh MysqLd chmod +x MysqLd 实现/etc/init.d/MysqL01 start启动,并且通过chkconfig设置开机自启动和关闭。 #!/bin/sh # chkconfig: 2345 21 60 (启动顺序不要和已有的冲突) # description: start MysqL and stop MysqL scripts. [root@node01 day8]# cp start_db.sh /etc/init.d/MysqL01 [root@node01 day8]# chkconfig --add MysqL01 [root@node01 day8]# chkconfig --list MysqL01 MysqL01 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@node01 day8]# ll /etc/rc.d/rc3.d/ |grep MysqL01 lrwxrwxrwx 1 root root 17 Aug 3 03:26 S21MysqL01 -> ../init.d/MysqL01 原文链接:https://www.f2er.com/bash/389647.html

猜你在找的Bash相关文章