SHARE
TWEET

Untitled

a guest Jul 22nd, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2. # Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB
  3. # This file is public domain and comes with NO WARRANTY of any kind
  4.  
  5. # MariaDB daemon start/stop script.
  6.  
  7. # Usually this is put in /etc/init.d (at least on machines SYSV R4 based
  8. # systems) and linked to /etc/rc3.d/S99mysql and /etc/rc0.d/K01mysql.
  9. # When this is done the mysql server will be started when the machine is
  10. # started and shut down when the systems goes down.
  11.  
  12. # Comments to support chkconfig on RedHat Linux
  13. # chkconfig: 2345 64 36
  14. # description: A very fast and reliable SQL database engine.
  15.  
  16. # Comments to support LSB init script conventions
  17. ### BEGIN INIT INFO
  18. # Provides: mysql
  19. # Required-Start: $local_fs $network $remote_fs
  20. # Should-Start: ypbind nscd ldap ntpd xntpd
  21. # Required-Stop: $local_fs $network $remote_fs
  22. # Default-Start:  2 3 4 5
  23. # Default-Stop: 0 1 6
  24. # Short-Description: start and stop MariaDB
  25. # Description: MariaDB is a very fast and reliable SQL database engine.
  26. ### END INIT INFO
  27.  
  28. # have to do one of the following things for this script to work:
  29. #
  30. # - Run this script from within the MariaDB installation directory
  31. # - Create a /etc/my.cnf file with the following information:
  32. #   [mysqld]
  33. #   basedir=<path-to-mysql-installation-directory>
  34. # - Add the above to any other configuration file (for example ~/.my.ini)
  35. #   and copy my_print_defaults to /usr/bin
  36. # - Add the path to the mysql-installation-directory to the basedir variable
  37. #   below.
  38. #
  39. # If you want to affect other MariaDB variables, you should make your changes
  40. # in the /etc/my.cnf, ~/.my.cnf or other MariaDB configuration files.
  41.  
  42. # If you change base dir, you must also change datadir. These may get
  43. # overwritten by settings in the MariaDB configuration files.
  44.  
  45. basedir=
  46. datadir=
  47.  
  48. # Default value, in seconds, afterwhich the script should timeout waiting
  49. # for server start.
  50. # Value here is overridden by value in my.cnf.
  51. # 0 means don't wait at all
  52. # Negative numbers mean to wait indefinitely
  53. service_startup_timeout=900
  54.  
  55. # Lock directory for RedHat / SuSE.
  56. lockdir='/var/lock/subsys'
  57. lock_file_path="$lockdir/mysql"
  58.  
  59. # The following variables are only set for letting mysql.server find things.
  60.  
  61. # Set some defaults
  62. mysqld_pid_file_path=
  63. if test -z "$basedir"
  64. then
  65.   basedir=/usr
  66.   bindir=/usr/bin
  67.   if test -z "$datadir"
  68.   then
  69.     datadir=/var/lib/mysql
  70.   fi
  71.   sbindir=/usr/sbin
  72.   libexecdir=/usr/sbin
  73. else
  74.   bindir="$basedir/bin"
  75.   if test -z "$datadir"
  76.   then
  77.     datadir="$basedir/data"
  78.   fi
  79.   sbindir="$basedir/sbin"
  80.   if test -f "$basedir/bin/mysqld"
  81.   then
  82.     libexecdir="$basedir/bin"
  83.   else
  84.     libexecdir="$basedir/libexec"
  85.   fi
  86. fi
  87.  
  88. # datadir_set is used to determine if datadir was set (and so should be
  89. # *not* set inside of the --basedir= handler.)
  90. datadir_set=
  91.  
  92. #
  93. # Use LSB init script functions for printing messages, if possible
  94. #
  95. lsb_functions="/lib/lsb/init-functions"
  96. if test -f $lsb_functions ; then
  97.   . $lsb_functions
  98. else
  99.   # Include non-LSB RedHat init functions to make systemctl redirect work
  100.   init_functions="/etc/init.d/functions"
  101.   if test -f $init_functions; then
  102.     . $init_functions
  103.   fi
  104.   log_success_msg()
  105.   {
  106.     echo " SUCCESS! $@"
  107.   }
  108.   log_failure_msg()
  109.   {
  110.     echo " ERROR! $@"
  111.   }
  112. fi
  113.  
  114. PATH="/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin"
  115. export PATH
  116.  
  117. mode=$1    # start or stop
  118.  
  119. [ $# -ge 1 ] && shift
  120.  
  121. case `echo "testing\c"`,`echo -n testing` in
  122.     *c*,-n*) echo_n=   echo_c=     ;;
  123.     *c*,*)   echo_n=-n echo_c=     ;;
  124.     *)       echo_n=   echo_c='\c' ;;
  125. esac
  126.  
  127. parse_server_arguments() {
  128.   for arg do
  129.     val=`echo "$arg" | sed -e 's/^[^=]*=//'`
  130.     case "$arg" in
  131.       --basedir=*)  basedir="$val"
  132.                     bindir="$basedir/bin"
  133.             if test -z "$datadir_set"; then
  134.               datadir="$basedir/data"
  135.             fi
  136.             sbindir="$basedir/sbin"
  137.                     if test -f "$basedir/bin/mysqld"
  138.                     then
  139.                       libexecdir="$basedir/bin"
  140.                     else
  141.                       libexecdir="$basedir/libexec"
  142.                     fi
  143.             libexecdir="$basedir/libexec"
  144.         ;;
  145.       --datadir=*)  datadir="$val"
  146.             datadir_set=1
  147.     ;;
  148.       --log-basename=*|--hostname=*|--loose-log-basename=*)
  149.         mysqld_pid_file_path="$val.pid"
  150.     ;;
  151.       --pid-file=*) mysqld_pid_file_path="$val" ;;
  152.       --service-startup-timeout=*) service_startup_timeout="$val" ;;
  153.       --user=*) user="$val"; ;;
  154.     esac
  155.   done
  156. }
  157.  
  158. # Get arguments from the my.cnf file,
  159. # the only group, which is read from now on is [mysqld]
  160. if test -x "$bindir/my_print_defaults";  then
  161.   print_defaults="$bindir/my_print_defaults"
  162. else
  163.   # Try to find basedir in /etc/my.cnf
  164.   conf=/etc/my.cnf
  165.   print_defaults=
  166.   if test -r $conf
  167.   then
  168.     subpat='^[^=]*basedir[^=]*=\(.*\)$'
  169.     dirs=`sed -e "/$subpat/!d" -e 's//\1/' $conf`
  170.     for d in $dirs
  171.     do
  172.       d=`echo $d | sed -e 's/[  ]//g'`
  173.       if test -x "$d/bin/my_print_defaults"
  174.       then
  175.         print_defaults="$d/bin/my_print_defaults"
  176.         break
  177.       fi
  178.     done
  179.   fi
  180.  
  181.   # Hope it's in the PATH ... but I doubt it
  182.   test -z "$print_defaults" && print_defaults="my_print_defaults"
  183. fi
  184.  
  185. user='mysql'
  186.  
  187. su_kill() {
  188.   if test "$USER" = "$user"; then
  189.     kill $* >/dev/null 2>&1
  190.   else
  191.     su - $user -s /bin/sh -c "kill $*" >/dev/null 2>&1
  192.   fi
  193. }
  194.  
  195. #
  196. # Read defaults file from 'basedir'.   If there is no defaults file there
  197. # check if it's in the old (depricated) place (datadir) and read it from there
  198. #
  199.  
  200. extra_args=""
  201. if test -r "$basedir/my.cnf"
  202. then
  203.   extra_args="-e $basedir/my.cnf"
  204. else
  205.   if test -r "$datadir/my.cnf"
  206.   then
  207.     extra_args="-e $datadir/my.cnf"
  208.   fi
  209. fi
  210.  
  211. parse_server_arguments `$print_defaults $extra_args --mysqld mysql.server`
  212. parse_server_arguments "$@"
  213.  
  214. # wait for the pid file to disappear
  215. wait_for_gone () {
  216.   pid="$1"           # process ID of the program operating on the pid-file
  217.   pid_file_path="$2" # path to the PID file.
  218.  
  219.   i=0
  220.   crash_protection="by checking again"
  221.  
  222.   while test $i -ne $service_startup_timeout ; do
  223.  
  224.     if su_kill -0 "$pid" ; then
  225.       :  # the server still runs
  226.     else
  227.       if test ! -s "$pid_file_path"; then
  228.         # no server process and no pid-file? great, we're done!
  229.         log_success_msg
  230.         return 0
  231.       fi
  232.  
  233.       # pid-file exists, the server process doesn't.
  234.       # it must've crashed, and mysqld_safe will restart it
  235.       if test -n "$crash_protection"; then
  236.         crash_protection=""
  237.         sleep 5
  238.         continue  # Check again.
  239.       fi
  240.  
  241.       # Cannot help it
  242.       log_failure_msg "The server quit without updating PID file ($pid_file_path)."
  243.       return 1  # not waiting any more.
  244.     fi
  245.  
  246.     echo $echo_n ".$echo_c"
  247.     i=`expr $i + 1`
  248.     sleep 1
  249.  
  250.   done
  251.  
  252.   log_failure_msg
  253.   return 1
  254. }
  255.  
  256. wait_for_ready () {
  257.  
  258.   i=0
  259.   while test $i -ne $service_startup_timeout ; do
  260.  
  261.     if $bindir/mysqladmin ping >/dev/null 2>&1; then
  262.       log_success_msg
  263.       return 0
  264.     elif kill -0 $! ; then
  265.       :  # mysqld_safe is still running
  266.     else
  267.       # mysqld_safe is no longer running, abort the wait loop
  268.       break
  269.     fi
  270.  
  271.     echo $echo_n ".$echo_c"
  272.     i=`expr $i + 1`
  273.     sleep 1
  274.  
  275.   done
  276.  
  277.   log_failure_msg
  278.   return 1
  279. }
  280. #
  281. # Set pid file if not given
  282. #
  283. if test -z "$mysqld_pid_file_path"
  284. then
  285.   mysqld_pid_file_path=$datadir/`hostname`.pid
  286. else
  287.   case "$mysqld_pid_file_path" in
  288.     /* ) ;;
  289.     * )  mysqld_pid_file_path="$datadir/$mysqld_pid_file_path" ;;
  290.   esac
  291. fi
  292.  
  293. # source other config files
  294. [ -f /etc/default/mysql ] && . /etc/default/mysql
  295. [ -f /etc/sysconfig/mysql ] && . /etc/sysconfig/mysql
  296. [ -f /etc/conf.d/mysql ] && . /etc/conf.d/mysql
  297.  
  298. case "$mode" in
  299.   'start')
  300.     # Start daemon
  301.  
  302.     # Safeguard (relative paths, core dumps..)
  303.     cd $basedir
  304.  
  305.     echo $echo_n "Starting MariaDB"
  306.     if test -x $bindir/mysqld_safe
  307.     then
  308.       # Give extra arguments to mysqld with the my.cnf file. This script
  309.       # may be overwritten at next upgrade.
  310.       $bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" "$@" &
  311.       wait_for_ready; return_value=$?
  312.  
  313.       # Make lock for RedHat / SuSE
  314.       if test -w "$lockdir"
  315.       then
  316.         touch "$lock_file_path"
  317.       fi
  318.  
  319.       exit $return_value
  320.     else
  321.       log_failure_msg "Couldn't find MariaDB server ($bindir/mysqld_safe)"
  322.     fi
  323.     ;;
  324.  
  325.   'stop')
  326.     # Stop daemon. We use a signal here to avoid having to know the
  327.     # root password.
  328.  
  329.     if test -s "$mysqld_pid_file_path"
  330.     then
  331.       mysqld_pid=`cat "$mysqld_pid_file_path"`
  332.  
  333.       if su_kill -0 $mysqld_pid ; then
  334.         echo $echo_n "Shutting down MariaDB"
  335.         su_kill $mysqld_pid
  336.         # mysqld should remove the pid file when it exits, so wait for it.
  337.         wait_for_gone $mysqld_pid "$mysqld_pid_file_path"; return_value=$?
  338.       else
  339.         log_failure_msg "MariaDB server process #$mysqld_pid is not running!"
  340.         rm "$mysqld_pid_file_path"
  341.       fi
  342.  
  343.       # Delete lock for RedHat / SuSE
  344.       if test -f "$lock_file_path"
  345.       then
  346.         rm -f "$lock_file_path"
  347.       fi
  348.       exit $return_value
  349.     else
  350.       log_failure_msg "MariaDB server PID file could not be found!"
  351.     fi
  352.     ;;
  353.  
  354.   'restart')
  355.     # Stop the service and regardless of whether it was
  356.     # running or not, start it again.
  357.     if $0 stop  "$@"; then
  358.       if ! $0 start "$@"; then
  359.         log_failure_msg "Failed to restart server."
  360.         exit 1
  361.       fi
  362.     else
  363.       log_failure_msg "Failed to stop running server, so refusing to try to start."
  364.       exit 1
  365.     fi
  366.     ;;
  367.  
  368.   'reload'|'force-reload')
  369.     if test -s "$mysqld_pid_file_path" ; then
  370.       read mysqld_pid <  "$mysqld_pid_file_path"
  371.       su_kill -HUP $mysqld_pid && log_success_msg "Reloading service MariaDB"
  372.       touch "$mysqld_pid_file_path"
  373.     else
  374.       log_failure_msg "MariaDB PID file could not be found!"
  375.       exit 1
  376.     fi
  377.     ;;
  378.   'status')
  379.     # First, check to see if pid file exists
  380.     if test -s "$mysqld_pid_file_path" ; then
  381.       read mysqld_pid < "$mysqld_pid_file_path"
  382.       if su_kill -0 $mysqld_pid ; then
  383.         log_success_msg "MariaDB running ($mysqld_pid)"
  384.         exit 0
  385.       else
  386.         log_failure_msg "MariaDB is not running, but PID file exists"
  387.         exit 1
  388.       fi
  389.     else
  390.       # Try to find appropriate mysqld process
  391.       mysqld_pid=`pgrep $libexecdir/mysqld`
  392.  
  393.       # test if multiple pids exist
  394.       pid_count=`echo $mysqld_pid | wc -w`
  395.       if test $pid_count -gt 1 ; then
  396.         log_failure_msg "Multiple MariaDB running but PID file could not be found ($mysqld_pid)"
  397.         exit 5
  398.       elif test -z $mysqld_pid ; then
  399.         if test -f "$lock_file_path" ; then
  400.           log_failure_msg "MariaDB is not running, but lock file ($lock_file_path) exists"
  401.           exit 2
  402.         fi
  403.         log_failure_msg "MariaDB is not running"
  404.         exit 3
  405.       else
  406.         log_failure_msg "MariaDB is running but PID file could not be found"
  407.         exit 4
  408.       fi
  409.     fi
  410.     ;;
  411.   'configtest')
  412.     # Safeguard (relative paths, core dumps..)
  413.     cd $basedir
  414.     echo $echo_n "Testing MariaDB configuration syntax"
  415.     daemon=$bindir/mysqld
  416.     if test -x $libexecdir/mysqld
  417.     then
  418.       daemon=$libexecdir/mysqld
  419.     elif test -x $sbindir/mysqld
  420.     then
  421.       daemon=$sbindir/mysqld
  422.     elif test -x `which mysqld`
  423.     then
  424.       daemon=`which mysqld`
  425.     else
  426.       log_failure_msg "Unable to locate the mysqld binary!"
  427.       exit 1
  428.     fi
  429.     help_out=`$daemon --help 2>&1`; r=$?
  430.     if test "$r" != 0 ; then
  431.       log_failure_msg "$help_out"
  432.       log_failure_msg "There are syntax errors in the server configuration. Please fix them!"
  433.     else
  434.       log_success_msg "Syntax OK"
  435.     fi
  436.     exit $r
  437.     ;;
  438.   'bootstrap')
  439.       if test "$_use_systemctl" == 1 ; then
  440.         log_failure_msg "Please use galera_new_cluster to start the mariadb service with --wsrep-new-cluster"
  441.         exit 1
  442.       fi
  443.       # Bootstrap the cluster, start the first node
  444.       # that initiate the cluster
  445.       echo $echo_n "Bootstrapping the cluster.. "
  446.       $0 start $other_args --wsrep-new-cluster
  447.       exit $?
  448.       ;;
  449.   *)
  450.       # usage
  451.       basename=`basename "$0"`
  452.       echo "Usage: $basename  {start|stop|restart|reload|force-reload|status|configtest|bootstrap}  [ MariaDB server options ]"
  453.       exit 1
  454.     ;;
  455. esac
  456.  
  457. exit 0
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top