Last active
May 22, 2016 12:57
-
-
Save zeuxisoo/9952627 to your computer and use it in GitHub Desktop.
mysql.sh (start, stop, restart, force-quit and status)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
PREFIX=/usr/local/mysql | |
MYSQL_CONFIG=${PREFIX}/my.cnf | |
MYSQL_PORT=3306 | |
MYSQL_USERNAME="root" | |
MYSQL_PASSWORD="password" | |
MYSQLD_SAFE=${PREFIX}/bin/mysqld_safe | |
MYSQLADMIN=${PREFIX}/bin/mysqladmin | |
MYSQLD_PID_FILE_PATH=${PREFIX}/data/mysql.pid | |
SERVICE_STARTUP_TIMEOUT=900 | |
# Message control | |
lsb_functions="/lib/lsb/init-functions" | |
if test -f $lsb_functions ; then | |
. $lsb_functions | |
else | |
log_success_msg() { | |
echo " SUCCESS! $@" | |
} | |
log_failure_msg() { | |
echo " ERROR! $@" | |
} | |
fi | |
function wait_for_pid() { | |
verb="$1" # created | removed | |
pid="$2" # process ID of the program operating on the pid-file | |
pid_file_path="$3" # path to the PID file. | |
i=0 | |
avoid_race_condition="by checking again" | |
while test $i -ne $SERVICE_STARTUP_TIMEOUT ; do | |
case "$verb" in | |
'created') | |
# wait for a PID-file to pop into existence. | |
test -s "$pid_file_path" && i='' && break | |
;; | |
'removed') | |
# wait for this PID-file to disappear | |
test ! -s "$pid_file_path" && i='' && break | |
;; | |
*) | |
echo "wait_for_pid () usage: wait_for_pid created|removed pid pid_file_path" | |
exit 1 | |
;; | |
esac | |
# if server isn't running, then pid-file will never be updated | |
if test -n "$pid"; then | |
if kill -0 "$pid" 2>/dev/null; then | |
: # the server still runs | |
else | |
# The server may have exited between the last pid-file check and now. | |
if test -n "$avoid_race_condition"; then | |
avoid_race_condition="" | |
continue # Check again. | |
fi | |
# there's nothing that will affect the file. | |
log_failure_msg "The server quit without updating PID file ($pid_file_path)." | |
return 1 # not waiting any more. | |
fi | |
fi | |
i=`expr $i + 1` | |
sleep 1 | |
done | |
if test -z "$i" ; then | |
log_success_msg | |
return 0 | |
else | |
log_failure_msg | |
return 1 | |
fi | |
} | |
function is_running() { | |
status=$(ps -ef | grep 'bin/mysqld_safe' | wc -l) | |
if [ $status -gt 1 ]; then | |
return 0 # have process | |
else | |
return 1 # not have process | |
fi | |
} | |
function start() { | |
echo -n "Starting MySQL" | |
if is_running ; then | |
echo " failed, MySQL is running" | |
exit 1 | |
else | |
$MYSQLD_SAFE --defaults-file=$MYSQL_CONFIG 2>&1 > /dev/null & | |
echo " done" | |
fi | |
} | |
function stop() { | |
if test -s $MYSQLD_PID_FILE_PATH | |
then | |
mysqld_pid=`cat $MYSQLD_PID_FILE_PATH` | |
if (kill -0 $mysqld_pid 2>/dev/null) | |
then | |
echo "Shutting down MySQL" | |
kill $mysqld_pid | |
wait_for_pid removed "$mysqld_pid" "$MYSQLD_PID_FILE_PATH"; return_value=$? | |
else | |
log_failure_msg "MySQL server process #$mysqld_pid is not running!" | |
rm "$mysqld_pid_file_path" | |
fi | |
exit $return_value | |
else | |
log_failure_msg "MySQL server PID file could not be found!" | |
fi | |
} | |
case "$1" in | |
start) | |
start | |
;; | |
stop) | |
stop | |
;; | |
restart) | |
echo -n "Restarting MySQL" | |
start | |
stop | |
;; | |
force-quit) | |
kill -9 $(ps -ef | grep 'bin/mysqld_safe' | awk '{printf $2}') | |
;; | |
status) | |
if is_running ; then | |
echo "MySQL is running" | |
else | |
echo "MySQL is not running" | |
fi | |
;; | |
*) | |
echo "Usage: $0 {start|stop|restart|force-quit|status}" | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment