diff --git a/mysql.init b/mysql.init index 86645a1..91d93d8 100644 --- a/mysql.init +++ b/mysql.init @@ -18,23 +18,55 @@ prog="MySQL" -datadir="/var/lib/mysql" - -start(){ - touch /var/log/mysqld.log - chown mysql:mysql /var/log/mysqld.log - chmod 0640 /var/log/mysqld.log - [ -x /sbin/restorecon ] && /sbin/restorecon /var/log/mysqld.log - if [ ! -d $datadir/mysql ] ; then - action $"Initializing MySQL database: " /usr/bin/mysql_install_db - ret=$? - chown -R mysql:mysql $datadir - if [ $ret -ne 0 ] ; then - return $ret; +# extract value of a MySQL option from /etc/my.cnf +# Usage: get_mysql_option FILE VARNAME DEFAULT +# result is returned in $result +# Ugly as this is, it knows nothing of option file sections ... +get_mysql_option(){ + result=`sed -n "s/^[ \t]*$2[ \t]*=[ \t]*//p" "$1" 2>/dev/null | tail -n 1` + if [ -z "$result" ]; then + # not found, use default + result="$3" + else + # found, still have to deal with quoting and end-of-line comments + dequoted=`echo "$result" | sed "s/^'\([^']*\)'.*$/\1/"` + if [ x"$dequoted" != x"$result" ]; then + result="$dequoted" + else + dequoted=`echo "$result" | sed 's/^"\([^"]*\)".*$/\1/'` + if [ x"$dequoted" != x"$result" ]; then + result="$dequoted" + else + result=`echo "$result" | sed 's/^\([^ \t#]*\).*$/\1/'` + fi fi fi - chown -R mysql:mysql $datadir - chmod 0755 $datadir +} + +get_mysql_option /etc/my.cnf datadir "/var/lib/mysql" +datadir="$result" +get_mysql_option /etc/my.cnf socket "$datadir/mysql.sock" +socketfile="$result" +get_mysql_option /etc/my.cnf err-log "/var/log/mysqld.log" +errlogfile="$result" +get_mysql_option /etc/my.cnf pid-file "/var/run/mysqld/mysqld.pid" +mypidfile="$result" + +start(){ + touch "$errlogfile" + chown mysql:mysql "$errlogfile" + chmod 0640 "$errlogfile" + [ -x /sbin/restorecon ] && /sbin/restorecon "$errlogfile" + if [ ! -d "$datadir/mysql" ] ; then + action $"Initializing MySQL database: " /usr/bin/mysql_install_db + ret=$? + chown -R mysql:mysql "$datadir" + if [ $ret -ne 0 ] ; then + return $ret + fi + fi + chown -R mysql:mysql "$datadir" + chmod 0755 "$datadir" /usr/bin/mysqld_safe --defaults-file=/etc/my.cnf >/dev/null 2>&1 & ret=$? # Spin for a maximum of N seconds waiting for the server to come up. @@ -62,21 +94,21 @@ start(){ } stop(){ - /bin/kill `cat /var/run/mysqld/mysqld.pid 2> /dev/null ` > /dev/null 2>&1 + /bin/kill `cat "$mypidfile" 2>/dev/null ` >/dev/null 2>&1 ret=$? if [ $ret -eq 0 ]; then + sleep 2 + rm -f /var/lock/subsys/mysqld + rm -f "$socketfile" action $"Stopping $prog: " /bin/true else action $"Stopping $prog: " /bin/false fi - [ $ret -eq 0 ] && rm -f /var/lock/subsys/mysqld - [ $ret -eq 0 ] && rm -f $datadir/mysql.sock return $ret } restart(){ stop - sleep 2 start }