Subversion Repositories configs

Rev

Rev 4 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 - 1
#!/bin/sh
2
#
3
# mysqld	This shell script takes care of starting and stopping
4
#		the MySQL subsystem (mysqld).
5
#
6
# chkconfig: - 64 36
7
# description:	MySQL database server.
8
# processname: mysqld
9
# config: /etc/my.cnf
10
# pidfile: /var/run/mysqld/mysqld.pid
11
### BEGIN INIT INFO
12
# Provides: mysqld
13
# Required-Start: $local_fs $remote_fs $network $named $syslog $time
14
# Required-Stop: $local_fs $remote_fs $network $named $syslog $time
15
# Short-Description: start and stop MySQL server
16
# Description: MySQL database server
17
### END INIT INFO
18
 
19
# Source function library.
20
. /etc/rc.d/init.d/functions
21
 
22
# Source networking configuration.
23
. /etc/sysconfig/network
24
 
25
 
26
exec="/usr/bin/mysqld_safe"
27
prog="mysqld"
28
 
29
# Set timeouts here so they can be overridden from /etc/sysconfig/mysqld
30
STARTTIMEOUT=120
31
STOPTIMEOUT=60
32
 
33
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
34
 
35
lockfile=/var/lock/subsys/$prog
36
 
37
 
38
# extract value of a MySQL option from config files
39
# Usage: get_mysql_option SECTION VARNAME DEFAULT
40
# result is returned in $result
41
# We use my_print_defaults which prints all options from multiple files,
42
# with the more specific ones later; hence take the last match.
43
get_mysql_option(){
44
	result=`/usr/bin/my_print_defaults "$1" | sed -n "s/^--$2=//p" | tail -n 1`
45
	if [ -z "$result" ]; then
46
	    # not found, use default
47
	    result="$3"
48
	fi
49
}
50
 
51
get_mysql_option mysqld datadir "/var/lib/mysql"
52
datadir="$result"
53
get_mysql_option mysqld socket "$datadir/mysql.sock"
54
socketfile="$result"
55
get_mysql_option mysqld_safe log-error "/var/log/mysqld.log"
56
errlogfile="$result"
57
get_mysql_option mysqld_safe pid-file "/var/run/mysqld/mysqld.pid"
58
mypidfile="$result"
59
 
60
 
61
start(){
62
    [ -x $exec ] || exit 5
63
    # check to see if it's already running
154 - 64
    MYSQLDRUNNING=0
65
    if [ -f "$mypidfile" ]; then
66
	MYSQLPID=`cat "$mypidfile" 2>/dev/null`
67
	if [ -n "$MYSQLPID" ] && [ -d "/proc/$MYSQLPID" ] ; then
68
	    MYSQLDRUNNING=1
69
	fi
70
    fi
4 - 71
    RESPONSE=`/usr/bin/mysqladmin --socket="$socketfile" --user=UNKNOWN_MYSQL_USER ping 2>&1`
154 - 72
    if [ $MYSQLDRUNNING = 1 ] && [ $? = 0 ]; then
4 - 73
	# already running, do nothing
74
	action $"Starting $prog: " /bin/true
75
	ret=0
154 - 76
    elif [ $MYSQLDRUNNING = 1 ] && echo "$RESPONSE" | grep -q "Access denied for user"
4 - 77
    then
78
	# already running, do nothing
79
	action $"Starting $prog: " /bin/true
80
	ret=0
81
    else
82
    	# prepare for start
154 - 83
	touch "$errlogfile" 2>/dev/null
84
	if [ $? -ne 0 ]; then
85
	     # failed to touch log file, probably insufficient permissions
86
	    action $"Starting $prog: " /bin/false
87
	    return 4
88
	fi
4 - 89
	chown mysql:mysql "$errlogfile"
90
	chmod 0640 "$errlogfile"
91
	[ -x /sbin/restorecon ] && /sbin/restorecon "$errlogfile"
92
	if [ ! -d "$datadir/mysql" ] ; then
93
	    # First, make sure $datadir is there with correct permissions
94
	    if [ ! -e "$datadir" -a ! -h "$datadir" ]
95
	    then
96
		mkdir -p "$datadir" || exit 1
97
	    fi
98
	    chown mysql:mysql "$datadir"
99
	    chmod 0755 "$datadir"
100
	    [ -x /sbin/restorecon ] && /sbin/restorecon "$datadir"
101
	    # Now create the database
102
	    action $"Initializing MySQL database: " /usr/bin/mysql_install_db --datadir="$datadir" --user=mysql
103
	    ret=$?
104
	    chown -R mysql:mysql "$datadir"
105
	    if [ $ret -ne 0 ] ; then
106
		return $ret
107
	    fi
108
	fi
109
	chown mysql:mysql "$datadir"
110
	chmod 0755 "$datadir"
154 - 111
	# We check if there is already a process using the socket file,
112
	# since otherwise this init script could report false positive
113
	# result and mysqld_safe would remove the socket file, which
114
	# actually uses a different daemon.
115
	if fuser "$socketfile" &>/dev/null ; then
116
	    echo "Socket file $socketfile exists. Is another MySQL daemon already running with the same unix socket?"
117
	    action $"Starting $prog: " /bin/false
118
	    return 1
4 - 119
	fi
120
	# Pass all the options determined above, to ensure consistent behavior.
121
	# In many cases mysqld_safe would arrive at the same conclusions anyway
122
	# but we need to be sure.  (An exception is that we don't force the
123
	# log-error setting, since this script doesn't really depend on that,
124
	# and some users might prefer to configure logging to syslog.)
125
	# Note: set --basedir to prevent probes that might trigger SELinux
126
	# alarms, per bug #547485
127
	$exec   --datadir="$datadir" --socket="$socketfile" \
128
		--pid-file="$mypidfile" \
129
		--basedir=/usr --user=mysql >/dev/null 2>&1 &
130
	safe_pid=$!
131
	# Spin for a maximum of N seconds waiting for the server to come up;
132
	# exit the loop immediately if mysqld_safe process disappears.
133
	# Rather than assuming we know a valid username, accept an "access
134
	# denied" response as meaning the server is functioning.
135
	ret=0
136
	TIMEOUT="$STARTTIMEOUT"
137
	while [ $TIMEOUT -gt 0 ]; do
138
	    RESPONSE=`/usr/bin/mysqladmin --socket="$socketfile" --user=UNKNOWN_MYSQL_USER ping 2>&1`
139
	    mret=$?
140
	    if [ $mret -eq 0 ]; then
141
		break
142
	    fi
143
	    # exit codes 1, 11 (EXIT_CANNOT_CONNECT_TO_SERVICE) are expected,
144
	    # anything else suggests a configuration error
145
	    if [ $mret -ne 1 -a $mret -ne 11 ]; then
146
		echo "$RESPONSE"
147
		echo "Cannot check for MySQL Daemon startup because of mysqladmin failure."
148
		ret=1
149
		break
150
	    fi
151
	    echo "$RESPONSE" | grep -q "Access denied for user" && break
152
	    if ! /bin/kill -0 $safe_pid 2>/dev/null; then
153
		echo "MySQL Daemon failed to start."
154
		ret=1
155
		break
156
	    fi
157
	    sleep 1
158
	    let TIMEOUT=${TIMEOUT}-1
159
	done
160
	if [ $TIMEOUT -eq 0 ]; then
161
	    echo "Timeout error occurred trying to start MySQL Daemon."
162
	    ret=1
163
	fi
164
	if [ $ret -eq 0 ]; then
165
	    action $"Starting $prog: " /bin/true
154 - 166
	    chmod o+r $mypidfile >/dev/null 2>&1
4 - 167
	    touch $lockfile
168
	else
169
	    action $"Starting $prog: " /bin/false
170
	fi
171
    fi
172
    return $ret
173
}
174
 
175
stop(){
176
	if [ ! -f "$mypidfile" ]; then
177
	    # not running; per LSB standards this is "ok"
178
	    action $"Stopping $prog: " /bin/true
179
	    return 0
180
	fi
154 - 181
	MYSQLPID=`cat "$mypidfile" 2>/dev/null`
4 - 182
	if [ -n "$MYSQLPID" ]; then
183
	    /bin/kill "$MYSQLPID" >/dev/null 2>&1
184
	    ret=$?
185
	    if [ $ret -eq 0 ]; then
186
		TIMEOUT="$STOPTIMEOUT"
187
		while [ $TIMEOUT -gt 0 ]; do
188
		    /bin/kill -0 "$MYSQLPID" >/dev/null 2>&1 || break
189
		    sleep 1
190
		    let TIMEOUT=${TIMEOUT}-1
191
		done
192
		if [ $TIMEOUT -eq 0 ]; then
193
		    echo "Timeout error occurred trying to stop MySQL Daemon."
194
		    ret=1
195
		    action $"Stopping $prog: " /bin/false
196
		else
197
		    rm -f $lockfile
198
		    rm -f "$socketfile"
199
		    action $"Stopping $prog: " /bin/true
200
		fi
201
	    else
202
		action $"Stopping $prog: " /bin/false
203
	    fi
204
	else
205
	    # failed to read pidfile, probably insufficient permissions
206
	    action $"Stopping $prog: " /bin/false
207
	    ret=4
208
	fi
209
	return $ret
210
}
211
 
212
restart(){
213
    stop
214
    start
215
}
216
 
217
condrestart(){
218
    [ -e $lockfile ] && restart || :
219
}
220
 
221
 
222
# See how we were called.
223
case "$1" in
224
  start)
225
    start
226
    ;;
227
  stop)
228
    stop
229
    ;;
230
  status)
154 - 231
    status -p "$mypidfile" $prog
4 - 232
    ;;
233
  restart)
234
    restart
235
    ;;
236
  condrestart|try-restart)
237
    condrestart
238
    ;;
239
  reload)
240
    exit 3
241
    ;;
242
  force-reload)
243
    restart
244
    ;;
245
  *)
154 - 246
    echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
4 - 247
    exit 2
248
esac
249
 
250
exit $?