In a real-world production environment, the MySQL service environment is typically deployed using a binary generic package downloaded from the official website, which allows for rapid deployment of the MySQL environment (the deployment can look at http://jim123.blog.51cto.com/4763600/1835010), But it's going to be a bit of a hassle when it starts and shuts down or restarts, and it needs to use the tools in the Bin folder under the MySQL environment path, and it doesn't add a startup script to the/etc/rc.d/init.d/. If you need to manage more than one of the different environments of MySQL restart, the efficiency of these methods will be very low, it is possible to forget the specified user error at startup, and so on, so we can add mysq in the/etc/rc.d/init.d/path launch script to implement, This allows us to quickly restart or close MySQL by using the service mysqld restart or/etc/init.d/mysqld retsart.
#!/bin/shbasedir=/usr/local/mysql#mysql Environment Path Datadir=/data/mysqldata#mysql the library file path service_startup_timeout=900pid_ file=server_pid_file=use_mysqld_safe=1user=mysqlif test -z "$basedir" then basedir=/ usr/local/mysql bindir=./bin if test -z "$datadir" then datadir=/usr/local/mysql/data fi sbindir=./bin libexecdir=. /binelse bindir= "$basedir/bin" if test -z "$datadir" then datadir= "$basedir/data" fi sbindir= "$basedir/sbin" Libexecdir= "$basedir/libexec" fidatadir_set=lsb_functions= "/lib/lsb/init-functions" if test -f $ lsb_functions ; then . $lsb _functionselse log_success_msg () { echo " success! [email protected]" } log_ Failure_msg () { echo " error! [email protected]" }fipath=/sbin:/usr/ Sbin:/bin:/usr/bin: $basedir/binexport pathmode=$1 # start or stopshiftother_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 ' ;; Esacparse_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= "$" Manager_pid= "$" 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_defaultsthen print_defaults= "./bin/my_ Print_defaults "elif test -x $bindir/my_print_defaultsthen print_defaults=" $bindir/my _print_defaults "elif test -x $bindir/mysql_print_defaultsthen 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" fiextra_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" fifiparse_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 ' .pidelse case "$pid _file" in /* ) ;; * ) pid_file= "$datadir/$pid _file" ;; esacfiif test -z "$server _pid_file" then server_pid_file= $datadir/'/bin/ Hostname ' .pidelse case "$server _pid_file" in /* ) ;; * ) server_pid_file= "$datadir/$server _pid_file" ;; esacficase "$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 ;; Esacexit 0
So that we can quickly manage the MySQL server, of course, there are many services in the actual production environment and there is no startup script, we also need to manually modify the Add startup script.
This article from the "Technical essay" blog, reproduced please contact the author!
Share one of the available mysq startup scripts