Advertisement
nazariy

MySQL bash

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