mysql.server.sh 5.23 KB
Newer Older
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1 2 3 4
#!/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

lenz@mysql.com's avatar
lenz@mysql.com committed
5
# MySQL daemon start/stop script.
bk@work.mysql.com's avatar
bk@work.mysql.com committed
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 10
# When this is done the mysql server will be started when the machine is
# started and shut down when the systems goes down.
bk@work.mysql.com's avatar
bk@work.mysql.com committed
11 12

# Comments to support chkconfig on RedHat Linux
13
# chkconfig: 2345 90 20
bk@work.mysql.com's avatar
bk@work.mysql.com committed
14 15
# description: A very fast and reliable SQL database engine.

16 17 18 19 20
# Comments to support LSB init script conventions
### BEGIN INIT INFO
# Provides: mysql
# Required-Start: $local_fs $network $remote_fs
# Required-Stop: $local_fs $network $remote_fs
21
# Default-Start:  2 3 4 5
22
# Default-Stop: 0 1 6
lenz@mysql.com's avatar
lenz@mysql.com committed
23
# Short-Description: start and stop MySQL
24 25 26
# Description: MySQL is a very fast and reliable SQL database engine.
### END INIT INFO
 
27
# If you install MySQL on some other places than @prefix@, then you
lenz@mysql.com's avatar
lenz@mysql.com committed
28
# have to do one of the following things for this script to work:
29
#
lenz@mysql.com's avatar
lenz@mysql.com committed
30
# - Run this script from within the MySQL installation directory
31 32
# - Create a /etc/my.cnf file with the following information:
#   [mysqld]
lenz@mysql.com's avatar
lenz@mysql.com committed
33
#   basedir=<path-to-mysql-installation-directory>
34 35 36 37 38
# - 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.
#
39
# If you want to affect other MySQL variables, you should make your changes
40
# in the /etc/my.cnf, ~/.my.cnf or other MySQL configuration files.
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
41

42 43 44
basedir=

# The following variables are only set for letting mysql.server find things.
bk@work.mysql.com's avatar
bk@work.mysql.com committed
45

46 47 48 49 50 51 52 53 54 55
# Set some defaults
datadir=@localstatedir@
pid_file=
if test -z "$basedir"
then
  basedir=@prefix@
  bindir=@bindir@
else
  bindir="$basedir/bin"
fi
56 57 58 59

PATH=/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin
export PATH

60 61 62 63 64 65 66 67 68 69
if test -z "$pid_file"
then
  pid_file=$datadir/`@HOSTNAME@`.pid
else
  case "$pid_file" in
    /* ) ;;
    * )  pid_file="$datadir/$pid_file" ;;
  esac
fi

70
mode=$1    # start or stop
bk@work.mysql.com's avatar
bk@work.mysql.com committed
71

72 73 74 75 76 77 78 79
parse_arguments() {
  for arg do
    case "$arg" in
      --basedir=*)  basedir=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
      --datadir=*)  datadir=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
      --pid-file=*) pid_file=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
    esac
  done
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
80
}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
81

82 83
# Get arguments from the my.cnf file,
# groups [mysqld] [mysql_server] and [mysql.server]
84 85 86
if test -x ./bin/my_print_defaults
then
  print_defaults="./bin/my_print_defaults"
87
elif test -x $bindir/my_print_defaults
88
then
89 90
  print_defaults="$bindir/my_print_defaults"
elif test -x $bindir/mysql_print_defaults
91
then
92
  print_defaults="$bindir/mysql_print_defaults"
93
else
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
  # 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"
119 120
fi

121 122 123 124 125 126 127 128 129 130 131 132
#
# Test if someone changed datadir;  In this case we should also read the
# default arguments from this directory
#

extra_args=""
if test "$datadir" != "@localstatedir@"
then
  extra_args="-e $datadir/my.cnf"
fi

parse_arguments `$print_defaults $extra_args mysqld mysql_server mysql.server`
133

bk@work.mysql.com's avatar
bk@work.mysql.com committed
134 135 136 137 138 139 140
# Safeguard (relative paths, core dumps..)
cd $basedir

case "$mode" in
  'start')
    # Start daemon

141
    if test -x $bindir/mysqld_safe
bk@work.mysql.com's avatar
bk@work.mysql.com committed
142 143 144
    then
      # Give extra arguments to mysqld with the my.cnf file. This script may
      # be overwritten at next upgrade.
145
      $bindir/mysqld_safe --datadir=$datadir --pid-file=$pid_file &
146
      # Make lock for RedHat / SuSE
147
      if test -w /var/lock/subsys
148 149 150
      then
        touch /var/lock/subsys/mysql
      fi
bk@work.mysql.com's avatar
bk@work.mysql.com committed
151
    else
152
      echo "Can't execute $bindir/mysqld_safe from dir $basedir"
bk@work.mysql.com's avatar
bk@work.mysql.com committed
153 154 155 156 157 158
    fi
    ;;

  'stop')
    # Stop daemon. We use a signal here to avoid having to know the
    # root password.
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
159
    if test -s "$pid_file"
bk@work.mysql.com's avatar
bk@work.mysql.com committed
160 161 162 163 164 165 166 167
    then
      mysqld_pid=`cat $pid_file`
      echo "Killing mysqld with pid $mysqld_pid"
      kill $mysqld_pid
      # mysqld should remove the pid_file when it exits, so wait for it.

      sleep 1
      while [ -s $pid_file -a "$flags" != aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ]
168
      do
lenz@mysql.com's avatar
lenz@mysql.com committed
169
        [ -z "$flags" ] && echo "Wait for mysqld to exit\c" || echo ".\c"
170 171
        flags=a$flags
        sleep 1
bk@work.mysql.com's avatar
bk@work.mysql.com committed
172 173 174 175 176 177
      done
      if [ -s $pid_file ]
         then echo " gave up waiting!"
      elif [ -n "$flags" ]
         then echo " done"
      fi
178
      # delete lock for RedHat / SuSE
tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
179
      if test -f /var/lock/subsys/mysql
180
      then
181
        rm -f /var/lock/subsys/mysql
182
      fi
bk@work.mysql.com's avatar
bk@work.mysql.com committed
183 184 185 186 187
    else
      echo "No mysqld pid file found. Looked for $pid_file."
    fi
    ;;

188 189 190 191 192 193 194
  'restart')
    # Stop the service and regardless of whether it was
    # running or not, start it again.
    $0 stop
    $0 start
		;;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
195 196
  *)
    # usage
197
    echo "Usage: $0 start|stop|restart"
bk@work.mysql.com's avatar
bk@work.mysql.com committed
198 199 200
    exit 1
    ;;
esac