shell指令碼:MySQL啟動簡易指令碼

來源:互聯網
上載者:User

shell指令碼:MySQL啟動簡易指令碼

shell指令碼編寫MySQL建議簡易啟動指令碼,練習使用變數,函數,case語句等知識;

[root@localhost baby]# cat mysql_function.sh
#!/bin/bash
. /etc/init.d/functions
path=/usr/local/mysql/bin/
function usage(){
 echo "$0 {start|stop|restart}"
 exit 1
}
[ $# -ne 1 ] && usage
 
function_mysql_start(){
    $path/mysqld_safe --user=mysql &>/dev/null &
 if [ $? -eq 0 ]
  then
      sleep 5
      action "start mysql" /bin/true
 else
      action "start mysql" /bin/false
 fi
}
 
function_mysql_stop(){
    $path/mysqladmin -uroot shutdown &>/dev/null
    if [ $? -eq 0 ]
      then
          action "stop mysql" /bin/true
    else
      action "stop mysql" /bin/false
    fi
}
 
function_mysql_restart(){
    function_mysql_stop
    sleep 3
    function_mysql_start
}
 
case $1 in
 start)
 function_mysql_start
 ;;
 stop)
 function_mysql_stop
 ;;
 restart)
 function_mysql_restart
 ;;
 *)
 printf "Usage:$0 {start|stop|restart}\n"
esac

執行結果如所示:

本文永久更新連結地址:

相關文章

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.