summaryrefslogtreecommitdiff
path: root/packaging/rpm-sles/mysql.init
blob: 25762d9bee263fc8a68fa160d534705603a0571e (plain)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#! /bin/bash
#

# MySQL Database start/stop script

# chkconfig:   - 64 36
# description: MySQL Database
# processname: mysqld
# config:      /etc/my.cnf
# pidfile:     /var/run/mysql/mysqld.pid

# Comments to support LSB init script conventions
### BEGIN INIT INFO
# Provides:          mysql
# Required-Start:    $network $remote_fs
# Required-Stop:     $network $remote_fs
# Default-Start:     3 5
# Default-Stop:      0 1 6
# Short-Description: MySQL Database
# Description:       MySQL Database
### END INIT INFO

# 
# https://en.opensuse.org/openSUSE:Packaging_init_scripts#Exit_Status_Codes
# 

[ -e /etc/rc.status ] && . /etc/rc.status

rc_reset

STARTTIMEOUT=180
STOPTIMEOUT=60
PROG=/usr/bin/mysqld_safe

[ -e /etc/sysconfig/mysql ] && . /etc/sysconfig/mysql

# Lock directory
lockfile=/var/lock/subsys/mysql

get_option () {
    local section=$1
    local option=$2
    local default=$3
    ret=$(/usr/bin/my_print_defaults $section | grep '^--'${option}'=' | cut -d= -f2-)
    [ -z $ret ] && ret=$default
    echo $ret
}

datadir=$(get_option mysqld datadir "/var/lib/mysql")
socket=$(get_option  mysqld socket "$datadir/mysql.sock")
pidfile=$(get_option mysqld_safe pid-file "/var/run/mysql/mysqld.pid")

install_db () {
    # Note: something different than datadir=/var/lib/mysql requires 
    # SELinux policy changes (in enforcing mode)
    datadir=$(get_option mysqld datadir "/var/lib/mysql")
    logfile=$(get_option mysqld_safe log-error "/var/log/mysql/mysqld.log")

    # Restore log, dir, perms and SELinux contexts
    if [ ! -d "$datadir" -a ! -h "$datadir" -a "x$(basedir "$datadir")" = "x/var/lib" ]; then
	install -d -m 0755 -omysql -gmysql "$datadir" || return 1
    fi

    if [ ! -e "$logfile" -a ! -h "$logfile" -a "x$(dirname "$logfile")" = "x/var/log/mysql" ]; then
	install /dev/null -omysql -gmysql "$logfile" || return 1
    fi
    if [ -x /usr/sbin/restorecon ]; then
        /usr/sbin/restorecon "$datadir"
        /usr/sbin/restorecon "$logfile"
    fi

    # If special mysql dir is in place, skip db install 
    [ -d "$datadir/mysql" ] && return 0

    # Create initial db
    /usr/bin/mysql_install_db --datadir="$datadir" --rpm --user=mysql
    return $?
}

# Wait for ping to answer to signal startup completed,
# might take a while in case of e.g. crash recovery
pinger () {
    mysqld_safe_pid=$1
    timer=$STARTTIMEOUT
    ret=0
    while [ $timer -gt 0 ]; do
        sleep 1
        mysqladmin --no-defaults --socket="$socket" ping >/dev/null 2>&1 && break
	timer=$(expr $timer - 1)

	# Check if mysqld_safe is still alive, if not there is no hope
	if ! kill -0 $mysqld_safe_pid >/dev/null 2>&1 ; then
	    ret=1
	    break
	fi
    done
    
    # Did we timeout?
    if [ $timer = 0 ]; then
	echo "MySQL Database start up timeout after ${STARTTIMEOUT}s"
	ret=1
    fi
    return $ret
}

# Check if mysqld is running
chk_running () {
    ret=0
    if [ -e "$pidfile" ]; then
	pid=$(cat "$pidfile") || ret=4
    else
	ret=7
    fi

    # Check if $pid is a mysqld pid
    if [ $ret -eq 0 ]; then
	[ -L "/proc/$pid/exe" ] || ret=7 
    fi

    if [ $ret -eq 0 ]; then
	exec=$(readlink "/proc/$pid/exe") || ret=7
    fi

    if [ $ret -eq 0 ]; then
	[ "x$(basename $exec)" = "xmysqld" ] || ret=7
    fi
    return $ret
}

start () {
    if chk_running && mysqladmin --no-defaults --socket="$socket" ping >/dev/null 2>&1 ; then
	echo -n "Starting service MySQL:"
	rc_reset ; rc_status -v ; rc_exit
    fi

    if ! install_db; then
	echo -n "MySQL Database could not initialize data directory:"
	rc_failed 6 ; rc_status -v ; rc_exit
    fi

    $PROG --basedir=/usr --datadir="$datadir" --pid-file="$pidfile" >/dev/null &
    if pinger $! ; then
	echo -n "Starting service MySQL:"
	touch $lockfile
	rc_reset
    else
	echo -n "Failed to start service MySQL:"
	rc_failed 3
    fi
    rc_status -v
}

stop () {
    chk_running
    ret=$?
    if [ $ret -ne 0 ]; then
	echo -n "Shutting down service MySQL:"
	rc_reset ; rc_status -v ; return 0
    fi

    # chk_running has verified this works
    pid=$(cat "$pidfile")

    # We use a signal to avoid having to know the root password
    # Send single kill command and then wait
    if kill $pid >/dev/null 2>&1; then
	timer=$STOPTIMEOUT
	while [ $timer -gt 0 ]; do
            kill -0 $pid >/dev/null 2>&1 || break
            sleep 1
            timer=$(expr $timer - 1)
	done
    else
	echo -n "Shutting down service MySQL:"
	rc_failed 4 ; rc_status -v ; rc_exit
    fi
    
    if [ $timer -eq 0 ]; then
	echo -n "Failed to stop service MySQL:"
	rc_failed 1
    else
	rm -f $lockfile
	rm -f "$socketfile"
	echo -n "Shutting down service MySQL:"
	rc_reset
    fi
    rc_status -v
} 

restart () {
    stop
    start
}

reload () {
    ret=0
    if chk_running && mysqladmin --no-defaults --socket="$socket" ping >/dev/null 2>&1 ; then
	pid=$(cat "$pidfile")
	kill -HUP $pid >/dev/null 2>&1
	echo -n "Reloading service MySQL:"
	rc_reset
    else
	echo -n "Reloading of service MySQL failed:"
        rc_failed 7
    fi
    rc_status -v
}

condrestart () {
    if chk_running && mysqladmin --no-defaults --socket="$socket" ping >/dev/null 2>&1 ; then
	restart
    fi
}

status () {
   echo -n "Checking for service MySQL:"
   checkproc mysqld
   rc_status -v
}  

case "$1" in
    start  ) start   ;;
    stop   ) stop    ;;
    restart) restart ;;
    status ) status  ;;
    condrestart        ) condrestart ;;
    reload|force-reload) reload ;;

    *) echo $"Usage: $0 {start|stop|restart|condrestart|status|reload|force-reload}"; exit 1 ;;
esac

rc_exit