3
# mysqld This shell script takes care of starting and stopping
4
# the MySQL subsystem (mysqld).
7
# description: MySQL database server.
10
# pidfile: /var/run/mysqld/mysqld.pid
12
# Source function library.
13
. /etc/rc.d/init.d/functions
15
# Source networking configuration.
16
. /etc/sysconfig/network
19
exec="/usr/bin/mysqld_safe"
22
# Set timeouts here so they can be overridden from /etc/sysconfig/mysqld
26
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
28
lockfile=/var/lock/subsys/$prog
31
# extract value of a MySQL option from config files
32
# Usage: get_mysql_option SECTION VARNAME DEFAULT
33
# result is returned in $result
34
# We use my_print_defaults which prints all options from multiple files,
35
# with the more specific ones later; hence take the last match.
37
result=`/usr/bin/my_print_defaults "$1" | sed -n "s/^--$2=//p" | tail -n 1`
38
if [ -z "$result" ]; then
39
# not found, use default
44
get_mysql_option mysqld datadir "/var/lib/mysql"
46
get_mysql_option mysqld socket "$datadir/mysql.sock"
48
get_mysql_option mysqld_safe log-error "/var/log/mysqld.log"
50
get_mysql_option mysqld_safe pid-file "/var/run/mysqld/mysqld.pid"
55
[ -x $exec ] || exit 5
56
# check to see if it's already running
57
RESPONSE=`/usr/bin/mysqladmin --socket="$socketfile" --user=UNKNOWN_MYSQL_USER ping 2>&1`
59
# already running, do nothing
60
action $"Starting $prog: " /bin/true
62
elif echo "$RESPONSE" | grep -q "Access denied for user"
64
# already running, do nothing
65
action $"Starting $prog: " /bin/true
70
chown mysql:mysql "$errlogfile"
71
chmod 0640 "$errlogfile"
72
[ -x /sbin/restorecon ] && /sbin/restorecon "$errlogfile"
73
if [ ! -d "$datadir/mysql" ] ; then
74
# First, make sure $datadir is there with correct permissions
75
if [ ! -e "$datadir" -a ! -h "$datadir" ]
77
mkdir -p "$datadir" || exit 1
79
chown mysql:mysql "$datadir"
81
[ -x /sbin/restorecon ] && /sbin/restorecon "$datadir"
82
# Now create the database
83
action $"Initializing MySQL database: " /usr/bin/mysql_install_db --rpm --datadir="$datadir" --user=mysql
85
chown -R mysql:mysql "$datadir"
86
if [ $ret -ne 0 ] ; then
90
chown mysql:mysql "$datadir"
92
# Pass all the options determined above, to ensure consistent behavior.
93
# In many cases mysqld_safe would arrive at the same conclusions anyway
94
# but we need to be sure. (An exception is that we don't force the
95
# log-error setting, since this script doesn't really depend on that,
96
# and some users might prefer to configure logging to syslog.)
97
# Note: set --basedir to prevent probes that might trigger SELinux
98
# alarms, per bug #547485
99
$exec --datadir="$datadir" --socket="$socketfile" \
100
--pid-file="$mypidfile" \
101
--basedir=/usr --user=mysql >/dev/null 2>&1 &
103
# Spin for a maximum of N seconds waiting for the server to come up;
104
# exit the loop immediately if mysqld_safe process disappears.
105
# Rather than assuming we know a valid username, accept an "access
106
# denied" response as meaning the server is functioning.
108
TIMEOUT="$STARTTIMEOUT"
109
while [ $TIMEOUT -gt 0 ]; do
110
RESPONSE=`/usr/bin/mysqladmin --socket="$socketfile" --user=UNKNOWN_MYSQL_USER ping 2>&1` && break
111
echo "$RESPONSE" | grep -q "Access denied for user" && break
112
if ! /bin/kill -0 $safe_pid 2>/dev/null; then
113
echo "MySQL Daemon failed to start."
118
let TIMEOUT=${TIMEOUT}-1
120
if [ $TIMEOUT -eq 0 ]; then
121
echo "Timeout error occurred trying to start MySQL Daemon."
124
if [ $ret -eq 0 ]; then
125
action $"Starting $prog: " /bin/true
128
action $"Starting $prog: " /bin/false
135
if [ ! -f "$mypidfile" ]; then
136
# not running; per LSB standards this is "ok"
137
action $"Stopping $prog: " /bin/true
140
MYSQLPID=`cat "$mypidfile"`
141
if [ -n "$MYSQLPID" ]; then
142
/bin/kill "$MYSQLPID" >/dev/null 2>&1
144
if [ $ret -eq 0 ]; then
145
TIMEOUT="$STOPTIMEOUT"
146
while [ $TIMEOUT -gt 0 ]; do
147
/bin/kill -0 "$MYSQLPID" >/dev/null 2>&1 || break
149
let TIMEOUT=${TIMEOUT}-1
151
if [ $TIMEOUT -eq 0 ]; then
152
echo "Timeout error occurred trying to stop MySQL Daemon."
154
action $"Stopping $prog: " /bin/false
158
action $"Stopping $prog: " /bin/true
161
action $"Stopping $prog: " /bin/false
164
# failed to read pidfile, probably insufficient permissions
165
action $"Stopping $prog: " /bin/false
177
[ -e $lockfile ] && restart || :
181
# See how we were called.
190
status -p "$mypidfile" $prog
195
condrestart|try-restart)
205
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"