MySQL 多实例启动脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL 多实例启动脚本相关的知识,希望对你有一定的参考价值。

企业案例:开发mysql多实例启动脚本:
mysql多实例路径为:

[[email protected] ~]# 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多实例启动脚本展示:


[[email protected] ~]# 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

脚本测试:


[[email protected] ~]# /data/3306/mysql start
Mysql start successfully.                                  [  OK  ]
[[email protected] ~]# /data/3306/mysql stop
Mysql stop is successfully.                                [  OK  ]
[[email protected] ~]# /data/3306/mysql restart   #<==这里因为是在代码中定义了进程不存在就会提示
MyySQL is not run.                                         [FAILED]
[[email protected] ~]# /data/3306/mysql start
Mysql start successfully.                                  [  OK  ]
[[email protected] ~]# /data/3306/mysql restart
Mysql stop is successfully.                                [  OK  ]
Mysql start successfully.                                  [  OK  ]
Mysql restart is successfully.                             [  OK  ]

以上是关于MySQL 多实例启动脚本的主要内容,如果未能解决你的问题,请参考以下文章

多实例MySQL启动脚本

编写mysql多实例启动脚本

开发mysql单实例或多实例启动脚本

MySQL单实例或多实例启动脚本

Mysql多实例进入db报错ERROR 2002

MySQL5.7 多实例