Blame SOURCES/mysql.init.in

f55c7d
#!/bin/sh
f55c7d
#
f55c7d
# @DAEMON_NAME@	This shell script takes care of starting and stopping
f55c7d
#		the MySQL subsystem (mysqld).
f55c7d
#
f55c7d
# chkconfig: - 64 36
f55c7d
# description:	MySQL database server.
f55c7d
# processname: mysqld
f55c7d
# config: @sysconfdir@/my.cnf
f55c7d
# pidfile: /var/run/@DAEMON_NAME@/@DAEMON_NO_PREFIX@.pid
f55c7d
### BEGIN INIT INFO
f55c7d
# Provides: mysqld
f55c7d
# Required-Start: $local_fs $remote_fs $network $named $syslog $time
f55c7d
# Required-Stop: $local_fs $remote_fs $network $named $syslog $time
f55c7d
# Short-Description: start and stop MySQL server
f55c7d
# Description: MySQL database server
f55c7d
### END INIT INFO
f55c7d
f55c7d
# Source function library.
f55c7d
. /etc/rc.d/init.d/functions
f55c7d
f55c7d
# Source networking configuration.
f55c7d
. /etc/sysconfig/network
f55c7d
f55c7d
f55c7d
exec="@bindir@/mysqld_safe"
f55c7d
prog="@DAEMON_NAME@"
f55c7d
f55c7d
# Set timeouts here so they can be overridden from @sysconfdir@/sysconfig/@DAEMON_NO_PREFIX@
f55c7d
STARTTIMEOUT=300
f55c7d
STOPTIMEOUT=60
f55c7d
f55c7d
# User and group the daemon will run under
f55c7d
MYUSER=mysql
f55c7d
MYGROUP=mysql
f55c7d
f55c7d
# Edit the following file in order to re-write some of the environment
f55c7d
# variables defined above, like $STARTTIMEOUT, $STOPTIMEOUT, $exec
f55c7d
[ -e @sysconfdir@/sysconfig/@DAEMON_NO_PREFIX@ ] && . @sysconfdir@/sysconfig/@DAEMON_NO_PREFIX@
f55c7d
f55c7d
lockfile=/var/lock/subsys/$prog
f55c7d
f55c7d
# get options from my.cnf
f55c7d
source "@libexecdir@/mysql-scripts-common"
f55c7d
f55c7d
start(){
f55c7d
    [ -x $exec ] || exit 5
f55c7d
f55c7d
    # check permissions
f55c7d
    if ! touch $(dirname $socketfile) &>/dev/null ; then
f55c7d
        action $"Starting $prog: " /bin/false
f55c7d
        return 4
f55c7d
    fi
f55c7d
9c9e62
    # Check if PID file exists (which should indicate, the daemon is already running)
f55c7d
    MYSQLDRUNNING=0
f55c7d
    if [ -f "$pidfile" ]; then
9c9e62
	MYSQLPID=`su - $MYUSER -s /bin/bash -c "cat $pidfile 2>/dev/null"`
f55c7d
	if [ -n "$MYSQLPID" ] && [ -d "/proc/$MYSQLPID" ] ; then
9c9e62
	    # Test that the read data is an integer
9c9e62
	    if [ 1 -ne `echo $MYSQLPID | grep -c -E "^[0-9]+$"` ] ; then
9c9e62
	        # Set an error value
9c9e62
	        MYSQLDRUNNING=2
9c9e62
	    else
9c9e62
	        MYSQLDRUNNING=1
9c9e62
	    fi
f55c7d
	fi
f55c7d
    fi
9c9e62
9c9e62
    # Ping the socket to check, if there's already a running server on that socket
f55c7d
    RESPONSE=`@bindir@/mysqladmin --no-defaults --socket="$socketfile" --user=UNKNOWN_MYSQL_USER ping 2>&1`
9c9e62
    if [ $? = 0 ] || echo "$RESPONSE" | grep -q "Access denied for user"
9c9e62
        then
9c9e62
        if [ $MYSQLDRUNNING = 0 ]; then
9c9e62
            # PID file does not exist, but the server is responsive = different server, or server with different configuration, is running, fail to prevent damage
9c9e62
            echo "Socket already in use by a different, responding server" >&2
9c9e62
	    action $"Starting $prog: " /bin/false
9c9e62
	    return 1
9c9e62
        elif [ $MYSQLDRUNNING = 1 ]; then
9c9e62
            # PID file is valid and server is responsive = already running, do nothing
9c9e62
	    action $"Starting $prog: " /bin/true
9c9e62
	    ret=0
9c9e62
        elif [ $MYSQLDRUNNING = 2 ]; then
9c9e62
            # PID file contains garbage, but the server is responsive. Notify admin and fail.
9c9e62
            echo "Socket already in use by a different, responding server" >&2
9c9e62
            echo "PID file does not contain an integer. Please investigate." >&2
9c9e62
	    action $"Starting $prog: " /bin/false
9c9e62
	    return 1
9c9e62
        fi
f55c7d
    else
9c9e62
        if [ $MYSQLDRUNNING = 1 ]; then
9c9e62
            # If the PID file exist by any reason, but there is no response on the socket, notify the admin and fail.
9c9e62
            # The PID might be in use by a server which configured different socket
9c9e62
            echo "PID file already exists. It might belong to a living process. Please investigate." >&2
9c9e62
	    action $"Starting $prog: " /bin/false
9c9e62
	    return 1
9c9e62
        elif [ $MYSQLDRUNNING = 2 ]; then
9c9e62
            # If the PID file exist, but contains garbage, and there is no response on the socket, notify the admin and fail.
9c9e62
            echo "PID file already exists. However it does not contain an integer. Please investigate." >&2
9c9e62
	    action $"Starting $prog: " /bin/false
9c9e62
	    return 1
9c9e62
        fi
9c9e62
f55c7d
        @libexecdir@/mysql-check-socket || return 1
9c9e62
        # KNOWN ISSUE: Per standard, following line should return '1' on error
9c9e62
        #              Leaving '4' for behaviour compatibility with released versions of this script
f55c7d
        su - $MYUSER -s /bin/bash -c "@libexecdir@/mysql-prepare-db-dir $MYUSER $MYGROUP" || return 4
f55c7d
f55c7d
	# Pass all the options determined above, to ensure consistent behavior.
f55c7d
	# In many cases mysqld_safe would arrive at the same conclusions anyway
f55c7d
	# but we need to be sure.  (An exception is that we don't force the
f55c7d
	# log-error setting, since this script doesn't really depend on that,
f55c7d
	# and some users might prefer to configure logging to syslog.)
f55c7d
	# Note: set --basedir to prevent probes that might trigger SELinux
f55c7d
	# alarms, per bug #547485
f55c7d
	su - $MYUSER -s /bin/bash -c "$exec   --datadir='$datadir' --socket='$socketfile' \
f55c7d
		--pid-file='$pidfile' $MYSQLD_OPTS $_WSREP_NEW_CLUSTER \
f55c7d
		--basedir=@prefix@ --user=$MYUSER" >/dev/null 2>&1 &
f55c7d
	safe_pid=$!
f55c7d
f55c7d
	# Wait until the daemon is up
f55c7d
	su - $MYUSER -s /bin/bash -c "@libexecdir@/mysql-wait-ready '$safe_pid'"
f55c7d
	ret=$?
f55c7d
f55c7d
	if [ $ret -eq 0 ]; then
f55c7d
	    action $"Starting $prog: " /bin/true
f55c7d
	    chmod o+r $pidfile >/dev/null 2>&1
f55c7d
	    touch $lockfile
f55c7d
	else
f55c7d
	    action $"Starting $prog: " /bin/false
f55c7d
	fi
f55c7d
    fi
f55c7d
    return $ret
f55c7d
}
f55c7d
f55c7d
stop(){
f55c7d
	if [ ! -f "$pidfile" ]; then
f55c7d
	    # not running; per LSB standards this is "ok"
f55c7d
	    action $"Stopping $prog: " /bin/true
f55c7d
	    return 0
f55c7d
	fi
9c9e62
9c9e62
	MYSQLPID=`su - $MYUSER -s /bin/bash -c "cat $pidfile 2>/dev/null"`
9c9e62
9c9e62
	# Test that the read data is an integer
9c9e62
	if [ 1 -ne `echo $MYSQLPID | grep -c -E "^[0-9]+$"` ] ; then
9c9e62
	    echo "PIDfile does not contain a number. Please investigate." >&2
9c9e62
	    action $"Stopping $prog: " /bin/false
9c9e62
	    return 1
9c9e62
	fi
9c9e62
f55c7d
	if [ -n "$MYSQLPID" ]; then
f55c7d
	    if ! [ -d "/proc/$MYSQLPID" ] ; then
f55c7d
		# process doesn't run anymore
f55c7d
		action $"Stopping $prog: " /bin/true
f55c7d
		return 0
f55c7d
	    fi
9c9e62
	    # Drop priviledges before calling kill
9c9e62
	    su - $MYUSER -s /bin/bash -c "/bin/kill $MYSQLPID" >/dev/null 2>&1
f55c7d
	    ret=$?
f55c7d
	    if [ $ret -eq 0 ]; then
f55c7d
		TIMEOUT="$STOPTIMEOUT"
f55c7d
		while [ $TIMEOUT -gt 0 ]; do
9c9e62
		    # Drop priviledges before calling kill
9c9e62
		    su - $MYUSER -s /bin/bash -c "/bin/kill -0 $MYSQLPID" >/dev/null 2>&1 || break
f55c7d
		    sleep 1
f55c7d
		    let TIMEOUT=${TIMEOUT}-1
f55c7d
		done
f55c7d
		if [ $TIMEOUT -eq 0 ]; then
f55c7d
		    echo "Timeout error occurred trying to stop MySQL Daemon."
f55c7d
		    ret=1
f55c7d
		    action $"Stopping $prog: " /bin/false
f55c7d
		else
f55c7d
		    rm -f $lockfile
f55c7d
		    rm -f "$socketfile"
f55c7d
		    action $"Stopping $prog: " /bin/true
f55c7d
		fi
f55c7d
	    else
f55c7d
		# kill command failed, probably insufficient permissions
f55c7d
		action $"Stopping $prog: " /bin/false
f55c7d
		ret=4
f55c7d
	    fi
f55c7d
	else
f55c7d
	    # failed to read pidfile, probably insufficient permissions
f55c7d
	    action $"Stopping $prog: " /bin/false
f55c7d
	    ret=4
f55c7d
	fi
f55c7d
	return $ret
f55c7d
}
f55c7d
 
f55c7d
restart(){
f55c7d
    stop
f55c7d
    start
f55c7d
}
f55c7d
f55c7d
condrestart(){
f55c7d
    [ -e $lockfile ] && restart || :
f55c7d
}
f55c7d
f55c7d
f55c7d
# See how we were called.
f55c7d
case "$1" in
f55c7d
  start)
f55c7d
    start
f55c7d
    ;;
f55c7d
  stop)
f55c7d
    stop
f55c7d
    ;;
f55c7d
  status)
f55c7d
    status -p "$pidfile" $prog
f55c7d
    ;;
f55c7d
  restart)
f55c7d
    restart
f55c7d
    ;;
f55c7d
  condrestart|try-restart)
f55c7d
    condrestart
f55c7d
    ;;
f55c7d
  reload)
f55c7d
    exit 3
f55c7d
    ;;
f55c7d
  force-reload)
f55c7d
    restart
f55c7d
    ;;
f55c7d
  *)
f55c7d
    echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
f55c7d
    exit 2
f55c7d
esac
f55c7d
f55c7d
exit $?