MySQL 多實例啓動腳本

企業案例:開發mysql多實例啓動腳本:
mysql多實例路徑爲:

[root@jason ~]# ls -ld /data/3306/   
drwxr-xr-x 3 mysql mysql 4096 Oct  9 13:28 /data/3306/

1)已知mysql多實例啓動命令爲:

mysql_safe --default-file=/data/3306/my.cnf &

2)停止命令爲:

mysqladmin -uroot -poldboy123 -S /data/3306/mysql.sock shutdown

注:其實啓動腳本可以更簡化,當然作者本人展示的是以謹慎的方式來編寫。


寫腳本思路:

定義一系列變量

fun_start(){
    如果mysql.sock存在那麼就提示mysql正在運行

    否則 啓動mysql進程,並且通過netstat命令查看進程號,通過返回值判斷是否等於0,如果等於0,那麼就提示啓動成功,如果不等於0就提示失敗
}

fun_stop(){
    如果mysql.sock不存在那麼就提示mysql未運行,然後退出
    否則 關閉mysql進程,並且通過netstat命令查看進程號,通過返回值判斷是否不等於0,如果不等於0,那麼就提示停止成功,如果等於0就提示失敗

}

fun_restart(){
        調用fun_stop函數 
        休息2s
        然後再調用fun_start函數
        並且通過netstat命令查看進程號,通過返回值判斷是否等於0,如果等於0,那麼就提示啓動成功,如果不等於0就提示失敗
}

MySQL多實例啓動腳本展示:


[root@jason ~]# cat /data/3306/mysql
#!/bin/sh
#kconfig:2345 13 15
#This is mysql start|stop|restart scripts.
#-------------------------------------
#Author:jason
#QQ:760966297
#mile:[email protected]
#-------------------------------------
[ -f /etc/init.d/functions ]&& . /etc/init.d/functions
Mysql_User=root
Mysql_Password=oldboy123
Mysql_Port=3306
Mysql_Path=/data/${Mysql_Port}
Mysql_Sock=/data/${Mysql_Port}/mysql.sock
Cmd_Path=/application/mysql/bin

fun_usage(){
        echo "USAGE $0:{start|stop|restart}"
                exit 1
}

fun_start(){
if [ -e $Mysql_Sock ];then 
        action "MySQL is running." /bin/false
   else 
        /bin/sh ${Cmd_Path}/mysqld_safe --defaults-file=$Mysql_Path/my.cnf  2>&1 >/dev/null & #Mysql start
        sleep 2
        netstat -lntup |grep 3306  >/dev/null  #acheck mysql process

   [ $? -eq 0 ]&&action "Mysql start successfully." /bin/true || action "Mysql startup failure." /bin/false
fi
}

fun_stop(){
if [ ! -e $Mysql_Sock  ];then 
        action "MyySQL is not run." /bin/false 
   exit 2
 else
        ${Cmd_Path}/mysqladmin -u${Mysql_User} -p${Mysql_Password} -S ${Mysql_Sock} shutdown
        netstat -lntup |grep 3306 >/dev/null
   [ $? -ne 0 ]&& action "Mysql stop is successfully." /bin/true || action "Mysql stop is failure." /bin/false  
fi
}

fun_restart(){
        fun_stop 
        sleep 2
        fun_start
        netstat -lntup |grep 3306  >/dev/null
         [ $? -eq 0 ]&& action "Mysql restart is successfully." /bin/true || action "Mysql restart is failure." /bin/false
exit 103
}

case $1 in
start)
        fun_start
        ;;
stop)
        fun_stop
        ;;
restart)
        fun_restart
        ;;
*)
          fun_usage
        ;;
esac

腳本測試:


[root@jason ~]# /data/3306/mysql start
Mysql start successfully.                                  [  OK  ]
[root@jason ~]# /data/3306/mysql stop
Mysql stop is successfully.                                [  OK  ]
[root@jason ~]# /data/3306/mysql restart   #<==這裏因爲是在代碼中定義了進程不存在就會提示
MyySQL is not run.                                         [FAILED]
[root@jason ~]# /data/3306/mysql start
Mysql start successfully.                                  [  OK  ]
[root@jason ~]# /data/3306/mysql restart
Mysql stop is successfully.                                [  OK  ]
Mysql start successfully.                                  [  OK  ]
Mysql restart is successfully.                             [  OK  ]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章