File: mysql-server.init

package info (click to toggle)
mysql-dfsg 4.0.24-10sarge3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 56,212 kB
  • ctags: 42,312
  • sloc: ansic: 257,918; cpp: 93,002; perl: 29,480; sh: 24,628; tcl: 19,882; yacc: 3,558; makefile: 2,452; java: 2,300; awk: 1,484; asm: 687; sed: 428; sql: 27
file content (163 lines) | stat: -rw-r--r-- 4,368 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/bash
#
# MySQL daemon start/stop script.
#
# Debian version. Based on the original by TcX.
#
set -e
set -u
${DEBIAN_SCRIPT_DEBUG:+ set -v -x}

test -x /usr/sbin/mysqld || exit 0

SELF=$(cd $(dirname $0); pwd -P)/$(basename $0)
CONF=/etc/mysql/my.cnf
MYADMIN="/usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf"
RUNDIR=/var/run/mysqld/

# priority can be overriden and "-s" adds output to stderr
ERR_LOGGER="logger -p daemon.err -t /etc/init.d/mysql -i"

# Safeguard (relative paths, core dumps..)
cd /
umask 077
export PATH=/bin:/usr/bin

# mysqladmin likes to read /root/.my.cnf. This is usually not what I want
# as many admins e.g. only store a password without a username there and
# so break my scripts.
export HOME=/etc/mysql/

## fetch a particular option from mysql's invocation
#
# usage: void mysqld_get_param option
mysqld_get_param() {
	/usr/sbin/mysqld --print-defaults \
		| tr " " "\n" \
		| grep -- "--$1" \
		| tail -n 1 \
		| cut -d= -f2
}

## Checks if there is a server running and if so if it is accessible.
#
# check_alive insists on a pingable server
# check_dead also fails if there is a lost mysqld in the process list
#
# Usage: boolean mysqld_status [check_alive|check_dead] [warn|nowarn]
mysqld_status () {
    ping_output=`$MYADMIN ping 2>&1`; ping_alive=$(( ! $? ))
    
    ps_alive=0
    pidfile=`mysqld_get_param pid-file`
    if [ -f "$pidfile" ]; then
        if ps `cat $pidfile` >/dev/null 2>&1; then ps_alive=1; fi
    fi
    
    if [ "$1" = "check_alive"  -a  $ping_alive = 1 ] ||
       [ "$1" = "check_dead"   -a  $ping_alive = 0  -a  $ps_alive = 0 ]; then
	return 0 # EXIT_SUCCESS
    else
  	if [ "$2" = "warn" ]; then
  	    /bin/echo -e "$ps_alive processes alive and '$MYADMIN ping' resulted in\n$ping_output\n" | $ERR_LOGGER -p daemon.debug
	fi
  	return 1 # EXIT_FAILURE
    fi
}

#
# main()
#

case "${1:-''}" in
  'start')
	# check for config file
	if [ ! -r $CONF ]; then
	  /bin/echo -e "\nWARNING: $CONF cannot be read. See README.Debian."
        fi 
	# check for /var/run/mysqld/ which maybe have only been on a tempfs
	if [ ! -d $RUNDIR ]; then
	  install --directory --owner=mysql --mode=755 $RUNDIR
	fi
	# Start daemon
	echo -n "Starting MySQL database server: mysqld"
	if mysqld_status check_alive nowarn; then
	   echo "...already running."
	else
  	    /usr/bin/mysqld_safe > /dev/null 2>&1 &
	    for i in 1 2 3 4 5 6; do
                sleep 1
	        if mysqld_status check_alive nowarn ; then break; fi
            done
	    if mysqld_status check_alive warn; then
                echo "."
	        # Now start mysqlcheck or whatever the admin wants.
	        /etc/mysql/debian-start
	    else
	        echo "...failed."
	        /bin/echo -e "\tPlease take a look at the syslog."
	    fi
	fi

	if $MYADMIN variables | egrep -q have_bdb.*YES; then
	    /bin/echo "BerkeleyDB is obsolete, see /usr/share/doc/mysql-server/README.Debian.gz" | $ERR_LOGGER -p daemon.info
	fi

	;;

  'stop')
	# * As a passwordless mysqladmin (e.g. via ~/.my.cnf) must be possible
	# at least for cron, we can rely on it here, too. (although we have 
	# to specify it explicit as e.g. sudo environments points to the normal
	# users home and not /root)
	echo -n "Stopping MySQL database server: mysqld"	
	if ! mysqld_status check_dead nowarn; then
	  set +e
	  shutdown_out=`$MYADMIN shutdown 2>&1`; r=$?
	  set -e
	  if [ "$r" -ne 0 ]; then
	    /bin/echo -e -n "...failed.\n$shutdown_out\nKilling MySQL database server by signal: mysqld"
	    killall -15 mysqld
            server_down=
	    for i in 1 2 3 4 5 6 7 8 9 10; do
              sleep 1
              if mysqld_status check_dead nowarn; then server_down=1; break; fi
            done
          if test -z "$server_down"; then killall -9 mysqld; fi
	  fi
        fi

        if ! mysqld_status check_dead warn; then
	  echo "...failed."
	  echo "Please stop MySQL manually and read /usr/share/doc/mysql-server/README.Debian!"
	  exit -1
	else
	  echo "."
        fi
	;;

  'restart')
	set +e; $SELF stop; set -e
	$SELF start 
	;;

  'reload'|'force-reload')
  	echo -n "Reloading MySQL database server: mysqld"
	$MYADMIN reload
	echo "."
	;;

  'status')
	if mysqld_status check_alive nowarn; then
	  $MYADMIN version
	else
	  echo "MySQL is stopped."
	fi
  	;;

  *)
	echo "Usage: $SELF start|stop|restart|reload|force-reload"
	exit 1
	;;
esac