Advertisement
Guest User

/etc/init.d/mysql

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