UocXLhefENcxJWtIOW92

mariaDB1.14 init.d script

Apr 1st, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.99 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. ### BEGIN INIT INFO
  4. # Provides:          mysql
  5. # Required-Start:    $remote_fs $syslog
  6. # Required-Stop:     $remote_fs $syslog
  7. # Should-Start:      $network $named $time
  8. # Should-Stop:       $network $named $time
  9. # Default-Start:     2 3 4 5
  10. # Default-Stop:      0 1 6
  11. # Short-Description: Start and stop the mysql database server daemon
  12. # Description:       Controls the main MariaDB database server daemon "mysqld"
  13. #                    and its wrapper script "mysqld_safe".
  14. ### END INIT INFO
  15. #
  16. set -e
  17. set -u
  18. ${DEBIAN_SCRIPT_DEBUG:+ set -v -x}
  19.  
  20. test -x /usr/sbin/mysqld || exit 0
  21.  
  22. . /lib/lsb/init-functions
  23.  
  24. SELF=$(cd $(dirname $0); pwd -P)/$(basename $0)
  25.  
  26. MYADMIN="/usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf"
  27.  
  28. # priority can be overridden and "-s" adds output to stderr
  29. ERR_LOGGER="logger -p daemon.err -t /etc/init.d/mysql -i"
  30.  
  31. if [ -f /etc/default/mysql ]; then
  32.   . /etc/default/mysql
  33. fi
  34.  
  35. # Also source default/mariadb in case the installation was upgraded from
  36. # packages originally installed from MariaDB.org repositories, which have
  37. # had support for reading /etc/default/mariadb since March 2016.
  38. if [ -f /etc/default/mariadb ]; then
  39.   . /etc/default/mariadb
  40. fi
  41.  
  42. # Safeguard (relative paths, core dumps..)
  43. cd /
  44. umask 077
  45.  
  46. # mysqladmin likes to read /root/.my.cnf. This is usually not what I want
  47. # as many admins e.g. only store a password without a username there and
  48. # so break my scripts.
  49. export HOME=/etc/mysql/
  50.  
  51. ## Fetch a particular option from mysql's invocation.
  52. #
  53. # Usage: void mysqld_get_param option
  54. mysqld_get_param() {
  55.   /usr/sbin/mysqld --print-defaults \
  56.     | tr " " "\n" \
  57.     | grep -- "--$1" \
  58.     | tail -n 1 \
  59.     | cut -d= -f2
  60. }
  61.  
  62. ## Do some sanity checks before even trying to start mysqld.
  63. sanity_checks() {
  64.   # check for config file
  65.   if [ ! -r /etc/mysql/my.cnf ]; then
  66.     log_warning_msg "$0: WARNING: /etc/mysql/my.cnf cannot be read. See README.Debian.gz"
  67.     echo                "WARNING: /etc/mysql/my.cnf cannot be read. See README.Debian.gz" | $ERR_LOGGER
  68.   fi
  69.  
  70.   # check for diskspace shortage
  71.   datadir=`mysqld_get_param datadir`
  72.   if LC_ALL=C BLOCKSIZE= df --portability $datadir/. | tail -n 1 | awk '{ exit ($4>4096) }'; then
  73.     log_failure_msg "$0: ERROR: The partition with $datadir is too full!"
  74.     echo                "ERROR: The partition with $datadir is too full!" | $ERR_LOGGER
  75.     exit 1
  76.   fi
  77. }
  78.  
  79. ## Checks if there is a server running and if so if it is accessible.
  80. #
  81. # check_alive insists on a pingable server
  82. # check_dead also fails if there is a lost mysqld in the process list
  83. #
  84. # Usage: boolean mysqld_status [check_alive|check_dead] [warn|nowarn]
  85. mysqld_status () {
  86.   ping_output=`$MYADMIN ping 2>&1`; ping_alive=$(( ! $? ))
  87.  
  88.   ps_alive=0
  89.   pidfile=`mysqld_get_param pid-file`
  90.   if [ -f "$pidfile" ] && ps `cat $pidfile` >/dev/null 2>&1; then ps_alive=1; fi
  91.  
  92.   if [ "$1" = "check_alive"  -a  $ping_alive = 1 ] ||
  93.      [ "$1" = "check_dead"   -a  $ping_alive = 0  -a  $ps_alive = 0 ]; then
  94.     return 0 # EXIT_SUCCESS
  95.   else
  96.     if [ "$2" = "warn" ]; then
  97.       echo -e "$ps_alive processes alive and '$MYADMIN ping' resulted in\n$ping_output\n" | $ERR_LOGGER -p daemon.debug
  98.     fi
  99.   return 1 # EXIT_FAILURE
  100.   fi
  101. }
  102.  
  103. #
  104. # main()
  105. #
  106.  
  107. case "${1:-''}" in
  108.  
  109.   'start')
  110.   sanity_checks;
  111.   # Start daemon
  112.   log_daemon_msg "Starting MariaDB database server" "mysqld"
  113.   if mysqld_status check_alive nowarn; then
  114.    log_progress_msg "already running"
  115.    log_end_msg 0
  116.   else
  117.     # Could be removed during boot
  118.     test -e /var/run/mysqld || install -m 755 -o mysql -g root -d /var/run/mysqld
  119.  
  120.     # Start MariaDB!
  121.     /usr/bin/mysqld_safe "${@:2}" 2>&1 >/dev/null | $ERR_LOGGER &
  122.  
  123.     for i in $(seq 1 "${MYSQLD_STARTUP_TIMEOUT:-30}"); do
  124.       sleep 1
  125.       if mysqld_status check_alive nowarn ; then break; fi
  126.       log_progress_msg "."
  127.     done
  128.     if mysqld_status check_alive warn; then
  129.       log_end_msg 0
  130.       # Now start mysqlcheck or whatever the admin wants.
  131.       output=$(/etc/mysql/debian-start)
  132.       if [ -n "$output" ]; then
  133.         log_action_msg "$output"
  134.       fi
  135.     else
  136.       log_end_msg 1
  137.       log_failure_msg "Please take a look at the syslog"
  138.     fi
  139.   fi
  140.   ;;
  141.  
  142.   'stop')
  143.   # * As a passwordless mysqladmin (e.g. via ~/.my.cnf) must be possible
  144.   # at least for cron, we can rely on it here, too. (although we have
  145.   # to specify it explicit as e.g. sudo environments points to the normal
  146.   # users home and not /root)
  147.   log_daemon_msg "Stopping MariaDB database server" "mysqld"
  148.   if ! mysqld_status check_dead nowarn; then
  149.     set +e
  150.     shutdown_out=`$MYADMIN shutdown 2>&1`; r=$?
  151.     set -e
  152.     if [ "$r" -ne 0 ]; then
  153.       log_end_msg 1
  154.       [ "$VERBOSE" != "no" ] && log_failure_msg "Error: $shutdown_out"
  155.       log_daemon_msg "Killing MariaDB database server by signal" "mysqld"
  156.       killall -15 mysqld
  157.       server_down=
  158.       for i in `seq 1 600`; do
  159.         sleep 1
  160.         if mysqld_status check_dead nowarn; then server_down=1; break; fi
  161.       done
  162.       if test -z "$server_down"; then killall -9 mysqld; fi
  163.     fi
  164.   fi
  165.  
  166.   if ! mysqld_status check_dead warn; then
  167.     log_end_msg 1
  168.     log_failure_msg "Please stop MariaDB manually and read /usr/share/doc/mariadb-server-10.1/README.Debian.gz!"
  169.     exit -1
  170.   else
  171.     log_end_msg 0
  172.   fi
  173.   ;;
  174.  
  175.   'restart')
  176.   set +e; $SELF stop; set -e
  177.   shift
  178.   $SELF start "${@}"
  179.   ;;
  180.  
  181.   'reload'|'force-reload')
  182.   log_daemon_msg "Reloading MariaDB database server" "mysqld"
  183.   $MYADMIN reload
  184.   log_end_msg 0
  185.   ;;
  186.  
  187.   'status')
  188.   if mysqld_status check_alive nowarn; then
  189.     log_action_msg "$($MYADMIN version)"
  190.   else
  191.     log_action_msg "MariaDB is stopped."
  192.     exit 3
  193.   fi
  194.   ;;
  195.  
  196.   'bootstrap')
  197.     # Bootstrap the cluster, start the first node
  198.     # that initiates the cluster
  199.     log_daemon_msg "Bootstrapping the cluster" "mysqld"
  200.     $SELF start "${@:2}" --wsrep-new-cluster
  201.     ;;
  202.  
  203.   *)
  204.   echo "Usage: $SELF start|stop|restart|reload|force-reload|status|bootstrap"
  205.   exit 1
  206.   ;;
  207. esac
Add Comment
Please, Sign In to add comment