编写mysql多实例启动脚本

脚本原理:

 启动MySQL动作:

   mysqld_safe来执行启动

 停止MySQL动作:

   使用mysqladmin来执行停止动作

 重启的MySQL动作:

     原理就是先停止,然后再启动

   但是要注意:简单的sleep来等待停止和启动过程极可能出现在短时间内重复运行重启,mysqld_safe报错提示已经启动了一个实例的问题

   这里可以参考MySQL提供的默认启动脚本mysqld.service中的wait_for_pid()这个函数。

以下是启动脚本,效果如图:

脚本:

  1 #!/bin/sh
  2 #init
  3 port=3306
  4 mysql_user="root"
  5 mysql_pwd="test"
  6 CmdPath="/usr/local/mysql-5.5.62/bin/"
  7 mysql_sock="/data/${port}/mysqld.sock"
  8 pid_file="/data/${port}/mysqld.pid"
  9 service_startup_timeout=900
 10 PATH=/sbin:/usr/sbin:/bin:/usr/bin:$CmdPath:$PATH
 11 export PATH
 12 #print success
 13 echo_info_ok(){
 14     echo -e "[\e[0;32;1m success \e[0;32;0m]"
 15 }
 16
 17 #print faild
 18 echo_info_false(){
 19     echo -e "[\e[0;31;1m failed \e[0;31;0m]"
 20 }
 21 #wait for pid
 22 wait_for_pid(){
 23     verb="$1" # created | removed
 24     pid="$2"  # process ID of the program operating on the pid-file
 25     pid_file_path="$3" # path to the PID file.
 26     i=0
 27     flag="by checking again"
 28     while test $i -ne $service_startup_timeout;do
 29         case "$verb" in
 30               ‘created‘)
 31                 # wait for a PID-file to pop into existence.
 32                 test -s "$pid_file_path" && i=‘‘ && break
 33                 ;;
 34               ‘removed‘)
 35                 # wait for this PID-file to disappear
 36                 test ! -s "$pid_file_path" && i=‘‘ && break
 37                 ;;
 38               *)
 39                 echo "wait_for_pid () usage: wait_for_pid created|removed pid pid_file_path"
 40                 exit 1
 41                 ;;
 42         esac
 43
 44         # if server isn‘t running, then pid-file will never be updated
 45         if test -n "$pid"; then
 46               if kill -0 "$pid" 2>/dev/null; then
 47                 :  # the server still runs
 48              else
 49                 # The server may have exited between the last pid-file check and now.
 50                 if test -n "$avoid_race_condition"; then
 51                        flag=""
 52                        continue  # Check again.
 53                 fi
 54                 return 1  # not waiting any more.
 55             fi
 56         fi
 57         sleep 1
 58     done
 59 }
 60
 61 #startup function
 62 function_start_mysql(){
 63     if [ ! -e "$mysql_sock" ];then
 64       printf "Starting MySQL..."
 65       /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &
 66       wait_for_pid created "$!" "$pid_file"
 67       if [ $? -eq 0 ];then
 68           echo_info_ok
 69       else
 70           echo_info_false
 71       fi
 72     else
 73       printf "MySQL is running...\n"
 74       exit
 75     fi
 76 }
 77
 78 #stop function
 79 function_stop_mysql(){
 80     if [ ! -e "$mysql_sock" ];then
 81        printf "MySQL is stopped...\n"
 82        exit
 83     else
 84        printf "Stoping MySQL..."
 85         ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S ${mysql_sock} shutdown
 86         wait_for_pid removed "$!" "$pid_file"
 87         if [ $? -eq 0 ];then
 88             echo_info_ok
 89         else
 90             echo_info_false
 91         fi
 92    fi
 93 }
 94
 95 #restart function
 96 function_restart_mysql(){
 97     if [ ! -e $mysql_sock ];then
 98         printf "Restarting MySQL..\n"
 99         function_start_mysql
100     else
101         printf "Restarting MySQL...\n"
102         function_stop_mysql
103             if [ $? -eq 0 ];then
104                 function_start_mysql
105             else
106                 printf "Starting MySQL"
107                 echo_info_false
108             fi
109         done
110     fi
111 }
112
113 #main
114 case $1 in
115     start)
116         function_start_mysql
117     ;;
118     stop)
119         function_stop_mysql
120     ;;
121     restart)
122         function_restart_mysql
123     ;;
124     *)
125            printf "Usage: /data/${port}/mysql {start|stop|restart}\n"
126 esac

原文地址:https://www.cnblogs.com/meizy/p/mysql_start_code.html

时间: 2024-10-29 19:08:19

编写mysql多实例启动脚本的相关文章

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/mys

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

单实例 启动:mysqld_safe --user=mysql & 停止:mysqladmin -u root -proot shutdown 开发脚本 #!/bin/bash #chkconfig: 2345 30 50 #Date:2017-6-29 #Author:xcn([email protected]) #version Ultimates PID="/var/run/mysqld/mysqld.pid" user="root"       #定

Mysql基础的启动脚本

mysql安装目录(Window): 解析: 第一张图: 前面四个是mysql的驱动: 可以通过后缀知道我们用什么方式连接就需要安装什么驱动了:  例如:java   使用的是jdbc  需要连接conntor j 第五个和最后一个 主要是文档和案例: Workbench 是GUI: 现在市面上有很多GUI无所谓用什么   看个人爱好  我用的是sqlyog: mysqlservice:这里面是mysqlservice的核心部分: 第二张图(mysqlservice): my-default.i

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

1.       [[email protected] scripts]# cat mysqld01.sh 2.       #!/bin/bash 3. 4.       . /etc/init.d/functions 5.       user=root 6.       pass=888888 7.       path="/application/mysql/bin" 8.       function usage(){ 9.           echo "Usag

Redis多实例启动脚本

1,修改redis.conf 设置redis后台启动 daemonize yes 2,编写脚本 vi /etc/init.d/redis #!/bin/sh #chkconfig: 2345 10 90 #description: Startup and stop script for Redis PATH=/usr/local/bin:/sbin:/usr/bin:/bin REDISPORT_1=6380 REDISPORT_2=6381 REDISPORT_3=6382 REDISPORT

02.利用Shell开发Redis多实例启动脚本

一.Redis部署情况 ## 软件部署情况 [[email protected] ~]# ls -ld /data/apps/redis <==目录本身及以下所有文件的属主/组为redis lrwxrwxrwx 1 root root 24 Oct 26 11:33 /data/apps/redis -> /data/apps/redis-4.0.14/ ## 实例部署情况 [[email protected] ~]# tree /data/redis/ <==目录本身及以下所有文件的属

一个简单的mysql服务检测启动脚本

目的: 监测mysql的存活状态,一旦监测到mysql down掉,重新启动mysql. 脚本内容: /usr/local/mysql/bin/mysqladmin -uroot -psharpower ping > /dev/null 2>&1 if [ $? -ne 0 ] then         /etc/init.d/mysql.server restart >/dev/null         echo "`/bin/date '+%Y%m%d %H:%M:

mysql多实例安装脚本

#! /bin/bash # v.mysql-5.6.30-linux-glibc2.5-x86_64.tar.gz # only install master mysql # time:2016-08-15 # pkg dir pkg_dir=`pwd` # mysql base dir data_dir="/data/mysql_root/mysql" base_dir="/usr/local/mysql" # create MySQL group and us

Mysql判断是否启动脚本

#!/bin/bash #By:sadoc.blog.51cto.com . /etc/rc.d/init.d/functions MYUSER=root MYPASS="123456" MYSOCK=/var/lib/mysql/mysql.sock MY_START="systemctl start mysqld.service" LOG_PATH=/tmp/mysql MY_PATH=/usr/bin/mysql DAYS=`date +%F` SECOND=