#!/bin/sh # MySQL daemon start/stop script. # chkconfig: - 78 12 # description: MySQL database server. # processname: mysqld # config: /etc/my.cnf # pidfile: /var/run/mysqld/mysqld.pid # # Author Tony Smith - Combined redhat mysql 3 init script and the one provided by MySQL prog="MySQL" rundir="/var/run/mysqld" basedir= # Check to see if run directory exists, if not create it if [ ! -d $rundir ]; then mkdir $rundir chown mysql.mysql $rundir fi # The following variables are only set for letting mysql.server find things. # Set some defaults datadir=/var/lib/mysql pid_file= if test -z "$basedir" then basedir=/ bindir=/usr/bin else bindir="$basedir/bin" fi PATH=/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin export PATH mode=$1 # start or stop parse_arguments() { for arg do case "$arg" in --basedir=*) basedir=`echo "$arg" | sed -e 's/^[^=]*=//'` ;; --datadir=*) datadir=`echo "$arg" | sed -e 's/^[^=]*=//'` ;; --pid-file=*) pid_file=`echo "$arg" | sed -e 's/^[^=]*=//'` ;; esac done } # Get arguments from the my.cnf file, # groups [mysqld] [mysql_server] and [mysql.server] if test -x ./bin/my_print_defaults then print_defaults="./bin/my_print_defaults" elif test -x $bindir/my_print_defaults then print_defaults="$bindir/my_print_defaults" elif test -x $bindir/mysql_print_defaults then print_defaults="$bindir/mysql_print_defaults" else # Try to find basedir in /etc/my.cnf conf=/etc/my.cnf print_defaults= if test -r $conf then subpat='^[^=]*basedir[^=]*=\(.*\)$' dirs=`sed -e "/$subpat/!d" -e 's//\1/' $conf` for d in $dirs do d=`echo $d | sed -e 's/[ ]//g'` if test -x "$d/bin/my_print_defaults" then print_defaults="$d/bin/my_print_defaults" break fi if test -x "$d/bin/mysql_print_defaults" then print_defaults="$d/bin/mysql_print_defaults" break fi done fi # Hope it's in the PATH ... but I doubt it test -z "$print_defaults" && print_defaults="my_print_defaults" fi # # Test if someone changed datadir; In this case we should also read the # default arguments from this directory # extra_args="" if test "$datadir" != "/var/lib/mysql" then extra_args="-e $datadir/my.cnf" fi parse_arguments `$print_defaults $extra_args mysqld mysql_server mysql.server` # # Set pid file if not given # if test -z "$pid_file" then pid_file="/var/run/mysqld/mysqld.pid" else case "$pid_file" in /* ) ;; * ) pid_file="/var/run/mysqld/mysqld.pid" ;; esac fi # Safeguard (relative paths, core dumps..) cd $basedir case "$mode" in 'start') # Start daemon touch /var/log/mysqld.log chown mysql.mysql /var/log/mysqld.log chmod 0640 /var/log/mysqld.log # Check to see if datadir exists, if not initialize database if [ ! -d $datadir ] ; then echo "Initializing MySQL database: " /usr/bin/mysql_install_db # Give a few seconds for mysql to start chown -R mysql.mysql $datadir echo "Please wait whilst initialization completes" sleep 5 fi chown -R mysql.mysql $datadir chmod 0755 $datadir # Check to see if MySQL is running already if test -s "$pid_file" then echo "$prog appears to be already running, or was shutdown uncleanly. If this is the case remove the file: '$pid_file', and start again" else if test -x $bindir/mysqld_safe then # Give extra arguments to mysqld with the my.cnf file. This script may # be overwritten at next upgrade. $bindir/mysqld_safe --datadir=$datadir --pid-file=$pid_file >/dev/null 2>&1 & # Make lock for RedHat / SuSE if test -w /var/lock/subsys then touch /var/lock/subsys/mysql fi echo $"Starting $prog: " # Give a few seconds for mysql to start completely echo "Please wait whilst $prog completes" sleep 5 else echo "Can't execute $bindir/mysqld_safe from dir $basedir" fi fi ;; 'stop') # Stop daemon. We use a signal here to avoid having to know the # root password. if test -s "$pid_file" then mysqld_pid=`cat $pid_file` echo "Stopping $prog: " kill $mysqld_pid # mysqld should remove the pid_file when it exits, so wait for it. echo "Waiting for $prog to shutdown" sleep 10 if [ -s $pid_file ] then echo "Forcing removal of pid file" rm -rf $pid_file elif [ -n "$flags" ] then echo " done" fi # delete lock for RedHat / SuSE if test -f /var/lock/subsys/mysql then rm -f /var/lock/subsys/mysql fi else echo "$prog does not appear to be running!!!" fi ;; 'restart') # Stop the service and regardless of whether it was # running or not, start it again. $0 stop $0 start ;; *) # usage echo "Usage: $0 start|stop|restart" exit 1 ;; esac