跳转至

63. 脚本:MySQL服务启停

11.开发MySQL服务启停脚本(与rsync启停脚本类似)

lsof -i :3306

思路分析

1.实现效果
/et/init.d/mysqld {start|stop|restart}
2.启动
mysqld_safe --user=mysql &
3.停止
killall,pkill
kill pid

脚本

#!/bin/bash
lockfile=/var/lock/subsys/mysqld
. /etc/init.d/functions
mysqld_pid_file_path="/appliacation/mysql/data/`uname -n`.pid"
mysqld_safe=/application/mysql/bin/mysqld_safe

start(){
    /bin/sh $mysqld_safe --datadir=/application/mysql/data --pid-file=$mysqld_pid_file_path &>/dev/null &
    retval=$?
    if [ $retval -eq 0 ];then
        action "mysql startup ok" /bin/true
        touch $lockfile
        return $retval
    else
        action "mysql startup fail" /bin/false
        return $retval
    fi
}

stop(){
    if test -s "$mysqld_pid_file_path"
    then
        mysqld_pid=`cat $mysqld_pid_file_path`
        if (kill -0 $mysqld_pid &>/dev/null)
        then
            kill $mysqld_pid
            retval=$?
            if [ $retval -eq 0 ];then
                action "mysql stop ok" /bin/true
                rm $lockfile
                return $retval
            else
                action "mysql stop fail" /bin/false
                return $retval
            fi
        else
            echo "mysqld process is not exist."
            return 2
        fi
    else
        echo "$mysqld_pid_file_path is not exist,or mysqld does not startup."
    fi
}

case "$1" in
    start)
        start
        retval=$?
        ;;
    stop)
        stop
        retval=$?
        ;;
    restart)
        stop
        sleep2
        start
        retval=$?
        ;;
    *)
        echo "usage: $0 {start|stop|restart}"
        exit 1
esac
exit $retval

最后更新: 2022-02-19 13:59:07