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. Multi-instance enhancements by Tim Bunce.
6
7# Usually this is put in /etc/init.d (at least on machines SYSV R4
8# based systems) and linked to
9#     /etc/rc3.d/S99mysql.svr1
10# and /etc/rc0.d/S01mysql.svr1
11# When this is done the mysql server will be started when the machine is
12# started and shut down when the systems goes down. The '.svr1' suffix can
13# be used to identify one of a number of servers. Multiple symlinks can be
14# created, one per instance. The 'svrN' suffix can then be used to
15# prefix configuration variables in a seperate section of /etc/my.cnf.
16# See example below.
17#
18# A typical multi-instance /etc/my.cnf file would look like:
19# [mysqld]
20# basedir=...
21# key_buffer_size=16M
22# max_allowed_packet=1M
23# [mysql_multi_server]
24# svr1-datadir=/foo1/bar
25# svr2-datadir=/foo2/bar
26#
27# and then the /foo1/bar/my.cnf and /foo2/bar/my.cnf files
28# would contain all the *instance specific* configurations.
29#
30# This script can also be run manually in which case the server instance
31# is identified by an extra argument, for example:
32#     /etc/init.d/mysql stop svr3
33#
34
35PATH=/sbin:/usr/sbin:/bin:/usr/bin
36export PATH
37
38mode=$1    # start or stop
39svr=$2     # eg 'svr1' (optional)
40if [ "$2" = "" ]
41then name=`basename $0`
42else name=$2
43fi
44
45# Extract identity of the server we are working with
46svr=`echo "$name" | sed -e 's/.*\<\(svr[1-9][0-9]*\)\>.*/\1/'`
47if [ "$svr" = "" ]
48then
49  echo "Can't determine database svr number from name '$name'"
50  exit 1
51fi
52
53echo "mysqld $svr $mode"
54
55parse_arguments() {
56  for arg do
57    case "$arg" in
58      --basedir=*|--${svr}-basedir=*)
59        basedir=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
60      --datadir=*|--${svr}-basedir=*)
61        datadir=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
62      --pid-file=*|--${svr}-basedir=*)
63        pid_file=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
64    esac
65  done
66}
67
68# Get arguments from the my.cnf file, groups [mysqld], [mysql_server],
69# and mysql_multi_server
70if test -x ./bin/my_print_defaults
71then
72  print_defaults="./bin/my_print_defaults"
73elif test -x @bindir@/my_print_defaults
74then
75  print_defaults="@bindir@/my_print_defaults"
76elif test -x @bindir@/mysql_print_defaults
77then
78  print_defaults="@bindir@/mysql_print_defaults"
79else
80  # Try to find basedir in /etc/my.cnf
81  conf=/etc/my.cnf
82  print_defaults=
83  if test -r $conf
84  then
85    subpat='^[^=]*basedir[^=]*=\(.*\)$'
86    dirs=`sed -e "/$subpat/!d" -e 's//\1/' $conf
87    for d in $dirs
88    do
89      d=`echo $d | sed -e 's/[ 	]//g'`
90      if test -x "$d/bin/my_print_defaults"
91      then
92        print_defaults="$d/bin/my_print_defaults"
93        break
94      fi
95      if test -x "$d/bin/mysql_print_defaults"
96      then
97        print_defaults="$d/bin/mysql_print_defaults"
98        break
99      fi
100    done
101  fi
102
103  # Hope it's in the PATH ... but I doubt it
104  test -z "$print_defaults" && print_defaults="my_print_defaults"
105fi
106
107datadir=@localstatedir@
108basedir=
109pid_file=
110parse_arguments `$print_defaults $defaults mysqld mysql_server mysql_multi_server`
111
112if test -z "$basedir"
113then
114  basedir=@prefix@
115  bindir=@bindir@
116else
117  bindir="$basedir/bin"
118fi
119if test -z "$pid_file"
120then
121  pid_file=$datadir/`@HOSTNAME@`.pid
122else
123  case "$pid_file" in
124    /* ) ;;
125    * )  pid_file="$datadir/$pid_file" ;;
126  esac
127fi
128
129# Safeguard (relative paths, core dumps..)
130cd $basedir
131
132case "$mode" in
133  'start')
134    # Start daemon
135
136    if test -x $bindir/mysqld_safe
137    then
138      # We only need to specify datadir and pid-file here and we
139      # get all other instance-specific config from $datadir/my.cnf.
140      # We have to explicitly pass --defaults-extra-file because it
141      # reads the config files before the command line options.
142      # Also it must be first because of the way mysqld_safe works.
143      $bindir/mysqld_safe --defaults-extra-file=$datadir/my.cnf \
144                          --datadir=$datadir --pid-file=$pid_file &
145      # Make lock for RedHat / SuSE
146      if test -d /var/lock/subsys
147      then
148        touch /var/lock/subsys/mysql
149      fi
150    else
151      echo "Can't execute $bindir/mysqld_safe"
152    fi
153    ;;
154
155  'stop')
156    # Stop daemon. We use a signal here to avoid having to know the
157    # root password.
158    if test -f "$pid_file"
159    then
160      mysqld_pid=`cat $pid_file`
161      echo "Killing mysqld $svr with pid $mysqld_pid"
162      kill $mysqld_pid
163      # mysqld should remove the pid_file when it exits, so wait for it.
164
165      sleep 1
166      while [ -s $pid_file -a "$flags" != aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ]
167      do
168        [ -z "$flags" ] && echo "Wait for mysqld $svr to exit\c" || echo ".\c"
169        flags=a$flags
170        sleep 1
171      done
172      if [ -s $pid_file ]
173         then echo " gave up waiting!"
174      elif [ -n "$flags" ]
175         then echo " done"
176      fi
177      # delete lock for RedHat / SuSE
178      if test -e /var/lock/subsys/mysql
179      then
180        rm /var/lock/subsys/mysql
181      fi
182    else
183      echo "No mysqld pid file found. Looked for $pid_file."
184    fi
185    ;;
186
187  *)
188    # usage
189    echo "usage: $0 start|stop [ svrN ]"
190    exit 1
191    ;;
192esac
193