Subversion Repositories configs

Rev

Details | Last modification | View Log | RSS feed

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