企業級通過shell指令碼開發MySQL啟動指令碼 案例

來源:互聯網
上載者:User

標籤: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:650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />http://oldboy.blog.51cto.com
#Time:2017-07-07 09:24:34
#Name:650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />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:650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />http://oldboy.blog.51cto.com
#Time:2017-07-07 09:24:34
#Name:650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />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





企業級通過shell指令碼開發MySQL啟動指令碼 案例

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.