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 (Percona Server) 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 (Percona Server)
25# Description: Percona-Server is a SQL database engine with focus on high performance.
26### END INIT INFO
27
28# If you install MySQL on some other places than @prefix@, 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
46basedir=
47datadir=
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
54service_startup_timeout=900
55
56[ -e /etc/sysconfig/mysql ] && . /etc/sysconfig/mysql
57
58# Lock directory for RedHat / SuSE.
59lockdir='/var/lock/subsys'
60lock_file_path="$lockdir/mysql"
61
62# The following variables are only set for letting mysql.server find things.
63
64# Set some defaults
65mysqld_pid_file_path=
66if test -z "$basedir"
67then
68  basedir=@prefix@
69  bindir=@bindir@
70  if test -z "$datadir"
71  then
72    datadir=@localstatedir@
73  fi
74  sbindir=@sbindir@
75  libexecdir=@libexecdir@
76else
77  bindir="$basedir/bin"
78  if test -z "$datadir"
79  then
80    datadir="$basedir/data"
81  fi
82  sbindir="$basedir/sbin"
83  libexecdir="$basedir/libexec"
84fi
85
86# datadir_set is used to determine if datadir was set (and so should be
87# *not* set inside of the --basedir= handler.)
88datadir_set=
89
90#
91# Use LSB init script functions for printing messages, if possible
92#
93lsb_functions="/lib/lsb/init-functions"
94if test -f $lsb_functions ; then
95  . $lsb_functions
96else
97  log_success_msg()
98  {
99    echo " SUCCESS! $@"
100  }
101  log_failure_msg()
102  {
103    echo " ERROR! $@"
104  }
105fi
106
107PATH="/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin"
108export PATH
109
110mode=$1    # start or stop
111
112[ $# -ge 1 ] && shift
113
114
115other_args="$*"   # uncommon, but needed when called from an RPM upgrade action
116           # Expected: "--skip-networking --skip-grant-tables"
117           # They are not checked here, intentionally, as it is the resposibility
118           # of the "spec" file author to give correct arguments only.
119
120case `echo "testing\c"`,`echo -n testing` in
121    *c*,-n*) echo_n=   echo_c=     ;;
122    *c*,*)   echo_n=-n echo_c=     ;;
123    *)       echo_n=   echo_c='\c' ;;
124esac
125
126parse_server_arguments() {
127  for arg do
128    case "$arg" in
129      --basedir=*)  basedir=`echo "$arg" | sed -e 's/^[^=]*=//'`
130                    bindir="$basedir/bin"
131		    if test -z "$datadir_set"; then
132		      datadir="$basedir/data"
133		    fi
134		    sbindir="$basedir/sbin"
135		    libexecdir="$basedir/libexec"
136        ;;
137      --datadir=*)  datadir=`echo "$arg" | sed -e 's/^[^=]*=//'`
138		    datadir_set=1
139	;;
140      --pid-file=*) mysqld_pid_file_path=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
141      --service-startup-timeout=*) service_startup_timeout=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
142    esac
143  done
144}
145
146wait_for_pid () {
147  verb="$1"           # created | removed
148  pid="$2"            # process ID of the program operating on the pid-file
149  pid_file_path="$3" # path to the PID file.
150
151  i=0
152  avoid_race_condition="by checking again"
153
154  while test $i -ne $service_startup_timeout ; do
155
156    case "$verb" in
157      'created')
158        # wait for a PID-file to pop into existence.
159        test -s "$pid_file_path" && i='' && break
160        ;;
161      'removed')
162        # wait for this PID-file to disappear
163        test ! -s "$pid_file_path" && i='' && break
164        ;;
165      *)
166        echo "wait_for_pid () usage: wait_for_pid created|removed pid pid_file_path"
167        exit 1
168        ;;
169    esac
170
171    # if server isn't running, then pid-file will never be updated
172    if test -n "$pid"; then
173      if kill -0 "$pid" 2>/dev/null; then
174        :  # the server still runs
175      else
176        # The server may have exited between the last pid-file check and now.
177        if test -n "$avoid_race_condition"; then
178          avoid_race_condition=""
179          continue  # Check again.
180        fi
181
182        # there's nothing that will affect the file.
183        log_failure_msg "The server quit without updating PID file ($pid_file_path)."
184        return 1  # not waiting any more.
185      fi
186    fi
187
188    echo $echo_n ".$echo_c"
189    i=`expr $i + 1`
190    sleep 1
191
192  done
193
194  if test -z "$i" ; then
195    log_success_msg
196    return 0
197  else
198    log_failure_msg
199    return 1
200  fi
201}
202
203# Get arguments from the my.cnf file,
204# the only group, which is read from now on is [mysqld]
205if test -x "$bindir/my_print_defaults";  then
206  print_defaults="$bindir/my_print_defaults"
207else
208  # Try to find basedir in /etc/my.cnf
209  conf=/etc/my.cnf
210  print_defaults=
211  if test -r $conf
212  then
213    subpat='^[^=]*basedir[^=]*=\(.*\)$'
214    dirs=`sed -e "/$subpat/!d" -e 's//\1/' $conf`
215    for d in $dirs
216    do
217      d=`echo $d | sed -e 's/[ 	]//g'`
218      if test -x "$d/bin/my_print_defaults"
219      then
220        print_defaults="$d/bin/my_print_defaults"
221        break
222      fi
223    done
224  fi
225
226  # Hope it's in the PATH ... but I doubt it
227  test -z "$print_defaults" && print_defaults="my_print_defaults"
228fi
229
230#
231# Read defaults file from 'basedir'.   If there is no defaults file there
232# check if it's in the old (depricated) place (datadir) and read it from there
233#
234
235extra_args=""
236if test -r "$basedir/my.cnf"
237then
238  extra_args="-e $basedir/my.cnf"
239else
240  if test -r "$datadir/my.cnf"
241  then
242    extra_args="-e $datadir/my.cnf"
243  fi
244fi
245
246parse_server_arguments `$print_defaults $extra_args mysqld server mysql_server mysql.server`
247
248#
249# Set pid file if not given
250#
251if test -z "$mysqld_pid_file_path"
252then
253  mysqld_pid_file_path=$datadir/`@HOSTNAME@`.pid
254else
255  case "$mysqld_pid_file_path" in
256    /* ) ;;
257    * )  mysqld_pid_file_path="$datadir/$mysqld_pid_file_path" ;;
258  esac
259fi
260
261case "$mode" in
262  'start')
263    # Start daemon
264
265    # Safeguard (relative paths, core dumps..)
266    cd $basedir
267
268    echo $echo_n "Starting MySQL (Percona Server)"
269    if test -x $bindir/mysqld_safe
270    then
271      # Give extra arguments to mysqld with the my.cnf file. This script
272      # may be overwritten at next upgrade.
273      $bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null &
274      wait_for_pid created "$!" "$mysqld_pid_file_path"; return_value=$?
275
276      # Make lock for RedHat / SuSE
277      if test -w "$lockdir"
278      then
279        touch "$lock_file_path"
280      fi
281
282      exit $return_value
283    else
284      log_failure_msg "Couldn't find MySQL server ($bindir/mysqld_safe)"
285    fi
286    ;;
287
288  'stop')
289    # Stop daemon. We use a signal here to avoid having to know the
290    # root password.
291
292    if test -s "$mysqld_pid_file_path"
293    then
294      mysqld_pid=`cat "$mysqld_pid_file_path"`
295
296      if (kill -0 $mysqld_pid 2>/dev/null)
297      then
298        echo $echo_n "Shutting down MySQL (Percona Server)"
299        kill $mysqld_pid
300        # mysqld should remove the pid file when it exits, so wait for it.
301        wait_for_pid removed "$mysqld_pid" "$mysqld_pid_file_path"; return_value=$?
302      else
303        log_failure_msg "MySQL (Percona Server) server process #$mysqld_pid is not running!"
304        rm "$mysqld_pid_file_path"
305      fi
306
307      # Delete lock for RedHat / SuSE
308      if test -f "$lock_file_path"
309      then
310        rm -f "$lock_file_path"
311      fi
312      exit $return_value
313    else
314      log_failure_msg "MySQL (Percona Server) PID file could not be found!"
315    fi
316    ;;
317
318  'restart')
319    # Stop the service and regardless of whether it was
320    # running or not, start it again.
321    if $0 stop  $other_args; then
322      $0 start $other_args
323    else
324      log_failure_msg "Failed to stop running server, so refusing to try to start."
325      exit 1
326    fi
327    ;;
328
329  'reload'|'force-reload')
330    if test -s "$mysqld_pid_file_path" ; then
331      read mysqld_pid <  "$mysqld_pid_file_path"
332      kill -HUP $mysqld_pid && log_success_msg "Reloading service MySQL (Percona Server)"
333      touch "$mysqld_pid_file_path"
334    else
335      log_failure_msg "MySQL (Percona Server) PID file could not be found!"
336      exit 1
337    fi
338    ;;
339  'status')
340    # First, check to see if pid file exists
341    if test -s "$mysqld_pid_file_path" ; then
342      read mysqld_pid < "$mysqld_pid_file_path"
343      if kill -0 $mysqld_pid 2>/dev/null ; then
344        log_success_msg "MySQL (Percona Server) running ($mysqld_pid)"
345        exit 0
346      else
347        log_failure_msg "MySQL (Percona Server) is not running, but PID file exists"
348        exit 1
349      fi
350    else
351      # Try to find appropriate mysqld process
352      mysqld_pid=`@PIDOF@ $libexecdir/mysqld`
353
354      # test if multiple pids exist
355      pid_count=`echo $mysqld_pid | wc -w`
356      if test $pid_count -gt 1 ; then
357        log_failure_msg "Multiple MySQL running but PID file could not be found ($mysqld_pid)"
358        exit 5
359      elif test -z $mysqld_pid ; then
360        if test -f "$lock_file_path" ; then
361          log_failure_msg "MySQL (Percona Server) is not running, but lock file ($lock_file_path) exists"
362          exit 2
363        fi
364        log_failure_msg "MySQL (Percona Server) is not running"
365        exit 3
366      else
367        log_failure_msg "MySQL (Percona Server) is running but PID file could not be found"
368        exit 4
369      fi
370    fi
371    ;;
372    *)
373      # usage
374      basename=`basename "$0"`
375      echo "Usage: $basename  {start|stop|restart|reload|force-reload|status}  [ MySQL (Percona Server) options ]"
376      exit 1
377    ;;
378esac
379
380exit 0
381