#!/bin/sh
#
# mysqld This shell script takes care of starting and stopping
# The MySQL subsystem (MYSQLD).
#
# chkconfig:345 64 36
# Description:mysql database server.
# Processname:mysqld
# config:/etc/my.cnf
# Pidfile:/var/run/mysqld/mysqld.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source Networking configuration.
. /etc/sysconfig/network
Exec= "/usr/bin/mysqld_safe"
Prog= "Mysqld"
# Set Timeouts They can be overridden from/etc/sysconfig/mysqld
starttimeout=120
Stoptimeout=60
# Set In/etc/sysconfig/mysqld, 'll is passed to Mysqld_safe
mysqld_opts=
[-e/etc/sysconfig/$prog] &&. /etc/sysconfig/$prog
lockfile=/var/lock/subsys/$prog
# Extract value of a MySQL option from config files
# usage:get_mysql_option option DEFAULT SECTION1 SECTION2 sectionn
# Result is returned in $result
# We use my_print_defaults which prints all options from multiple files,
# with the more specific ones later; Hence take the last match.
Get_mysql_option () {
Option=$1
Default=$2
Shift 2
result=$ (/usr/bin/my_print_defaults "[email protected]" | sed-n "s/^--${option}=//p" | tail-n 1)
If [-Z "$result"]; Then
# found, use default
result= "${default}"
Fi
}
Get_mysql_option datadir "/var/lib/mysql" mysqld
Datadir= "$result"
Get_mysql_option socket "$datadir/mysql.sock" mysqld
Socketfile= "$result"
Get_mysql_option log-error "/var/log/mysqld.log" Mysqld Mysqld_safe
Errlogfile= "$result"
Get_mysql_option pid-file "/var/run/mysqld/mysqld.pid" Mysqld Mysqld_safe
Mypidfile= "$result"
Case $socketfile in
/*) adminsocket= "$socketfile";;
*) adminsocket= "$datadir/$socketfile";;
Esac
Start () {
[-X $exec] | | Exit 5
# Check to see if it ' s already running
response=$ (/usr/bin/mysqladmin--no-defaults--socket= "$adminsocket"--user=unknown_mysql_user ping 2>&1)
If [$ = 0]; Then
# already running, do nothing
Action $ "Starting $prog:"/bin/true
Ret=0
elif echo "$RESPONSE" | Grep-q "Access denied for user"
Then
# already running, do nothing
Action $ "Starting $prog:"/bin/true
Ret=0
Else
# Prepare for Start
if [!-e "$errlogfile"-A!-h "$errlogfile"-A "x$ (dirname" $errlogfile ")" = "X/var/log"]; Then
Install/dev/null-m0640-omysql-gmysql "$errlogfile"
Fi
[-x/sbin/restorecon] &&/sbin/restorecon "$errlogfile"
if [!-d "$datadir/mysql"]; Then
# First, make sure $datadir are there with correct permissions
if [!-d "$datadir"-A!-h "$datadir"-A "x$ (dirname" $datadir ")" = "x/var/lib"]; Then
install-d-m0755-omysql-gmysql "$datadir" | | Exit 1
Fi
if [!-h "$datadir"-A "x$ (dirname" $datadir ")" = "x/var/lib"]; Then
Chown mysql:mysql "$datadir"
chmod 0755 "$datadir"
Fi
if [-x/sbin/restorecon]; Then
/sbin/restorecon "$datadir"
For dir in/var/lib/mysql-files; Do
If [-x/usr/sbin/semanage-a-d/var/lib/mysql-a-D $dir]; Then
/usr/sbin/semanage fcontext-a-e/var/lib/mysql $dir >/dev/null 2>&1
/sbin/restorecon $dir
Fi
Done
Fi
# now create the database
Action $ "Initializing MySQL database:"/usr/bin/mysql_install_db--rpm--datadir= "$datadir"--user=mysql
Ret=$?
If [$ret-ne 0]; Then
Return $ret
Fi
Fi
if [!-h "$datadir"-A "x$ (dirname" $datadir ")" = "x/var/lib"]; Then
Chown mysql:mysql "$datadir"
chmod 0755 "$datadir"
Fi
# Pass All the options determined above, to ensure consistent behavior.
# In many cases Mysqld_safe would arrive at the same conclusions anyway
# but we need to be sure. (An exception are that we don ' t force the
# Log-error setting, since this script doesn ' t really depend on that,
# and some users might prefer to configure logging to syslog.)
# Note:set--basedir To prevent probes that might trigger SELinux
# alarms, per bug #547485
$exec $MYSQLD _opts--datadir= "$datadir"--socket= "$socketfile" \
--pid-file= "$mypidfile" \
--BASEDIR=/USR--user=mysql >/dev/null &
safe_pid=$!
# Spin for a maximum of N seconds waiting for the server to come up;
# exit the loop immediately if Mysqld_safe process disappears.
# rather than assuming we know a valid username, accept an "access
# denied "response as meaning the server is functioning.
Ret=0
timeout= "$STARTTIMEOUT"
While [$TIMEOUT-GT 0]; Do
response=$ (/usr/bin/mysqladmin--no-defaults--socket= "$adminsocket"--user=unknown_mysql_user ping 2>&1) && break
echo "$RESPONSE" | Grep-q "Access denied for user" && break
if! /bin/kill-0 $safe _pid 2>/dev/null; Then
echo "MySQL Daemon failed to start."
Ret=1
Break
Fi
Sleep 1
Let Timeout=${timeout}-1
Done
If [$TIMEOUT-eq 0]; Then
echo "Timeout error occurred trying to start MySQL Daemon."
Ret=1
Fi
If [$ret-eq 0]; Then
Action $ "Starting $prog:"/bin/true
Touch $lockfile
Else
Action $ "Starting $prog:"/bin/false
Fi
Fi
Return $ret
}
Stop () {
If [!-f ' $mypidfile]; then
# not running; per LSB standards the "OK"
Action $ "stopping $pro G: "/bin/true
return 0
Fi
mysqlpid= ' cat" $mypidfile "'
If [-N" $MYSQLPID "]; Then
/bin/kill "$MYSQLPID" >/dev/null 2>&1
ret=$?
If [$ret-eq 0], then
timeout= "$STOPTIMEOUT"
While [$TIMEOUT-GT 0], do
/bin/kill-0 ' $MYSQLPID ' >/dev/null 2>&1 | | Break
Sleep 1
Let time Out=${timeout}-1
Done
If [$TIMEOUT-eq 0], then
echo "TIMEOUT error occurred trying to stop MySQL Daemon."
Ret=1
Action $ "stopping $prog:"/bin/false
Else
rm-f $lockfile
rm-f "$socketfile"
Action $ "Sto Pping $prog: "/bin/true
Fi
Else
Action $" stopping $prog: "/bin/false
Fi
Else
# failed to read pi Dfile, probably insufficient permissions
action $ "stopping $prog:"/bin/false
ret=4
Fi
return $ret
}
Restart () {
Stop
start
}
Condrestart () {
[-e $lockfile] && Restart | | :
}
# See how we were called.
Case "$" in
Start
Start
;;
Stop
Stop
;;
Status
Status-p "$mypidfile" $prog
;;
Restart
Restart
;;
Condrestart|try-restart)
Condrestart
;;
Reload
Exit 3
;;
Force-reload)
Restart
;;
*)
echo $ "Usage: $ {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
Exit 2
Esac
Exit $?
MYSQLD Service boot-up (CentOS 6.5)