MySQL启动关闭系统脚本

start与stop

start关键的语句:

$bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null &

    # Start daemon

    # Safeguard (relative paths, core dumps..)
    cd $basedir

    echo $echo_n "Starting MySQL"
    if test -x $bindir/mysqld_safe
    then
      # Give extra arguments to mysqld with the my.cnf file. This script
      # may be overwritten at next upgrade.
      $bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null &
      wait_for_pid created "$!" "$mysqld_pid_file_path"; return_value=$?

      # Make lock for RedHat / SuSE
      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
    ;;

命令行实行情况:

/application/mysql/bin/mysqld_safe --user=mysql

[root@db01-51 ~]# netstat -lntup|grep 3306        ###先查看3306端口
tcp        0      0 :::3306                     :::*                        LISTEN      7023/mysqld       ###pid为7023  
[root@db01-51 ~]# cat /application/mysql/data/db01-51.pid
7023
[root@db01-51 ~]# kill 7023      ###杀死pid
[root@db01-51 ~]# netstat -lntup|grep 3306      ###找不到3306端口

[root@db01-51 ~]# /application/mysql/bin/mysqld_safe --user=mysql   ###mysqld_safe启动mysql
170316 09:18:07 mysqld_safe Logging to '/application/mysql-5.6.34/data/db01-51.err'.
170316 09:18:07 mysqld_safe Starting mysqld daemon with databases from /application/mysql-5.6.34/data
170316 09:19:50 mysqld_safe mysqld from pid file /application/mysql-5.6.34/data/db01-51.pid ended
[root@db01-51 ~]# netstat -lntup|grep 3306
tcp        0      0 :::3306                     :::*                        LISTEN      7150/mysqld         
[root@db01-51 ~]# cat /application/mysql/data/db01-51.pid
7150

stop关键的语句:

mysqld_pid=`cat "$mysqld_pid_file_path"`

kill -0 $mysqld_pid 2>/dev/null

# Stop daemon. We use a signal here to avoid having to know the
    # root password.

    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 $echo_n "Shutting down MySQL"
        kill $mysqld_pid
        # mysqld should remove the pid file when it exits, so wait for it.
        wait_for_pid removed "$mysqld_pid" "$mysqld_pid_file_path"; return_value=$?
      else
        log_failure_msg "MySQL server process #$mysqld_pid is not running!"
        rm "$mysqld_pid_file_path"
      fi

      # Delete lock for RedHat / SuSE
      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
    ;;

命令行实行的情况:

[root@db01-51 ~]# cat /application/mysql/data/db01-51.pid
7023
[root@db01-51 ~]# kill 7023

平滑杀死进程:

shutdown 平滑关闭。需要指定密码$mysql_pwd。

port=330*
mysql_user="root"
mysql_pwd=""
CmdPath="/application/mysql/bin"
mysql_sock="/data/${port}/mysql.sock"
start(){
    if [ ! -e "$mysql_sock" ];then
     	printf "Starting MySQL...\n"
        /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &
    else
        printf "MySQL is running...\n"
        exit 1
    fi
}
stop(){
    if [ ! -e "$mysql_sock" ];then
        printf "MySQL is stopped...\n"
        exit 1
    else
        printf "Stoping MySQL...\n"
        ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown
    fi
}

restart(){
    printf "Restarting MySQL...\n"
    stop
    sleep 2
    start
}

case "$1" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    restart)
        restart
    ;;
    *)
        printf "Usage: /data/${port}/mysql {start|stop|restart}\n"
esac

 

  

 

  

posted @ 2017-03-16 01:58  reborn枪  阅读(327)  评论(0)    收藏  举报