4b7078
#!/bin/sh
4b7078
#
4b7078
# @DAEMON_NAME@	This shell script takes care of starting and stopping
4b7078
#		the MySQL subsystem (mysqld).
4b7078
#
4b7078
# chkconfig: - 64 36
4b7078
# description:	MySQL database server.
4b7078
# processname: mysqld
4b7078
# config: @sysconfdir@/my.cnf
4b7078
# pidfile: /var/run/@DAEMON_NAME@/@DAEMON_NAME@.pid
4b7078
### BEGIN INIT INFO
4b7078
# Provides: mysqld
4b7078
# Required-Start: $local_fs $remote_fs $network $named $syslog $time
4b7078
# Required-Stop: $local_fs $remote_fs $network $named $syslog $time
4b7078
# Short-Description: start and stop MySQL server
4b7078
# Description: MySQL database server
4b7078
### END INIT INFO
4b7078
4b7078
# Source function library.
4b7078
. /etc/rc.d/init.d/functions
4b7078
4b7078
# Source networking configuration.
4b7078
. /etc/sysconfig/network
4b7078
4b7078
4b7078
exec="@bindir@/mysqld_safe"
4b7078
prog="@DAEMON_NAME@"
4b7078
4b7078
# Set timeouts here so they can be overridden from /etc/sysconfig/@DAEMON_NAME@
4b7078
STARTTIMEOUT=300
4b7078
STOPTIMEOUT=60
4b7078
4b7078
# User and group the daemon will run under
4b7078
MYUSER=mysql
4b7078
MYGROUP=mysql
4b7078
4b7078
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
4b7078
4b7078
lockfile=/var/lock/subsys/$prog
4b7078
4b7078
# get options from my.cnf
4b7078
source "@libexecdir@/mysql-scripts-common"
4b7078
4b7078
start(){
4b7078
    [ -x $exec ] || exit 5
4b7078
    # check to see if it's already running
4b7078
    MYSQLDRUNNING=0
4b7078
    if [ -f "$pidfile" ]; then
4b7078
	MYSQLPID=`cat "$pidfile" 2>/dev/null`
4b7078
	if [ -n "$MYSQLPID" ] && [ -d "/proc/$MYSQLPID" ] ; then
4b7078
	    MYSQLDRUNNING=1
4b7078
	fi
4b7078
    fi
4b7078
    RESPONSE=`@bindir@/mysqladmin --socket="$socketfile" --user=UNKNOWN_MYSQL_USER ping 2>&1`
4b7078
    if [ $MYSQLDRUNNING = 1 ] && [ $? = 0 ]; then
4b7078
	# already running, do nothing
4b7078
	action $"Starting $prog: " /bin/true
4b7078
	ret=0
4b7078
    elif [ $MYSQLDRUNNING = 1 ] && echo "$RESPONSE" | grep -q "Access denied for user"
4b7078
    then
4b7078
	# already running, do nothing
4b7078
	action $"Starting $prog: " /bin/true
4b7078
	ret=0
4b7078
    else
4b7078
        @libexecdir@/mysql-prepare-db-dir $MYUSER $MYGROUP || return 4
4b7078
        @libexecdir@/mysql-check-socket || return 1
4b7078
4b7078
	# Pass all the options determined above, to ensure consistent behavior.
4b7078
	# In many cases mysqld_safe would arrive at the same conclusions anyway
4b7078
	# but we need to be sure.  (An exception is that we don't force the
4b7078
	# log-error setting, since this script doesn't really depend on that,
4b7078
	# and some users might prefer to configure logging to syslog.)
4b7078
	# Note: set --basedir to prevent probes that might trigger SELinux
4b7078
	# alarms, per bug #547485
4b7078
	$exec   --datadir="$datadir" --socket="$socketfile" \
4b7078
		--pid-file="$pidfile" \
4b7078
		--basedir=@prefix@ --user=$MYUSER >/dev/null 2>&1 &
4b7078
	safe_pid=$!
4b7078
4b7078
	# Wait until the daemon is up
4b7078
        @libexecdir@/mysql-wait-ready "$safe_pid"
4b7078
        ret=$?
4b7078
4b7078
	if [ $ret -eq 0 ]; then
4b7078
	    action $"Starting $prog: " /bin/true
4b7078
	    chmod o+r $pidfile >/dev/null 2>&1
4b7078
	    touch $lockfile
4b7078
	else
4b7078
	    action $"Starting $prog: " /bin/false
4b7078
	fi
4b7078
    fi
4b7078
    return $ret
4b7078
}
4b7078
4b7078
stop(){
4b7078
	if [ ! -f "$pidfile" ]; then
4b7078
	    # not running; per LSB standards this is "ok"
4b7078
	    action $"Stopping $prog: " /bin/true
4b7078
	    return 0
4b7078
	fi
4b7078
	MYSQLPID=`cat "$pidfile" 2>/dev/null`
4b7078
	if [ -n "$MYSQLPID" ]; then
4b7078
	    if ! [ -d "/proc/$MYSQLPID" ] ; then
4b7078
		# process doesn't run anymore
4b7078
		action $"Stopping $prog: " /bin/true
4b7078
		return 0
4b7078
	    fi
4b7078
	    /bin/kill "$MYSQLPID" >/dev/null 2>&1
4b7078
	    ret=$?
4b7078
	    if [ $ret -eq 0 ]; then
4b7078
		TIMEOUT="$STOPTIMEOUT"
4b7078
		while [ $TIMEOUT -gt 0 ]; do
4b7078
		    /bin/kill -0 "$MYSQLPID" >/dev/null 2>&1 || break
4b7078
		    sleep 1
4b7078
		    let TIMEOUT=${TIMEOUT}-1
4b7078
		done
4b7078
		if [ $TIMEOUT -eq 0 ]; then
4b7078
		    echo "Timeout error occurred trying to stop MySQL Daemon."
4b7078
		    ret=1
4b7078
		    action $"Stopping $prog: " /bin/false
4b7078
		else
4b7078
		    rm -f $lockfile
4b7078
		    rm -f "$socketfile"
4b7078
		    action $"Stopping $prog: " /bin/true
4b7078
		fi
4b7078
	    else
4b7078
		# kill command failed, probably insufficient permissions
4b7078
		action $"Stopping $prog: " /bin/false
4b7078
		ret=4
4b7078
	    fi
4b7078
	else
4b7078
	    # failed to read pidfile, probably insufficient permissions
4b7078
	    action $"Stopping $prog: " /bin/false
4b7078
	    ret=4
4b7078
	fi
4b7078
	return $ret
4b7078
}
4b7078
 
4b7078
restart(){
4b7078
    stop
4b7078
    start
4b7078
}
4b7078
4b7078
condrestart(){
4b7078
    [ -e $lockfile ] && restart || :
4b7078
}
4b7078
4b7078
4b7078
# See how we were called.
4b7078
case "$1" in
4b7078
  start)
4b7078
    start
4b7078
    ;;
4b7078
  stop)
4b7078
    stop
4b7078
    ;;
4b7078
  status)
4b7078
    status -p "$pidfile" $prog
4b7078
    ;;
4b7078
  restart)
4b7078
    restart
4b7078
    ;;
4b7078
  condrestart|try-restart)
4b7078
    condrestart
4b7078
    ;;
4b7078
  reload)
4b7078
    exit 3
4b7078
    ;;
4b7078
  force-reload)
4b7078
    restart
4b7078
    ;;
4b7078
  *)
4b7078
    echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
4b7078
    exit 2
4b7078
esac
4b7078
4b7078
exit $?