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