MySQL、MariaDB安装和多实例配置(5)

下面的脚本即可作为多实例服务管理脚本,也可以作为单实例服务管理脚本,只需将脚本名称改一改即可。

#!/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 # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network basedir=/usr exec="$basedir/bin/mysqld_safe" prog="mysqld" port=3306 datadir="/mydata/$port/data" socketfile="$datadir/mysql.sock" errlogfile="$datadir/mysqld.log" mypidfile="$datadir/mysqld.pid" cnf="/mydata/$port/my.cnf" # Set timeouts here so they can be overridden from /etc/sysconfig/mysqld STARTTIMEOUT=120 STOPTIMEOUT=60 # Set in /etc/sysconfig/mysqld, will be passed to mysqld_safe MYSQLD_OPTS= [ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog lockfile=/var/lock/subsys/$prog 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 # Now start service $exec $MYSQLD_OPTS --defaults-file="$cnf" --datadir="$datadir" --socket="$socketfile" \ --pid-file="$mypidfile" \ --basedir="$basedir" --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 this is "ok" action $"Stopping $prog: " /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 TIMEOUT=${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 $"Stopping $prog: " /bin/true fi else action $"Stopping $prog: " /bin/false fi else # failed to read pidfile, 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 "$1" in start) start ;; stop) stop ;; status) status -p "$mypidfile" $prog ;; restart) restart ;; condrestart|try-restart) condrestart ;; reload) exit 3 ;; force-reload) restart ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}" exit 2 esac exit $?

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/34940e79b6de6dc3cc1570e752003025.html