在实际生产环境中mysql服务的环境一般是使用官网下载的二进制通用包部署的,它可以快速部署mysql的环境(具体部署可以看http://jim123.blog.51cto.com/4763600/1835010),但是它的启动和关闭或者重启的时候会有些麻烦需要用到mysql环境路径下的bin文件夹里的工具,它并没有在/etc/rc.d/init.d/里添加启动脚本,如果需要管理多台环境不一样的mysql重启的时候就用这些方法效率就会很低,有可能还会在启动时忘记指定用户带来的错误等等,所以我们可以在/etc/rc.d/init.d/路径下添加mysq的启动脚本来实现,这样我们平时就可以通过使用service mysqld restart或者是/etc/init.d/mysqld retsart来快速重启或关闭mysql
#!/bin/sh basedir=/usr/local/mysql#mysql的环境路径 datadir=/data/mysqldata#mysql的库文件路径 service_startup_timeout=900 pid_file= server_pid_file= use_mysqld_safe=1 user=mysql if test -z "$basedir" then basedir=/usr/local/mysql bindir=./bin if test -z "$datadir" then datadir=/usr/local/mysql/data fi sbindir=./bin libexecdir=./bin else bindir="$basedir/bin" if test -z "$datadir" then datadir="$basedir/data" fi sbindir="$basedir/sbin" libexecdir="$basedir/libexec" fi datadir_set= lsb_functions="/lib/lsb/init-functions" if test -f $lsb_functions ; then . $lsb_functions else log_success_msg() { echo " SUCCESS! [email protected]" } log_failure_msg() { echo " ERROR! [email protected]" } fi PATH=/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin export PATH mode=$1 # start or stop shift other_args="$*" case `echo "testing\c"`,`echo -n testing` in *c*,-n*) echo_n= echo_c= ;; *c*,*) echo_n=-n echo_c= ;; *) echo_n= echo_c=‘\c‘ ;; esac parse_server_arguments() { for arg do case "$arg" in --basedir=*) basedir=`echo "$arg" | sed -e ‘s/^[^=]*=//‘` bindir="$basedir/bin" if test -z "$datadir_set"; then datadir="$basedir/data" fi sbindir="$basedir/sbin" libexecdir="$basedir/libexec" ;; --datadir=*) datadir=`echo "$arg" | sed -e ‘s/^[^=]*=//‘` datadir_set=1 ;; --user=*) user=`echo "$arg" | sed -e ‘s/^[^=]*=//‘` ;; --pid-file=*) server_pid_file=`echo "$arg" | sed -e ‘s/^[^=]*=//‘` ;; --service-startup-timeout=*) service_startup_timeout=`echo "$arg" | sed -e ‘s/^[^=]*=//‘` ;; --use-mysqld_safe) use_mysqld_safe=1;; --use-manager) use_mysqld_safe=0;; esac done } parse_manager_arguments() { for arg do case "$arg" in --pid-file=*) pid_file=`echo "$arg" | sed -e ‘s/^[^=]*=//‘` ;; --user=*) user=`echo "$arg" | sed -e ‘s/^[^=]*=//‘` ;; esac done } wait_for_pid () { verb="$1" manager_pid="$2" i=0 avoid_race_condition="by checking again" while test $i -ne $service_startup_timeout ; do case "$verb" in ‘created‘) test -s $pid_file && i=‘‘ && break ;; ‘removed‘) test ! -s $pid_file && i=‘‘ && break ;; *) echo "wait_for_pid () usage: wait_for_pid created|removed manager_pid" exit 1 ;; esac if test -n "$manager_pid"; then if kill -0 "$manager_pid" 2>/dev/null; then : else if test -n "$avoid_race_condition"; then avoid_race_condition="" continue fi log_failure_msg "Manager of pid-file quit without updating file." return 1 fi fi echo $echo_n ".$echo_c" i=`expr $i + 1` sleep 1 done if test -z "$i" ; then log_success_msg return 0 else log_failure_msg return 1 fi } if test -x ./bin/my_print_defaults then print_defaults="./bin/my_print_defaults" elif test -x $bindir/my_print_defaults then print_defaults="$bindir/my_print_defaults" elif test -x $bindir/mysql_print_defaults then print_defaults="$bindir/mysql_print_defaults" else conf=/etc/my.cnf print_defaults= if test -r $conf then subpat=‘^[^=]*basedir[^=]*=\(.*\)$‘ dirs=`sed -e "/$subpat/!d" -e ‘s//\1/‘ $conf` for d in $dirs do d=`echo $d | sed -e ‘s/[ ]//g‘` if test -x "$d/bin/my_print_defaults" then print_defaults="$d/bin/my_print_defaults" break fi if test -x "$d/bin/mysql_print_defaults" then print_defaults="$d/bin/mysql_print_defaults" break fi done fi test -z "$print_defaults" && print_defaults="my_print_defaults" fi extra_args="" if test -r "$basedir/my.cnf" then extra_args="-e $basedir/my.cnf" else if test -r "$datadir/my.cnf" then extra_args="-e $datadir/my.cnf" fi fi parse_server_arguments `$print_defaults $extra_args mysqld server mysql_server mysql.server` parse_manager_arguments `$print_defaults $extra_args manager` if test -z "$pid_file" then pid_file=$datadir/mysqlmanager-`/bin/hostname`.pid else case "$pid_file" in /* ) ;; * ) pid_file="$datadir/$pid_file" ;; esac fi if test -z "$server_pid_file" then server_pid_file=$datadir/`/bin/hostname`.pid else case "$server_pid_file" in /* ) ;; * ) server_pid_file="$datadir/$server_pid_file" ;; esac fi case "$mode" in ‘start‘) cd $basedir manager=$bindir/mysqlmanager if test -x $libexecdir/mysqlmanager then manager=$libexecdir/mysqlmanager elif test -x $sbindir/mysqlmanager then manager=$sbindir/mysqlmanager fi echo $echo_n "Starting MySQL" if test -x $manager -a "$use_mysqld_safe" = "0" then if test -n "$other_args" then log_failure_msg "MySQL manager does not support options ‘$other_args‘" exit 1 fi "$manager" --mysqld-safe-compatible --user="$user" --pid-file="$pid_file" >/dev/null 2>&1 & wait_for_pid created $!; return_value=$? if test -w /var/lock/subsys then touch /var/lock/subsys/mysqlmanager fi exit $return_value elif test -x $bindir/mysqld_safe then pid_file=$server_pid_file $bindir/mysqld_safe --datadir=$datadir --pid-file=$server_pid_file $other_args >/dev/null 2>&1 & wait_for_pid created $!; return_value=$? if test -w /var/lock/subsys then touch /var/lock/subsys/mysql fi exit $return_value else log_failure_msg "Couldn‘t find MySQL manager ($manager) or server ($bindir/mysqld_safe)" fi ;; ‘stop‘) lock_dir=/var/lock/subsys/mysqlmanager if test ! -s "$pid_file" then pid_file=$server_pid_file lock_dir=/var/lock/subsys/mysql fi if test -s "$pid_file" then mysqlmanager_pid=`cat $pid_file` if (kill -0 $mysqlmanager_pid 2>/dev/null) then echo $echo_n "Shutting down MySQL" kill $mysqlmanager_pid wait_for_pid removed "$mysqlmanager_pid"; return_value=$? else log_failure_msg "MySQL manager or server process #$mysqlmanager_pid is not running!" rm $pid_file fi if test -f $lock_dir then rm -f $lock_dir fi exit $return_value else log_failure_msg "MySQL manager or server PID file could not be found!" fi ;; ‘restart‘) if $0 stop $other_args; then $0 start $other_args else log_failure_msg "Failed to stop running server, so refusing to try to start." exit 1 fi ;; ‘reload‘|‘force-reload‘) if test -s "$server_pid_file" ; then read mysqld_pid < $server_pid_file kill -HUP $mysqld_pid && log_success_msg "Reloading service MySQL" touch $server_pid_file else log_failure_msg "MySQL PID file could not be found!" exit 1 fi ;; ‘status‘) if test -s "$server_pid_file" ; then read mysqld_pid < $server_pid_file if kill -0 $mysqld_pid 2>/dev/null ; then log_success_msg "MySQL running ($mysqld_pid)" exit 0 else log_failure_msg "MySQL is not running, but PID file exists" exit 1 fi else mysqld_pid=`pidof $libexecdir/mysqld` if test -z $mysqld_pid ; then if test "$use_mysqld_safe" = "0" ; then lockfile=/var/lock/subsys/mysqlmanager else lockfile=/var/lock/subsys/mysql fi if test -f $lockfile ; then log_failure_msg "MySQL is not running, but lock exists" exit 2 fi log_failure_msg "MySQL is not running" exit 3 else log_failure_msg "MySQL is running but PID file could not be found" exit 4 fi fi ;; *) echo "Usage: $0 {start|stop|restart|reload|force-reload|status} [ MySQL server options ]" exit 1 ;; esac exit 0
这样我们就可以快速管理mysql的服务器了,当然可能实际生产环境中还有很多的服务并没有启动脚本,也需要我们手动去修改添加启动脚本。
时间: 2024-12-30 00:07:22