~ubuntu-branches/ubuntu/trusty/mariadb-5.5/trusty-proposed

« back to all changes in this revision

Viewing changes to support-files/mysql.server.sh

  • Committer: Package Import Robot
  • Author(s): Otto Kekäläinen
  • Date: 2013-12-22 10:27:05 UTC
  • Revision ID: package-import@ubuntu.com-20131222102705-mndw7s12mz0szrcn
Tags: upstream-5.5.32
Import upstream version 5.5.32

Show diffs side-by-side

added added

removed removed

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