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