1. 程式人生 > >MySQL 多實例啟動腳本

MySQL 多實例啟動腳本

null .so 不存在 aso 啟動 cas min data script

企業案例:開發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

註:其實啟動腳本可以更簡化,當然作者本人展示的以謹慎的編寫來展示腳本


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  ]

MySQL 多實例啟動腳本