精华内容
下载资源
问答
  • Linux Mysql启动脚本

    2019-07-03 14:27:02
    Linux Mysql启动脚本 #/bin/bash #init Port=3306 MysqlUser="root" MysqlPass="123456" CmdPath="/application/mysql/bin" #startup fnction start() { if [ `netstat -lnt|grep "$Port"|wc -l` -eq 0 ] then ...

    Linux Mysql启动脚本

    #/bin/bash
    #init
    Port=3306
    MysqlUser="root"
    MysqlPass="123456"
    CmdPath="/application/mysql/bin"
    
    #startup fnction
    start()
    {
    	if [ `netstat -lnt|grep "$Port"|wc -l` -eq 0 ]
    		then
    			printf "Starting Mysql...\n"
    			/bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${Port}/my.conf2>&1 > /dev/null &
    	else
    		printf "Mysql is running...\n"
    	fi
    }
    
    #stop function
    stop()
    {
    	if [ ! `netstat -lnt|grep "$Port"|wc` -l -eq 0]
    		then
    			printf "Stopping Mysql...\n"
    			${CmdPath}/mysqladmin -u ${MysqlUser} -p${MysqlPass} -s /data/${Port}/mysql.sock shutdown
    	else
    		printf "Mysql is stopped...\n"
    	fi
    }
    
    #restart funcation
    restart()
    {
    	printf "Restarting Mysql...\n"
    	stop
    	sleep 2
    	start
    }
    
    case "$1" in
    start)
    	start
    	;;
    stop)
    	stop
    	;;
    restart)
    	restart
    	;;
    *)
    	printf "Usage: $0 {start|stop|restart}\n"
    esac
    
    展开全文
  • mysql启动脚本详解

    千次阅读 2016-06-06 12:00:44
    mysql启动脚本详解 1 #!/bin/sh 2 # Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB 3 # This file is public domain and comes with NO WARRANTY of any kind 4 5 # MySQL ...

    mysql启动脚本详解

      1 #!/bin/sh
      2 # Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB
      3 # This file is public domain and comes with NO WARRANTY of any kind
      4 
      5 # MySQL daemon start/stop script.
      6 
      7 # Usually this is put in /etc/init.d (at least on machines SYSV R4 based
      8 # systems) and linked to /etc/rc3.d/S99mysql and /etc/rc0.d/K01mysql.
      9 # When this is done the mysql server will be started when the machine is
     10 # started and shut down when the systems goes down.
     11 
     12 # Comments to support chkconfig on RedHat Linux
     13 # chkconfig: 2345 64 36
     14 # description: A very fast and reliable SQL database engine.
     15 
     16 # Comments to support LSB init script conventions
     17 ### BEGIN INIT INFO
     18 # Provides: mysql
     19 # Required-Start: $local_fs $network $remote_fs
     20 # Should-Start: ypbind nscd ldap ntpd xntpd
     21 # Required-Stop: $local_fs $network $remote_fs
     22 # Default-Start:  2 3 4 5
     23 # Default-Stop: 0 1 6
     24 # Short-Description: start and stop MySQL
     25 # Description: MySQL is a very fast and reliable SQL database engine.
     26 ### END INIT INFO
     27  
     28 # If you install MySQL on some other places than /usr, then you
     29 # have to do one of the following things for this script to work:
     30 #
     31 # - Run this script from within the MySQL installation directory
     32 # - Create a /etc/my.cnf file with the following information:
     33 #   [mysqld]
     34 #   basedir=<path-to-mysql-installation-directory>
     35 # - Add the above to any other configuration file (for example ~/.my.ini)
     36 #   and copy my_print_defaults to /usr/bin
     37 # - Add the path to the mysql-installation-directory to the basedir variable
     38 #   below.
     39 #
     40 # If you want to affect other MySQL variables, you should make your changes
     41 # in the /etc/my.cnf, ~/.my.cnf or other MySQL configuration files.
     42 
     43 # If you change base dir, you must also change datadir. These may get
     44 # overwritten by settings in the MySQL configuration files.
     45 
     46 basedir=            #mysql的安装目录
     47 datadir=            #数据存储路径
     48 
     49 # Default value, in seconds, afterwhich the script should timeout waiting
     50 # for server start. 
     51 # Value here is overriden by value in my.cnf. 
     52 # 0 means don‘t wait at all
     53 # Negative numbers mean to wait indefinitely
     54 service_startup_timeout=900
     55 
     56 # Lock directory for RedHat / SuSE.
     57 lockdir=‘/var/lock/subsys‘                #mysql进程锁
     58 lock_file_path="$lockdir/mysql"
     59 
     60 # The following variables are only set for letting mysql.server find things.
     61 
     62 # Set some defaults
     63 mysqld_pid_file_path=
     64 if test -z "$basedir"                     #依次确定$basedir,$datadir,$bindir等路径
     65 then
     66   basedir=/usr
     67   bindir=/usr/bin
     68   if test -z "$datadir"   
     69   then
     70     datadir=/var/lib/mysql
     71   fi
     72   sbindir=/usr/sbin
     73   libexecdir=/usr/sbin
     74 else
     75   bindir="$basedir/bin"
     76   if test -z "$datadir"
     77   then
     78     datadir="$basedir/data"
     79   fi
     80   sbindir="$basedir/sbin"
     81   libexecdir="$basedir/libexec"
     82 fi
     83 
     84 # datadir_set is used to determine if datadir was set (and so should be
     85 # *not* set inside of the --basedir= handler.)
     86 datadir_set=
     87 
     88 #
     89 # Use LSB init script functions for printing messages, if possible
     90 #
     91 lsb_functions="/lib/lsb/init-functions"   #linux系统脚本,包含打印log函数
     92 if test -f $lsb_functions ; then    #如果存在改文件,将这些函数导入该脚本
     93   . $lsb_functions
     94 else                              #否则使用自定义信息
     95   log_success_msg()
     96   {
     97     echo " SUCCESS! $@"
     98   }
     99   log_failure_msg()
    100   {
    101     echo " ERROR! $@"
    102   }
    103 fi
    104 
    105 PATH="/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin"           #添加$basrdir到系统环境变量
    106 export PATH
    107 
    108 mode=$1    # start or Stop        #传入启动脚本的第1个参数
    109 
    110 [ $# -ge 1 ] && shift      #如果参数个数大于等于1个,shift删掉第一个参数,将剩余参数传给$other_args
    111 
    112 
    113 other_args="$*"   # uncommon, but needed when called from an RPM upgrade action
    114            # Expected: "--skip-networking --skip-grant-tables"
    115            # They are not checked here, intentionally, as it is the resposibility
    116            # of the "spec" file author to give correct arguments only.
    117 
    118 case `echo "testing\c"`,`echo -n testing`     #echo_c和echo_n的定义;\c不加换行符,-n不换行
    119     *c*,-n*) echo_n=   echo_c=     ;;
    120     *c*,*)   echo_n=-n echo_c=     ;;
    121     *)       echo_n=   echo_c=‘\c‘ ;;
    122 esac
    123 
    124 
    125 parse_server_arguments() {                 #该函数将my_print_defaults 命令分析出得参数赋值给相应的变量
    126   for arg do 
    127     case "$arg" in
    128       --basedir=*)  basedir=`echo "$arg" | sed -e ‘s/^[^=]*=//‘`    #利用正则表达式获取
    129                     bindir="$basedir/bin"
    130             if test -z "$datadir_set"; then  
    131               datadir="$basedir/data"
    132             fi
    133             sbindir="$basedir/sbin"
    134             libexecdir="$basedir/libexec"
    135         ;;
    136       --datadir=*)  datadir=`echo "$arg" | sed -e ‘s/^[^=]*=//‘`
    137             datadir_set=1
    138     ;;
    139       --pid-file=*) mysqld_pid_file_path=`echo "$arg" | sed -e ‘s/^[^=]*=//‘` ;;
    140       --service-startup-timeout=*) service_startup_timeout=`echo "$arg" | sed -e ‘s/^[^=]*=//‘` ;;
    141     esac
    142   done
    143 }
    144 
    145 
    146 
    147 wait_for_pid () {                        #该函数在启动和停止mysql服务时用于等待pid的生成
    148   verb="$1"           # created | removed
    149   pid="$2"            # process ID of the program operating on the pid-file
    150   pid_file_path="$3" # path to the PID file.
    151 
    152   i=0
    153   avoid_race_condition="by checking again"
    154 
    155   while test $i -ne $service_startup_timeout ; do    
    156 
    157     case "$verb" in                                  #处理第1个参数,created或removed
    158       ‘created‘)
    159         # wait for a PID-file to pop into existence.
    160         test -s "$pid_file_path" && i=‘‘ && break
    161         ;;
    162       ‘removed‘)
    163         # wait for this PID-file to disappear
    164         test ! -s "$pid_file_path" && i=‘‘ && break
    165         ;;
    166       *)
    167         echo "wait_for_pid () usage: wait_for_pid created|removed pid pid_file_path"
    168         exit 1
    169         ;;
    170     esac
    171 
    172     # if server isn‘t running, then pid-file will never be updated
    173     if test -n "$pid"; then                 #如果pid非空
    174       if kill -0 "$pid" 2>/dev/null; then   #kill -0发送一个无效信号,可检查进程是否存在,存在返回0,反之则1
    175         :  # the server still runs
    176       else
    177         # The server may have exited between the last pid-file check and now.  
    178         if test -n "$avoid_race_condition"; then
    179           avoid_race_condition=""
    180           continue  # Check again.
    181         fi
    182 
    183         # there‘s nothing that will affect the file.
    184         log_failure_msg "The server quit without updating PID file ($pid_file_path)."
    185         return 1  # not waiting any more.
    186       fi
    187     fi
    188 
    189     echo $echo_n ".$echo_c"    
    190     i=`expr $i + 1`
    191     sleep 1                   #睡眠1秒
    192 
    193   done
    194 
    195   if test -z "$i" ; then   #如果i长度为0
    196     log_success_msg
    197     return 0
    198   else
    199     log_failure_msg
    200     return 1
    201   fi
    202 }
    203 
    204 
    205 # Get arguments from the my.cnf file,
    206 # the only group, which is read from now on is [mysqld]
    207 if test -x ./bin/my_print_defaults          #找到print_defaults的路径
    208 then
    209   print_defaults="./bin/my_print_defaults"
    210 elif test -x $bindir/my_print_defaults
    211 then
    212   print_defaults="$bindir/my_print_defaults"
    213 elif test -x $bindir/mysql_print_defaults
    214 then
    215   print_defaults="$bindir/mysql_print_defaults"
    216 else
    217   # Try to find basedir in /etc/my.cnf
    218   conf=/etc/my.cnf
    219   print_defaults=
    220   if test -r $conf                         #如果可读,从配置文件$conf读取
    221   then
    222     subpat=‘^[^=]*basedir[^=]*=\(.*\)$‘
    223     dirs=`sed -e "/$subpat/!d" -e ‘s//\1/‘ $conf`
    224     for d in $dirs
    225     do
    226       d=`echo $d | sed -e ‘s/[     ]//g‘`
    227       if test -x "$d/bin/my_print_defaults"
    228       then
    229         print_defaults="$d/bin/my_print_defaults"
    230         break
    231       fi
    232       if test -x "$d/bin/mysql_print_defaults"
    233       then
    234         print_defaults="$d/bin/mysql_print_defaults"
    235         break
    236       fi
    237     done
    238   fi
    239 
    240   # Hope it‘s in the PATH ... but I doubt it
    241   test -z "$print_defaults" && print_defaults="my_print_defaults"     #还是不行,就用my_print_defaults
    242 fi
    243 
    244 #
    245 # Read defaults file from ‘basedir‘.   If there is no defaults file there
    246 # check if it‘s in the old (depricated) place (datadir) and read it from there
    247 #
    248 
    249 #配置文件的目录,先从basedir读取,不行再读取datadir的
    250 extra_args=""
    251 if test -r "$basedir/my.cnf"
    252 then
    253   extra_args="-e $basedir/my.cnf"
    254 else
    255   if test -r "$datadir/my.cnf"
    256   then
    257     extra_args="-e $datadir/my.cnf"
    258   fi
    259 fi
    260 
    261 #使用parse_server_arguments函数和my_print_defaults 从my.cnf 中获取相应的参数并付给相应的变量
    262 parse_server_arguments `$print_defaults $extra_args mysqld server mysql_server mysql.server`
    263 
    264 #
    265 # Set pid file if not given
    266 #指定mysql启动时进程文件的位置
    267 if test -z "$mysqld_pid_file_path"
    268 then
    269   mysqld_pid_file_path=$datadir/`hostname`.pid
    270 else
    271   case "$mysqld_pid_file_path" in
    272     /* ) ;;
    273     * )  mysqld_pid_file_path="$datadir/$mysqld_pid_file_path" ;;
    274   esac
    275 fi
    276 
    277 case "$mode" in
    278   ‘start‘)
    279     # Start daemon
    280 
    281     # Safeguard (relative paths, core dumps..)
    282     cd $basedir
    283 
    284     echo $echo_n "Starting MySQL"
    285     if test -x $bindir/mysqld_safe  #如果mysqld_safe存在且可执行
    286     then
    287       # Give extra arguments to mysqld with the my.cnf file. This script
    288       # may be overwritten at next upgrade.
    289       #关键语句,启动mysqld_safe,传入3个参数:datadir,pid-file,other_args;将输出丢弃到系统黑洞/dev/null
    290       $bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null 2>&1 &
    291       wait_for_pid created "$!" "$mysqld_pid_file_path"; return_value=$?
    292 
    293       # Make lock for RedHat / SuSE          #mysql锁的检测和创建
    294       if test -w "$lockdir"       #如果存在且可写
    295       then
    296         touch "$lock_file_path"
    297       fi
    298 
    299       exit $return_value
    300     else
    301       log_failure_msg "Couldn‘t find MySQL server ($bindir/mysqld_safe)"
    302     fi
    303     ;;
    304 
    305   ‘stop‘)
    306     # Stop daemon. We use a signal here to avoid having to know the
    307     # root password.
    308 
    309     if test -s "$mysqld_pid_file_path"    #如果非空
    310     then
    311       mysqld_pid=`cat "$mysqld_pid_file_path"`      #获取mysqld进程
    312 
    313       if (kill -0 $mysqld_pid 2>/dev/null)
    314       then
    315         echo $echo_n "Shutting down MySQL"
    316         kill $mysqld_pid                             #杀死
    317         # mysqld should remove the pid file when it exits, so wait for it.
    318         wait_for_pid removed "$mysqld_pid" "$mysqld_pid_file_path"; return_value=$?
    319       else
    320         log_failure_msg "MySQL server process #$mysqld_pid is not running!"
    321         rm "$mysqld_pid_file_path"
    322       fi
    323 
    324       # Delete lock for RedHat / SuSE
    325       if test -f "$lock_file_path"    #如果存在锁,需要移除
    326       then
    327         rm -f "$lock_file_path"
    328       fi
    329       exit $return_value
    330     else
    331       log_failure_msg "MySQL server PID file could not be found!"
    332     fi
    333     ;;
    334 
    335   ‘restart‘)                    
    336     # Stop the service and regardless of whether it was          #重启mysql
    337     # running or not, start it again.
    338     if $0 stop  $other_args; then
    339       $0 start $other_args
    340     else
    341       log_failure_msg "Failed to stop running server, so refusing to try to start."
    342       exit 1
    343     fi
    344     ;;
    345 
    346   ‘reload‘|‘force-reload‘)                        #重载mysql
    347     if test -s "$mysqld_pid_file_path" ; then
    348       read mysqld_pid <  "$mysqld_pid_file_path"
    349       kill -HUP $mysqld_pid && log_success_msg "Reloading service MySQL"
    350       touch "$mysqld_pid_file_path"
    351     else
    352       log_failure_msg "MySQL PID file could not be found!"
    353       exit 1
    354     fi
    355     ;;
    356   ‘status‘)               
    357     # First, check to see if pid file exists           #mysql的状态           
    358     if test -s "$mysqld_pid_file_path" ; then 
    359       read mysqld_pid < "$mysqld_pid_file_path"
    360       if kill -0 $mysqld_pid 2>/dev/null ; then 
    361         log_success_msg "MySQL running ($mysqld_pid)"
    362         exit 0
    363       else
    364         log_failure_msg "MySQL is not running, but PID file exists"
    365         exit 1
    366       fi
    367     else
    368       # Try to find appropriate mysqld process                             
    369       mysqld_pid=`pidof $libexecdir/mysqld`
    370 
    371       # test if multiple pids exist
    372       pid_count=`echo $mysqld_pid | wc -w`
    373       if test $pid_count -gt 1 ; then
    374         log_failure_msg "Multiple MySQL running but PID file could not be found ($mysqld_pid)"
    375         exit 5
    376       elif test -z $mysqld_pid ; then 
    377         if test -f "$lock_file_path" ; then 
    378           log_failure_msg "MySQL is not running, but lock file ($lock_file_path) exists"
    379           exit 2
    380         fi 
    381         log_failure_msg "MySQL is not running"
    382         exit 3
    383       else
    384         log_failure_msg "MySQL is running but PID file could not be found"
    385         exit 4
    386       fi
    387     fi
    388     ;;
    389     *)
    390       # usage            #输出用法的提示
    391       basename=`basename "$0"`
    392       echo "Usage: $basename  {start|stop|restart|reload|force-reload|status}  [ MySQL server options ]"
    393       exit 1
    394     ;;
    395 esac
    396 
    397 exit 0

    选自:
    http://www.bubuko.com/infodetail-529751.html

    展开全文
  • MySQL启动脚本 mysql.server 详解

    千次阅读 2015-05-29 00:10:31
    今晚看了下5.6.24版本中的mysql.server脚本,发现与5.1中的脚本有了很大的改变。 先把脚本贴出来 #!/bin/sh # Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB # This file is public...

    今晚看了下5.6.24版本中的mysql.server脚本,发现与5.1中的脚本有了很大的改变。

    我们经常用到的/etc/init.d/mysql 这个启动脚本就是拷贝的mysql.server

    先把脚本贴出来

    #!/bin/sh
    # Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB
    # This file is public domain and comes with NO WARRANTY of any kind
    
    # MySQL daemon start/stop script.
    
    # Usually this is put in /etc/init.d (at least on machines SYSV R4 based
    # systems) and linked to /etc/rc3.d/S99mysql and /etc/rc0.d/K01mysql.
    # When this is done the mysql server will be started when the machine is
    # started and shut down when the systems goes down.
    
    # Comments to support chkconfig on RedHat Linux
    # chkconfig: 2345 64 36
    # description: A very fast and reliable SQL database engine.
    
    # Comments to support LSB init script conventions
    ### BEGIN INIT INFO
    # Provides: mysql
    # Required-Start: $local_fs $network $remote_fs
    # Should-Start: ypbind nscd ldap ntpd xntpd
    # Required-Stop: $local_fs $network $remote_fs
    # Default-Start:  2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: start and stop MySQL
    # Description: MySQL is a very fast and reliable SQL database engine.
    ### END INIT INFO
     
    # If you install MySQL on some other places than /usr/local/mysql, then you
    # have to do one of the following things for this script to work:
    #
    # - Run this script from within the MySQL installation directory
    # - Create a /etc/my.cnf file with the following information:
    #   [mysqld]
    #   basedir=<path-to-mysql-installation-directory>
    # - Add the above to any other configuration file (for example ~/.my.ini)
    #   and copy my_print_defaults to /usr/bin
    # - Add the path to the mysql-installation-directory to the basedir variable
    #   below.
    #
    # If you want to affect other MySQL variables, you should make your changes
    # in the /etc/my.cnf, ~/.my.cnf or other MySQL configuration files.
    
    # If you change base dir, you must also change datadir. These may get
    # overwritten by settings in the MySQL configuration files.
    # 如果你的basedir不是默认的/usr/local/mysql目录,则需要设置这个参数。
    # 如果你的datadir不是在$basedir/data目录下,则需要设置datadir参数。
    basedir=
    datadir=
    # Default value, in seconds, afterwhich the script should timeout waiting
    # for server start. 
    # Value here is overriden by value in my.cnf. 
    # 0 means don't wait at all
    # Negative numbers mean to wait indefinitely
    service_startup_timeout=900
    
    # Lock directory for RedHat / SuSE.
    lockdir='/var/lock/subsys'
    lock_file_path="$lockdir/mysql"
    
    # The following variables are only set for letting mysql.server find things.
    #如果basedir或者datadir为空,则给与相应的默认值。同时设置对应的sbindir和libexecdir变量值。
    # Set some defaults
    mysqld_pid_file_path=
    if test -z "$basedir"
    then
      basedir=/usr/local/mysql
      bindir=/usr/local/mysql/bin
      if test -z "$datadir"
      then
        datadir=/usr/local/mysql/data
      fi
      sbindir=/usr/local/mysql/bin
      libexecdir=/usr/local/mysql/bin
    else
      bindir="$basedir/bin"
      if test -z "$datadir"
      then
        datadir="$basedir/data"
      fi
      sbindir="$basedir/sbin"
      libexecdir="$basedir/libexec"
    fi
    
    # datadir_set is used to determine if datadir was set (and so should be
    # *not* set inside of the --basedir= handler.)
    datadir_set=
    
    #
    # Use LSB init script functions for printing messages, if possible
    # 如果得到系统自带日志函数,则使用,否则用自己的日志函数。
    lsb_functions="/lib/lsb/init-functions"
    if test -f $lsb_functions ; then
      . $lsb_functions
    else
      log_success_msg()
      {
        echo " SUCCESS! $@"
      }
      log_failure_msg()
      {
        echo " ERROR! $@"
      }
    fi
    #设置PATH环境变量
    PATH="/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin"
    export PATH
    
    mode=$1    # start or stop
    
    [ $# -ge 1 ] && shift
    
    #将第一个参数后的其他参数赋予other_args变量。
    other_args="$*"   # uncommon, but needed when called from an RPM upgrade action
               # Expected: "--skip-networking --skip-grant-tables"
               # They are not checked here, intentionally, as it is the resposibility
               # of the "spec" file author to give correct arguments only.
    
    case `echo "testing\c"`,`echo -n testing` in
        *c*,-n*) echo_n=   echo_c=     ;;
        *c*,*)   echo_n=-n echo_c=     ;;
        *)       echo_n=   echo_c='\c' ;;
    esac
    #参数解析函数(将参数值赋予对于变量)
    parse_server_arguments() {
      for arg do
        case "$arg" in
          --basedir=*)  basedir=`echo "$arg" | sed -e 's/^[^=]*=//'`
                        bindir="$basedir/bin"
    		    if test -z "$datadir_set"; then
    		      datadir="$basedir/data"
    		    fi
    		    sbindir="$basedir/sbin"
    		    libexecdir="$basedir/libexec"
            ;;
          --datadir=*)  datadir=`echo "$arg" | sed -e 's/^[^=]*=//'`
    		    datadir_set=1
    	;;
          --pid-file=*) mysqld_pid_file_path=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
          --service-startup-timeout=*) service_startup_timeout=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
        esac
      done
    }
    
    wait_for_pid () {
      verb="$1"           # created | removed
      pid="$2"            # process ID of the program operating on the pid-file
      pid_file_path="$3" # path to the PID file.
    
      i=0
      avoid_race_condition="by checking again"
    
      while test $i -ne $service_startup_timeout ; do
    
        case "$verb" in
          'created')
            # wait for a PID-file to pop into existence.
            test -s "$pid_file_path" && i='' && break
            ;;
          'removed')
            # wait for this PID-file to disappear
            test ! -s "$pid_file_path" && i='' && break
            ;;
          *)
            echo "wait_for_pid () usage: wait_for_pid created|removed pid pid_file_path"
            exit 1
            ;;
        esac
    
        # if server isn't running, then pid-file will never be updated
        if test -n "$pid"; then
          if kill -0 "$pid" 2>/dev/null; then
            :  # the server still runs
          else
            # The server may have exited between the last pid-file check and now.  
            if test -n "$avoid_race_condition"; then
              avoid_race_condition=""
              continue  # Check again.
            fi
    
            # there's nothing that will affect the file.
            log_failure_msg "The server quit without updating PID file ($pid_file_path)."
            return 1  # not waiting any more.
          fi
        fi
    
        echo $echo_n ".$echo_c"
        i=`expr $i + 1`
        sleep 1
    
      done
    
      if test -z "$i" ; then
        log_success_msg
        return 0
      else
        log_failure_msg
        return 1
      fi
    }
    
    # Get arguments from the my.cnf file,
    # the only group, which is read from now on is [mysqld]
    #得到有执行权限的my_print_defaults程序(这个程序是将my.cnf中的参数打印出来,
    #其读取my.cnf的顺序是/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf,后面的参数会覆盖掉前面的参数)
    if test -x ./bin/my_print_defaults
    then
      print_defaults="./bin/my_print_defaults"
    elif test -x $bindir/my_print_defaults
    then
      print_defaults="$bindir/my_print_defaults"
    elif test -x $bindir/mysql_print_defaults
    then
      print_defaults="$bindir/mysql_print_defaults"
    else
      # Try to find basedir in /etc/my.cnf
      conf=/etc/my.cnf
      print_defaults=
      if test -r $conf
      then
        subpat='^[^=]*basedir[^=]*=\(.*\)$'
        dirs=`sed -e "/$subpat/!d" -e 's//\1/' $conf`
        for d in $dirs
        do
          d=`echo $d | sed -e 's/[ 	]//g'`
          if test -x "$d/bin/my_print_defaults"
          then
            print_defaults="$d/bin/my_print_defaults"
            break
          fi
          if test -x "$d/bin/mysql_print_defaults"
          then
            print_defaults="$d/bin/mysql_print_defaults"
            break
          fi
        done
      fi
    
      # Hope it's in the PATH ... but I doubt it
      test -z "$print_defaults" && print_defaults="my_print_defaults"
    fi
    
    #
    # Read defaults file from 'basedir'.   If there is no defaults file there
    # check if it's in the old (depricated) place (datadir) and read it from there
    #
    #获取附加的my.cnf。这个里的参数值会覆盖其他的参数设置。
    #注意,如果basedir下有my.cnf就不会再去读取datadir下的my.cnf。如果basedir下面没有my.cnf则会继续读取datadir下的my.cnf。
    extra_args=""
    if test -r "$basedir/my.cnf"
    then
      extra_args="-e $basedir/my.cnf"
    else
      if test -r "$datadir/my.cnf"
      then
        extra_args="-e $datadir/my.cnf"
      fi
    fi
    #将my_print_defaults程序打印出来的Mysql参数用parse_server_arguments进行解析,解析后的值会是这个脚本环境中的变量值。
    parse_server_arguments `$print_defaults $extra_args mysqld server mysql_server mysql.server`
    
    #
    # Set pid file if not given
    #设置mysqld_pid_file_path变量(其代表pid 文件的位置),如果没有设置这个值或者不是绝对路径,则默认会在$datadir下面
    if test -z "$mysqld_pid_file_path"
    then
      mysqld_pid_file_path=$datadir/`hostname`.pid
    else
      case "$mysqld_pid_file_path" in
        /* ) ;;
        * )  mysqld_pid_file_path="$datadir/$mysqld_pid_file_path" ;;
      esac
    fi
    
    case "$mode" in
      'start')
        # Start daemon
    
        # Safeguard (relative paths, core dumps..)
        cd $basedir
    
        echo $echo_n "Starting MySQL"
        if test -x $bindir/mysqld_safe
        then
          # Give extra arguments to mysqld with the my.cnf file. This script
          # may be overwritten at next upgrade.
          #真正启动数据库就是下面这条命令。
          $bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null 2>&1 &
          wait_for_pid created "$!" "$mysqld_pid_file_path"; return_value=$?
    
          # Make lock for RedHat / SuSE
          if test -w "$lockdir"
          then
            touch "$lock_file_path"
          fi
    
          exit $return_value
        else
          log_failure_msg "Couldn't find MySQL server ($bindir/mysqld_safe)"
        fi
        ;;
    
      'stop')
        # Stop daemon. We use a signal here to avoid having to know the
        # root password.
    
        if test -s "$mysqld_pid_file_path"
        then
          mysqld_pid=`cat "$mysqld_pid_file_path"`
    
          if (kill -0 $mysqld_pid 2>/dev/null)
          then
            echo $echo_n "Shutting down MySQL"
            kill $mysqld_pid
            # mysqld should remove the pid file when it exits, so wait for it.
            wait_for_pid removed "$mysqld_pid" "$mysqld_pid_file_path"; return_value=$?
          else
            log_failure_msg "MySQL server process #$mysqld_pid is not running!"
            rm "$mysqld_pid_file_path"
          fi
    
          # Delete lock for RedHat / SuSE
          if test -f "$lock_file_path"
          then
            rm -f "$lock_file_path"
          fi
          exit $return_value
        else
          log_failure_msg "MySQL server PID file could not be found!"
        fi
        ;;
    
      'restart')
        # Stop the service and regardless of whether it was
        # running or not, start it again.
        if $0 stop  $other_args; then
          $0 start $other_args
        else
          log_failure_msg "Failed to stop running server, so refusing to try to start."
          exit 1
        fi
        ;;
    
      'reload'|'force-reload')
        if test -s "$mysqld_pid_file_path" ; then
          read mysqld_pid <  "$mysqld_pid_file_path"
          kill -HUP $mysqld_pid && log_success_msg "Reloading service MySQL"
          touch "$mysqld_pid_file_path"
        else
          log_failure_msg "MySQL PID file could not be found!"
          exit 1
        fi
        ;;
      'status')
        # First, check to see if pid file exists
        if test -s "$mysqld_pid_file_path" ; then 
          read mysqld_pid < "$mysqld_pid_file_path"
          if kill -0 $mysqld_pid 2>/dev/null ; then 
            log_success_msg "MySQL running ($mysqld_pid)"
            exit 0
          else
            log_failure_msg "MySQL is not running, but PID file exists"
            exit 1
          fi
        else
          # Try to find appropriate mysqld process
          mysqld_pid=`pidof $libexecdir/mysqld`
    
          # test if multiple pids exist
          pid_count=`echo $mysqld_pid | wc -w`
          if test $pid_count -gt 1 ; then
            log_failure_msg "Multiple MySQL running but PID file could not be found ($mysqld_pid)"
            exit 5
          elif test -z $mysqld_pid ; then 
            if test -f "$lock_file_path" ; then 
              log_failure_msg "MySQL is not running, but lock file ($lock_file_path) exists"
              exit 2
            fi 
            log_failure_msg "MySQL is not running"
            exit 3
          else
            log_failure_msg "MySQL is running but PID file could not be found"
            exit 4
          fi
        fi
        ;;
        *)
          # usage
          basename=`basename "$0"`
          echo "Usage: $basename  {start|stop|restart|reload|force-reload|status}  [ MySQL server options ]"
          exit 1
        ;;
    esac
    
    exit 0





    展开全文
  • linux自定义开机启动脚本

    万次阅读 2018-11-29 19:06:35
    有一种方法是在/etc/rc.local文件中加上启动脚本的命令,我加上之后,出现如下错误 我猜可能是mysql服务还没有启动,导致连接请求被拒绝。 因为我一直想将生成的这个.sh文件开机自启动,但是无奈总是不尽人意,...

    一、概述

    使用IDEA生成的linux系统可执行程序.sh文件,手动启动没有问题,开机自启动踩了不少坑,网上提供的三种方法都不适合,

    有一种方法是在/etc/rc.local文件中加上启动脚本的命令,我加上之后,出现如下错误

    enter image description here

    我猜可能是mysql服务还没有启动,导致连接请求被拒绝。

    因为我一直想将生成的这个.sh文件开机自启动,但是无奈总是不尽人意,后来换个思路,通过xshell脚本来启动这个.sh文件,搞定!

    二、自定义开机启动脚本

    在/etc/profile.d/目录下创建rds_start.sh文件

    vi rds_start.sh
    

    输入以下内容:

    #!/bin/sh
    #chkconfig: 2345 80 90
    #description:rds_start.sh
    cd /home/softs/rds/bin/
    nohup ./rds_start.sh &
    

    其中其三行是固定格式

    #!/bin/sh
    是指此脚本使用/bin/sh来解释执行
    
    #chkconfig: 2345 20 80
    2345表示系统运行级别是2,3,4或者5时都启动此服务,20,是启动的优先级,80是关闭的优先级,如果启动优先级配置的数太小时如0时,则有可能启动不成功,因为此时可能其依赖的网络服务还没有启动,从而导致自启动失败。
    
    #description:rds_start.sh
    是描述信息
    

    wq!保存后 使用chmod设置可执行权限

    chmod +x rds_start.sh
    

    然后将其拷贝到/etc/init.d/目录下,否则添加服务不成功:

    cp rds_start.sh /etc/init.d/
    

    添加到系统服务

    chkconfig --add rds_start.sh
    

    设置开机启动

    chkconfig rds_start.sh on
    

    然后重启linux

    shutdown -r now
    

    重启之后连接查看效果:

    ps -ef | grep rds
    

    enter image description here

    开机自启动成功!

    备注

    pom.xml中加入以下插件可打包生成.sh文件

    <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>appassembler-maven-plugin</artifactId>
            <version>1.10</version>
            <configuration>
                <configurationDirectory>conf</configurationDirectory>
                <includeConfigurationDirectoryInClasspath>true</includeConfigurationDirectoryInClasspath>
                <repositoryLayout>flat</repositoryLayout>
                <assembleDirectory>${project.build.directory}/rds</assembleDirectory>
                <repositoryName>libs</repositoryName>
                <extraJvmArguments>-Xms1024m -Xmx1024m</extraJvmArguments>
                <!-- 生成linux -->
                <binFileExtensions>
                    <unix>.sh</unix>
                </binFileExtensions>
                <programs>
                    <program>
                        <mainClass>com.dj.rds.Application</mainClass>
                        <name>rds_start</name>
                    </program>
                </programs>
            </configuration>
        </plugin>
    

    maven打包命令

    package appassembler:assemble
    

    参考地址:https://blog.csdn.net/achilles12345/article/details/52150867

    访问我的达人课

    访问我的博客 Wang's Blog

    关注我的微信公众号获取更多资源

    展开全文
  • 引入库函数 #!/bin/bash #引入系统库函数 ....基本路径定义 #基础路径定义 basedir=’/application/mysql’ bandir=’/application/mysql/bin’ ...lock_file_path=‘lockdir/mysql′mysqldpidfilepath...
  • linuxmysql启动

    千次阅读 2019-04-02 23:40:09
    {mysql}表示mysql的安装目录 如何启动/停止/重启MySQL ... 2、使用 mysqld 脚本启动:/etc/inint.d/mysqld start如果/etc/inint.d/下找不到mysqld,可以去mysql安装目录,{mysql}/init.d/mysqld start  3、...
  • 方法一 netstat -lanp|grep tcp|grep 3306|wc -l 方法二 /usr/bin/lsof -i:3306|wc -l 方法三 mysql -u用户名 -p密码 -e "select version();' &>/dev/null echo $? 判断返回值
  • Mysql mysql.server启动脚本详解

    万次阅读 2012-05-22 17:28:53
    今天不忙,看了一下linuxMysql启动脚本,记录一下大体的过程,具体详细的命令的执行,没有写出   #1.变量初始化 #--------------------------------------------------------------------------------------...
  • linux启动脚本和service、chkconfig

    千次阅读 2018-08-03 23:22:36
    1. 启动脚本   (1) 启动脚本的写法   case "$1" in start) do start-thing; ;; stop) do stop-thing; ;; restart) do restart-thing; ;; ... esac ...
  • linux添加开机自启动脚本示例详解

    万次阅读 2017-02-17 18:19:47
    linux开机启动的顺序及添加开机自启动脚本的方法
  • Linux开机自启动脚本的总结

    千次阅读 2017-08-04 16:35:39
    一、在/etc/rc.local中添加如果不想将脚本粘来粘去,或创建链接什么的,... 再在/etc/rc.local的末尾添加一行以绝对路径启动脚本的行; 如:$ vim /etc/rc.local #!/bin/sh # # This script will be executed *after* al
  • linux 利用脚本安装mysql

    千次阅读 2020-01-18 12:13:41
    脚本代码:新建文件将代码拷贝修改文件名称:mysql_install.sh #!/bin/bash #-------------CopyRight------------- # Name:MySQL Install 5.6 # Version Number:1.02 # System:CentOS7 # Type:shell # Language...
  • linuxmysql 启动命令

    2017-04-12 10:44:00
    1,使用service 启动、关闭MySQL服务 service mysql start service mysql stop ...service mysql restart运行上面命令,其实是service命令去找/...2,使用/etc/init.d/mysql启动、关闭MySQL服务, [root@DB-Ser...
  • Linux启动mysql

    万次阅读 2019-04-23 10:47:12
    linux平台及windows平台mysql重启方法  Linux下重启MySQL的正确方法:  1、通过rpm包安装的MySQL  service mysqld restart  2、从源码包安装的MySQL  // linux关闭MySQL的命令  $mysql_dir/bin/...
  • Linux-shell脚本-mysql一键安装

    千次阅读 2016-06-08 22:26:40
    mysql-5.6.29-linux-glibc2.5-x86_64.tar.gz mysql.sh脚本 mysql.sh脚本#!/bin/bash#输入mysql压缩文件地址 fileName=$1;#解压mysql tar -zxvf ${fileName}; echo 'mysql解压完成,正在删除/usr/local/mys
  • LinuxMySQL 启动与关闭

    千次阅读 2019-04-01 10:07:14
    版权声明: ... 一.启动 1.1 MySQL 进程 可以用ps 命令查看进程: [root@rac2 ~]# ps -ef|grep mysql root 2161 1 0 09:38 ? 00:00:00 /bin/sh /usr/bin/m...
  • #service servicename status是当前状态 #chkconfig --list servicename是查看启动状态,也就是是否开机自动启动 <br /> 首先写好脚本,如 mysql,把它放到 /etc/init.d 下,修改属性: $ ...
  • Linux 平台MySQL启动关闭方式总结

    千次阅读 2017-02-09 15:54:04
    MySQL启动方法有很多种,下面对比、总结这几种方法的一些差异和特性,下面实验的版本为MySQL 5.6。如有疏漏或不足,敬请指点一二。   1:使用mysqld启动、关闭MySQL服务    mysqld是MySQL的...
  • linux 启动mysql 命令

    千次阅读 2012-01-01 12:56:39
    如何启动/停止/重启MySQL 一、启动方式 1、使用 service 启动:service mysqld start 2、使用 mysqld 脚本启动:/etc/inint.d/mysqld start 3、使用 safe_mysqld 启动:safe_mysqld& 二、停止 1、...
  • Linux环境使用Shell脚本安装Mysql5.6

    千次阅读 2019-04-03 23:27:48
    之前在Linux上安装Mysql总是花费很多时间,一行一行执行命令,很是繁琐,所以决定写个脚本提升下效率,这个脚本是基于Mysql5.6版本写的,后续版本不适用,但是如果自己能看懂脚本的可以稍加修改,如果有需要其他...
  • Linux启动MySQL的问题

    2017-05-10 02:45:39
    此时便可以通过service mysql start启动服务 ![图片说明](https://img-ask.csdn.net/upload/201705/10/1494382682_284211.png) 而在**CentOS6上安装MySQL5.6.36**的tar.gz包后,同上面没有添加或修改配置文件,my....
  • MySqlLinux启动方式

    万次阅读 2016-05-25 15:16:10
    Linux系统下,MySQL服务器通常有四种启动方式:mysqld守护进程启动,mysqld_safe启动mysql.server启动,mysqld_multi多实例启动。1、mysqld守护进程启动一般的,我们通过这种方式手动的调用mysqld,如果不是出去...
  • Linux环境使用Shell脚本安装Mysql5.7

    千次阅读 2019-11-19 00:27:46
    1. 前言 之前写过使用shell脚本安装Mysql5.6版本,闲暇之余研究了下mysql5.7与...(Linux环境使用Shell脚本安装Mysql5.6:https://blog.csdn.net/u011294519/article/details/89004581) 2.Mysql5.6与5.7的安装区...
  • LinuxMySQL 启动与关闭 说明

    万次阅读 2011-11-29 15:36:02
    启动1.1 MySQL 进程可以用ps 命令查看进程:[root@rac2 ~]# ps -ef|grep mysqlroot 2161 1 0 09:38 ? 00:00:00 /bin/sh /usr/bin/mysqld_safe--datadir=/var/lib/mysql --pid-file=/var/lib/mysql/rac2.pidmysq
  • linux启动mysql服务

    2013-03-21 09:24:29
    LINUX重启MYSQL的命令 . 分类: Linux 2010-06-25 10:21 6367人阅读 评论(0) 收藏 举报 如何启动/停止/重启MySQL ...2、使用 mysqld 脚本启动:/etc/inint.d/mysqld start 3、使用 safe_mysqld
  • 首先检查你所用的Linux下有没有安装过mysql,有没有卸载干净 同时也要卸载centos7自带数据库mariadb #uninstall centos7 mariadb rpm -e --nodeps mariadb-libs-5.5.37-1.el7_0.x86_64 rpm -qa|grep -i mysql ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 95,568
精华内容 38,227
关键字:

linuxmysql启动脚本

linux 订阅
mysql 订阅