| 1 |
#!/bin/sh |
| 2 |
# |
| 3 |
# mysqld This shell script takes care of starting and stopping |
| 4 |
# the MySQL subsystem (mysqld). |
| 5 |
# |
| 6 |
# chkconfig: 345 64 36 |
| 7 |
# description: MySQL database server. |
| 8 |
# processname: mysqld |
| 9 |
# config: /etc/my.cnf |
| 10 |
# pidfile: /var/run/mysqld/mysqld.pid |
| 11 |
|
| 12 |
# Source function library. |
| 13 |
. /etc/rc.d/init.d/functions |
| 14 |
|
| 15 |
# Source networking configuration. |
| 16 |
. /etc/sysconfig/network |
| 17 |
|
| 18 |
|
| 19 |
exec="/usr/bin/mysqld_safe" |
| 20 |
prog="mysqld" |
| 21 |
|
| 22 |
# Set timeouts here so they can be overridden from /etc/sysconfig/mysqld |
| 23 |
STARTTIMEOUT=120 |
| 24 |
STOPTIMEOUT=600 |
| 25 |
|
| 26 |
# Set in /etc/sysconfig/mysqld, will be passed to mysqld_safe |
| 27 |
MYSQLD_OPTS= |
| 28 |
|
| 29 |
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog |
| 30 |
|
| 31 |
lockfile=/var/lock/subsys/$prog |
| 32 |
|
| 33 |
# Support for extra options passed to mysqld |
| 34 |
command=$1 && shift |
| 35 |
extra_opts="$@" |
| 36 |
|
| 37 |
# Extract value of a MySQL option from config files |
| 38 |
# Usage: get_mysql_option OPTION DEFAULT SECTION1 SECTION2 SECTIONN |
| 39 |
# Result is returned in $result |
| 40 |
# We use my_print_defaults which prints all options from multiple files, |
| 41 |
# with the more specific ones later; hence take the last match. |
| 42 |
get_mysql_option () { |
| 43 |
option=$1 |
| 44 |
default=$2 |
| 45 |
shift 2 |
| 46 |
result=$(/usr/bin/my_print_defaults "$@" | sed -n "s/^--${option}=//p" | tail -n 1) |
| 47 |
if [ -z "$result" ]; then |
| 48 |
# not found, use default |
| 49 |
result="${default}" |
| 50 |
fi |
| 51 |
} |
| 52 |
|
| 53 |
get_mysql_option datadir "/var/lib/mysql" mysqld |
| 54 |
datadir="$result" |
| 55 |
get_mysql_option socket "$datadir/mysql.sock" mysqld |
| 56 |
socketfile="$result" |
| 57 |
get_mysql_option log-error "/var/log/mysqld.log" mysqld mysqld_safe |
| 58 |
errlogfile="$result" |
| 59 |
get_mysql_option pid-file "/var/run/mysqld/mysqld.pid" mysqld mysqld_safe |
| 60 |
mypidfile="$result" |
| 61 |
|
| 62 |
case $socketfile in |
| 63 |
/*) adminsocket="$socketfile" ;; |
| 64 |
*) adminsocket="$datadir/$socketfile" ;; |
| 65 |
esac |
| 66 |
|
| 67 |
install_validate_password_sql_file () { |
| 68 |
local initfile |
| 69 |
initfile="$(mktemp /var/lib/mysql-files/install-validate-password-plugin.XXXXXX.sql)" |
| 70 |
chmod a+r "$initfile" |
| 71 |
echo "SET @@SESSION.SQL_LOG_BIN=0;" > "$initfile" |
| 72 |
echo "INSERT INTO mysql.component (component_id, component_group_id, component_urn) VALUES (1, 1, 'file://component_validate_password');" >> "$initfile" |
| 73 |
echo "$initfile" |
| 74 |
} |
| 75 |
|
| 76 |
fix_mysql_upgrade_info () { |
| 77 |
if [ -d "$datadir" ] && [ -O "$datadir/mysql_upgrade_info" ]; then |
| 78 |
chown --reference="$datadir" "$datadir/mysql_upgrade_info" |
| 79 |
if [ -x /usr/bin/chcon ]; then |
| 80 |
/usr/bin/chcon --reference="$datadir" "$datadir/mysql_upgrade_info" |
| 81 |
fi |
| 82 |
fi |
| 83 |
} |
| 84 |
|
| 85 |
start(){ |
| 86 |
[ -x $exec ] || exit 5 |
| 87 |
# check to see if it's already running |
| 88 |
RESPONSE=$(/usr/bin/mysqladmin --no-defaults --enable-cleartext-plugin --socket="$adminsocket" --user=UNKNOWN_MYSQL_USER ping 2>&1) |
| 89 |
if [ $? = 0 ]; then |
| 90 |
# already running, do nothing |
| 91 |
action $"Starting $prog: " /bin/true |
| 92 |
ret=0 |
| 93 |
elif echo "$RESPONSE" | grep -q "Access denied for user" |
| 94 |
then |
| 95 |
# already running, do nothing |
| 96 |
action $"Starting $prog: " /bin/true |
| 97 |
ret=0 |
| 98 |
else |
| 99 |
# prepare for start |
| 100 |
if [ ! -e "$errlogfile" -a ! -h "$errlogfile" -a "x$(dirname "$errlogfile")" = "x/var/log" ]; then |
| 101 |
install /dev/null -m0640 -omysql -gmysql "$errlogfile" |
| 102 |
fi |
| 103 |
[ -x /sbin/restorecon ] && /sbin/restorecon "$errlogfile" |
| 104 |
|
| 105 |
# mysql_upgrade_info file should be owned by mysql user since MySQL 8.0.16 |
| 106 |
fix_mysql_upgrade_info |
| 107 |
|
| 108 |
if [ ! -d "$datadir/mysql" ] ; then |
| 109 |
# First, make sure $datadir is there with correct permissions |
| 110 |
if [ ! -d "$datadir" -a ! -h "$datadir" -a "x$(dirname "$datadir")" = "x/var/lib" ]; then |
| 111 |
install -d -m0751 -omysql -gmysql "$datadir" || exit 1 |
| 112 |
fi |
| 113 |
if [ ! -h "$datadir" -a "x$(dirname "$datadir")" = "x/var/lib" ]; then |
| 114 |
chown mysql:mysql "$datadir" |
| 115 |
chmod 0751 "$datadir" |
| 116 |
fi |
| 117 |
if [ -x /sbin/restorecon ]; then |
| 118 |
/sbin/restorecon "$datadir" |
| 119 |
for dir in /var/lib/mysql-files /var/lib/mysql-keyring ; do |
| 120 |
if [ -x /usr/sbin/semanage -a -d /var/lib/mysql -a -d $dir ] ; then |
| 121 |
/usr/sbin/semanage fcontext -a -e /var/lib/mysql $dir >/dev/null 2>&1 |
| 122 |
/sbin/restorecon -r $dir |
| 123 |
fi |
| 124 |
done |
| 125 |
fi |
| 126 |
# Now create the database |
| 127 |
initfile="$(install_validate_password_sql_file)" |
| 128 |
action $"Initializing MySQL database: " /usr/sbin/mysqld --initialize --datadir="$datadir" --user=mysql --init-file="$initfile" |
| 129 |
ret=$? |
| 130 |
rm -f "$initfile" |
| 131 |
[ $ret -ne 0 ] && return $ret |
| 132 |
# Generate certs if needed |
| 133 |
if [ -x /usr/bin/mysql_ssl_rsa_setup -a ! -e "${datadir}/server-key.pem" ] ; then |
| 134 |
/usr/bin/mysql_ssl_rsa_setup --datadir="$datadir" --uid=mysql >/dev/null 2>&1 |
| 135 |
fi |
| 136 |
fi |
| 137 |
if [ ! -h "$datadir" -a "x$(dirname "$datadir")" = "x/var/lib" ]; then |
| 138 |
chown mysql:mysql "$datadir" |
| 139 |
chmod 0751 "$datadir" |
| 140 |
fi |
| 141 |
# Pass all the options determined above, to ensure consistent behavior. |
| 142 |
# In many cases mysqld_safe would arrive at the same conclusions anyway |
| 143 |
# but we need to be sure. (An exception is that we don't force the |
| 144 |
# log-error setting, since this script doesn't really depend on that, |
| 145 |
# and some users might prefer to configure logging to syslog.) |
| 146 |
# Note: set --basedir to prevent probes that might trigger SELinux |
| 147 |
# alarms, per bug #547485 |
| 148 |
$exec $MYSQLD_OPTS --datadir="$datadir" --socket="$socketfile" \ |
| 149 |
--pid-file="$mypidfile" \ |
| 150 |
--basedir=/usr --user=mysql $extra_opts >/dev/null & |
| 151 |
safe_pid=$! |
| 152 |
# Spin for a maximum of N seconds waiting for the server to come up; |
| 153 |
# exit the loop immediately if mysqld_safe process disappears. |
| 154 |
# Rather than assuming we know a valid username, accept an "access |
| 155 |
# denied" response as meaning the server is functioning. |
| 156 |
ret=0 |
| 157 |
TIMEOUT="$STARTTIMEOUT" |
| 158 |
while [ $TIMEOUT -gt 0 ]; do |
| 159 |
RESPONSE=$(/usr/bin/mysqladmin --no-defaults --enable-cleartext-plugin --socket="$adminsocket" --user=UNKNOWN_MYSQL_USER ping 2>&1) && break |
| 160 |
echo "$RESPONSE" | grep -q "Access denied for user" && break |
| 161 |
if ! /bin/kill -0 $safe_pid 2>/dev/null; then |
| 162 |
echo "MySQL Daemon failed to start." |
| 163 |
ret=1 |
| 164 |
break |
| 165 |
fi |
| 166 |
sleep 1 |
| 167 |
let TIMEOUT=${TIMEOUT}-1 |
| 168 |
done |
| 169 |
if [ $TIMEOUT -eq 0 ]; then |
| 170 |
echo "Timeout error occurred trying to start MySQL Daemon." |
| 171 |
ret=1 |
| 172 |
fi |
| 173 |
if [ $ret -eq 0 ]; then |
| 174 |
action $"Starting $prog: " /bin/true |
| 175 |
touch $lockfile |
| 176 |
else |
| 177 |
action $"Starting $prog: " /bin/false |
| 178 |
fi |
| 179 |
fi |
| 180 |
return $ret |
| 181 |
} |
| 182 |
|
| 183 |
stop(){ |
| 184 |
if [ ! -f "$mypidfile" ]; then |
| 185 |
# not running; per LSB standards this is "ok" |
| 186 |
action $"Stopping $prog: " /bin/true |
| 187 |
return 0 |
| 188 |
fi |
| 189 |
MYSQLPID=$(cat "$mypidfile") |
| 190 |
if [ -n "$MYSQLPID" ]; then |
| 191 |
/bin/su - mysql -s /bin/bash -c "/bin/kill $MYSQLPID" >/dev/null 2>&1 |
| 192 |
ret=$? |
| 193 |
if [ $ret -eq 0 ]; then |
| 194 |
TIMEOUT="$STOPTIMEOUT" |
| 195 |
while [ $TIMEOUT -gt 0 ]; do |
| 196 |
/bin/kill -0 "$MYSQLPID" >/dev/null 2>&1 || break |
| 197 |
sleep 1 |
| 198 |
let TIMEOUT=${TIMEOUT}-1 |
| 199 |
done |
| 200 |
if [ $TIMEOUT -eq 0 ]; then |
| 201 |
echo "Timeout error occurred trying to stop MySQL Daemon." |
| 202 |
ret=1 |
| 203 |
action $"Stopping $prog: " /bin/false |
| 204 |
else |
| 205 |
rm -f $lockfile |
| 206 |
rm -f "$socketfile" |
| 207 |
action $"Stopping $prog: " /bin/true |
| 208 |
fi |
| 209 |
else |
| 210 |
action $"Stopping $prog: " /bin/false |
| 211 |
fi |
| 212 |
else |
| 213 |
# failed to read pidfile, probably insufficient permissions |
| 214 |
action $"Stopping $prog: " /bin/false |
| 215 |
ret=4 |
| 216 |
fi |
| 217 |
return $ret |
| 218 |
} |
| 219 |
|
| 220 |
restart(){ |
| 221 |
stop |
| 222 |
start |
| 223 |
} |
| 224 |
|
| 225 |
condrestart(){ |
| 226 |
[ -e $lockfile ] && restart || : |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
# See how we were called. |
| 231 |
case "$command" in |
| 232 |
start) |
| 233 |
start |
| 234 |
;; |
| 235 |
stop) |
| 236 |
stop |
| 237 |
;; |
| 238 |
status) |
| 239 |
status -p "$mypidfile" $prog |
| 240 |
;; |
| 241 |
restart) |
| 242 |
restart |
| 243 |
;; |
| 244 |
condrestart|try-restart) |
| 245 |
condrestart |
| 246 |
;; |
| 247 |
reload) |
| 248 |
exit 3 |
| 249 |
;; |
| 250 |
force-reload) |
| 251 |
restart |
| 252 |
;; |
| 253 |
*) |
| 254 |
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}" |
| 255 |
exit 2 |
| 256 |
esac |
| 257 |
|
| 258 |
exit $? |