Prevent package removal during upgrade of libc6 and dpkg.
[carnet-upgrade.git] / files / etc / init.d / mysql
1 #!/bin/bash
2 #
3 # MySQL daemon start/stop script.
4 #
5 # Debian version. Based on the original by TcX.
6 #
7 set -e
8 set -u
9 ${DEBIAN_SCRIPT_DEBUG:+ set -v -x}
10
11 test -x /usr/sbin/mysqld || exit 0
12
13 SELF=$(cd $(dirname $0); pwd -P)/$(basename $0)
14 CONF=/etc/mysql/my.cnf
15 MYADMIN="/usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf"
16 RUNDIR=/var/run/mysqld/
17
18 # priority can be overriden and "-s" adds output to stderr
19 ERR_LOGGER="logger -p daemon.err -t /etc/init.d/mysql -i"
20
21 # Safeguard (relative paths, core dumps..)
22 cd /
23 umask 077
24 export PATH=/bin:/usr/bin
25
26 # mysqladmin likes to read /root/.my.cnf. This is usually not what I want
27 # as many admins e.g. only store a password without a username there and
28 # so break my scripts.
29 export HOME=/etc/mysql/
30
31 ## fetch a particular option from mysql's invocation
32 #
33 # usage: void mysqld_get_param option
34 mysqld_get_param() {
35         /usr/sbin/mysqld --print-defaults \
36                 | tr " " "\n" \
37                 | grep -- "--$1" \
38                 | tail -n 1 \
39                 | cut -d= -f2
40 }
41
42 ## Checks if there is a server running and if so if it is accessible.
43 #
44 # check_alive insists on a pingable server
45 # check_dead also fails if there is a lost mysqld in the process list
46 #
47 # Usage: boolean mysqld_status [check_alive|check_dead] [warn|nowarn]
48 mysqld_status () {
49     ping_output=`$MYADMIN ping 2>&1`; ping_alive=$(( ! $? ))
50     
51     ps_alive=0
52     pidfile=`mysqld_get_param pid-file`
53     if [ -f "$pidfile" ]; then
54         if ps `cat $pidfile` >/dev/null 2>&1; then ps_alive=1; fi
55     fi
56     
57     if [ "$1" = "check_alive"  -a  $ping_alive = 1 ] ||
58        [ "$1" = "check_dead"   -a  $ping_alive = 0  -a  $ps_alive = 0 ]; then
59         return 0 # EXIT_SUCCESS
60     else
61         if [ "$2" = "warn" ]; then
62             /bin/echo -e "$ps_alive processes alive and '$MYADMIN ping' resulted in\n$ping_output\n" | $ERR_LOGGER -p daemon.debug
63         fi
64         return 1 # EXIT_FAILURE
65     fi
66 }
67
68 #
69 # main()
70 #
71
72 case "${1:-''}" in
73   'start')
74         # check for config file
75         if [ ! -r $CONF ]; then
76           /bin/echo -e "\nWARNING: $CONF cannot be read. See README.Debian."
77         fi 
78         # check for /var/run/mysqld/ which maybe have only been on a tempfs
79         if [ ! -d $RUNDIR ]; then
80           install --directory --owner=mysql --mode=755 $RUNDIR
81         fi
82         # Start daemon
83         echo -n "Starting MySQL database server: mysqld"
84         if mysqld_status check_alive nowarn; then
85            echo "...already running."
86         else
87             /usr/bin/mysqld_safe > /dev/null 2>&1 &
88             for i in 1 2 3 4 5 6; do
89                 sleep 1
90                 if mysqld_status check_alive nowarn ; then break; fi
91             done
92             if mysqld_status check_alive warn; then
93                 echo "."
94                 # Now start mysqlcheck or whatever the admin wants.
95                 /etc/mysql/debian-start
96             else
97                 echo "...failed."
98                 /bin/echo -e "\tPlease take a look at the syslog."
99             fi
100         fi
101
102         if $MYADMIN variables | egrep -q have_bdb.*YES; then
103             /bin/echo "BerkeleyDB is obsolete, see /usr/share/doc/mysql-server/README.Debian.gz" | $ERR_LOGGER -p daemon.info
104         fi
105
106         ;;
107
108   'stop')
109         # * As a passwordless mysqladmin (e.g. via ~/.my.cnf) must be possible
110         # at least for cron, we can rely on it here, too. (although we have 
111         # to specify it explicit as e.g. sudo environments points to the normal
112         # users home and not /root)
113         echo -n "Stopping MySQL database server: mysqld"        
114         if ! mysqld_status check_dead nowarn; then
115           set +e
116           shutdown_out=`$MYADMIN shutdown 2>&1`; r=$?
117           set -e
118           if [ "$r" -ne 0 ]; then
119             /bin/echo -e -n "...failed.\n$shutdown_out\nKilling MySQL database server by signal: mysqld"
120             killall -15 mysqld
121             server_down=
122             for i in 1 2 3 4 5 6 7 8 9 10; do
123               sleep 1
124               if mysqld_status check_dead nowarn; then server_down=1; break; fi
125             done
126           if test -z "$server_down"; then killall -9 mysqld; fi
127           fi
128         fi
129
130         if ! mysqld_status check_dead warn; then
131           echo "...failed."
132           echo "Please stop MySQL manually and read /usr/share/doc/mysql-server/README.Debian!"
133           exit -1
134         else
135           echo "."
136         fi
137         ;;
138
139   'restart')
140         set +e; $SELF stop; set -e
141         $SELF start 
142         ;;
143
144   'reload'|'force-reload')
145         echo -n "Reloading MySQL database server: mysqld"
146         $MYADMIN reload
147         echo "."
148         ;;
149
150   'status')
151         if mysqld_status check_alive nowarn; then
152           $MYADMIN version
153         else
154           echo "MySQL is stopped."
155         fi
156         ;;
157
158   *)
159         echo "Usage: $SELF start|stop|restart|reload|force-reload"
160         exit 1
161         ;;
162 esac
163