企业级通过shell脚本开发MySQL启动脚本 案例

企业Shell面试题10:开发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语句等实现。
解答:

[[email protected] 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=0

# Lock directory.
lockdir=‘/var/lock/subsys‘
lock_file_path="$lockdir/mysql"

log_success_msg(){ 
    echo " SUCCESS! [email protected]"
}   
log_failure_msg(){     
    echo " ERROR! [email protected]"
}

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=0

# Lock directory.
lockdir=‘/var/lock/subsys‘
lock_file_path="$lockdir/mysql"

log_success_msg(){ 
    echo " SUCCESS! [email protected]"
}   
log_failure_msg(){     
    echo " ERROR! [email protected]"
}

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

时间: 2024-10-13 22:20:22

企业级通过shell脚本开发MySQL启动脚本 案例的相关文章

开发rsync启动脚本2

使用函数更加规范的开发rsync启动脚本 #!/bin/bash #chkconfig: 2345 20 80 #description: create by vincen . /etc/init.d/functions function usage(){ echo $"usage:$0 {start|stop|restart}" exit 1 } function start(){ rsync --daemon sleep 1 if [ `netstat -lntup|grep rs

Mysql学习之--Mysql启动失败案例

Mysql学习之--Mysql启动失败案例 系统环境: 操作系统:Linux RH6 数据库:  Mysql 5.6 在启动mysql server时,启动失败 [[email protected] mysql]# service mysql start Starting MySQL...The server quit without updating PID file [FAILED]b/mysql/ogg.pid). 查看mysql错误日志: [[email protected] mysql

老鸟带你开发专业规范的MySQL启动脚本

每一个合格的Linux运维人员都应该做到熟练或精通Shell脚本编程,因为Shell脚本语言差不多是所有编程语言里最简单的语言,如果Shell脚本不行,意味着运维之路可能还没开始就将要终结.--老男孩老师 #!/bin/bash # chkconfig: 2345 64 36 #配置系统自启动 # description: A very fast and reliable SQL database engine. #########################################

多实例MySQL启动脚本

开发mysql多实例启动脚本: 已知mysql多实例启动命令为:mysqld_safe–defaults-file=/data/3306/my.cnf & 停止命令为:mysqladmin -u root -p123456 -S /data/3306/mysql.sock shutdown 要求:用函数,case语句.if语句等实现. #!/bin/sh [ -f /etc/init.d/functions ]&&. /etc/init.d/functions||exit #Def

MySQL启动脚本

今天在公司,上午搞了一个小时,MySQL单实例启动脚本搞定. [[email protected] scripts]# cat test3.sh  #!/bin/bash RETVAL=0 . /etc/init.d/functions basedir=/application/mysql-5.5.32/bin datadir=/application/mysql-5.5.32/data PSD=123.com   hostname=`hostname` menu(){ cat <<END =

mysql启动脚本详解

1 #!/bin/sh 2 # Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB 3 # This file is public domain and comes with NO WARRANTY of any kind 4 5 # MySQL daemon start/stop script. 6 7 # Usually this is put in /etc/init.d (at lea

Shell开发rsync启动脚本

需求:实现shell脚本对rsync的start|stop|restartrsync pid所在路径:/var/run/rsyncd.pidrsync启动命令:rsync --daemonrsync进程停止命令:pkill rsync 文中通过判断rsync pid所在路径和进程状态来实现rsync的start|stop|restart 1)脚本演示 [[email protected] scripts]# cat rsync.sh #!/bin/sh #This is the launch s

Linux的shell脚本LvsDR模式启动脚本和模拟keepalived高可用脚本

一.脚本运行前模拟环境 DR:server1:172.25.29.1 ipvsadm  keepalived RS:server2:172.25.29.2 arptables_jf   httpd RS:server3:172.25.29.3 arptables_jf   httpd DR:server4:172.25.29.4 ipvsadm  keepalived 使用脚本前先安装好server1.server2.server3的软件,做好httpd的后端访问页面 二.LVS_DR模式脚本介

用shell写一个服务启动脚本,速度get!

制作服务启动程序:(case+if)vi /etc/init.d/myprog #!/bin/bash#chkconfig:35 90 10#description:Startup script for myprog Servercase "$1" instart)if [ -f /opt/test ];thenecho "myprog服务已经启动 [失败]"elseecho "正在启动 myprog 服务 [确定]"touch /opt/tes