论坛风格切换
  • 5699阅读
  • 0回复

[mysql]linux下mysql启动工具 [复制链接]

上一主题 下一主题
离线太史慈
 

发帖
766
金币
626
威望
556
只看楼主 倒序阅读 使用道具 楼主  发表于: 2011-09-29
mysql.server

  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. # MySQL daemon start/stop script.
  5. # Usually this is put in /etc/init.d (at least on machines SYSV R4 based
  6. # systems) and linked to /etc/rc3.d/S99mysql and /etc/rc0.d/K01mysql.
  7. # When this is done the mysql server will be started when the machine is
  8. # started and shut down when the systems goes down.
  9. # Comments to support chkconfig on RedHat Linux
  10. # chkconfig: 2345 64 36
  11. # description: A very fast and reliable SQL database engine.
  12. # Comments to support LSB init script conventions
  13. ### BEGIN INIT INFO
  14. # Provides: mysql
  15. # Required-Start: $local_fs $network $remote_fs
  16. # Should-Start: ypbind nscd ldap ntpd xntpd
  17. # Required-Stop: $local_fs $network $remote_fs
  18. # Default-Start:  2 3 4 5
  19. # Default-Stop: 0 1 6
  20. # Short-Description: start and stop MySQL
  21. # Description: MySQL is a very fast and reliable SQL database engine.
  22. ### END INIT INFO
  23. # If you install MySQL on some other places than /aliyun/webserver/mysql-5.1.57, then you
  24. # have to do one of the following things for this script to work:
  25. #
  26. # - Run this script from within the MySQL installation directory
  27. # - Create a /etc/my.cnf file with the following information:
  28. #   [mysqld]
  29. #   basedir=<path-to-mysql-installation-directory>
  30. # - Add the above to any other configuration file (for example ~/.my.ini)
  31. #   and copy my_print_defaults to /usr/bin
  32. # - Add the path to the mysql-installation-directory to the basedir variable
  33. #   below.
  34. #
  35. # If you want to affect other MySQL variables, you should make your changes
  36. # in the /etc/my.cnf, ~/.my.cnf or other MySQL configuration files.
  37. # If you change base dir, you must also change datadir. These may get
  38. # overwritten by settings in the MySQL configuration files.
  39. basedir=
  40. datadir=
  41. # Default value, in seconds, afterwhich the script should timeout waiting
  42. # for server start.
  43. # Value here is overriden by value in my.cnf.
  44. # 0 means don't wait at all
  45. # Negative numbers mean to wait indefinitely
  46. service_startup_timeout=900
  47. # The following variables are only set for letting mysql.server find things.
  48. # Set some defaults
  49. pid_file=
  50. server_pid_file=
  51. use_mysqld_safe=1
  52. user=mysql
  53. if test -z "$basedir"
  54. then
  55.   basedir=/aliyun/webserver/mysql-5.1.57
  56.   bindir=/aliyun/webserver/mysql-5.1.57/bin
  57.   if test -z "$datadir"
  58.   then
  59.     datadir=/aliyun/webserver/mysql-5.1.57/var
  60.   fi
  61.   sbindir=/aliyun/webserver/mysql-5.1.57/sbin
  62.   libexecdir=/aliyun/webserver/mysql-5.1.57/libexec
  63. else
  64.   bindir="$basedir/bin"
  65.   if test -z "$datadir"
  66.   then
  67.     datadir="$basedir/data"
  68.   fi
  69.   sbindir="$basedir/sbin"
  70.   libexecdir="$basedir/libexec"
  71. fi
  72. # datadir_set is used to determine if datadir was set (and so should be
  73. # *not* set inside of the --basedir= handler.)
  74. datadir_set=
  75. #
  76. # Use LSB init script functions for printing messages, if possible
  77. #
  78. lsb_functions="/lib/lsb/init-functions"
  79. if test -f $lsb_functions ; then
  80.   . $lsb_functions
  81. else
  82.   log_success_msg()
  83.   {
  84.     echo " SUCCESS! $@"
  85.   }
  86.   log_failure_msg()
  87.   {
  88.     echo " ERROR! $@"
  89.   }
  90. fi
  91. PATH=/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin
  92. export PATH
  93. mode=$1    # start or stop
  94. shift
  95. other_args="$*"   # uncommon, but needed when called from an RPM upgrade action
  96.            # Expected: "--skip-networking --skip-grant-tables"
  97.            # They are not checked here, intentionally, as it is the resposibility
  98.            # of the "spec" file author to give correct arguments only.
  99. case `echo "testing\c"`,`echo -n testing` in
  100.     *c*,-n*) echo_n=   echo_c=     ;;
  101.     *c*,*)   echo_n=-n echo_c=     ;;
  102.     *)       echo_n=   echo_c='\c' ;;
  103. esac
  104. parse_server_arguments() {
  105.   for arg do
  106.     case "$arg" in
  107.       --basedir=*)  basedir=`echo "$arg" | sed -e 's/^[^=]*=//'`
  108.                     bindir="$basedir/bin"
  109.       if test -z "$datadir_set"; then
  110.         datadir="$basedir/data"
  111.       fi
  112.       sbindir="$basedir/sbin"
  113.       libexecdir="$basedir/libexec"
  114.         ;;
  115.       --datadir=*)  datadir=`echo "$arg" | sed -e 's/^[^=]*=//'`
  116.       datadir_set=1
  117. ;;
  118.       --user=*)  user=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
  119.       --pid-file=*) server_pid_file=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
  120.       --service-startup-timeout=*) service_startup_timeout=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
  121.       --use-mysqld_safe) use_mysqld_safe=1;;
  122.       --use-manager)     use_mysqld_safe=0;;
  123.     esac
  124.   done
  125. }
  126. parse_manager_arguments() {
  127.   for arg do
  128.     case "$arg" in
  129.       --pid-file=*) pid_file=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
  130.       --user=*)  user=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
  131.     esac
  132.   done
  133. }
  134. wait_for_pid () {
  135.   verb="$1"
  136.   manager_pid="$2"  # process ID of the program operating on the pid-file
  137.   i=0
  138.   avoid_race_condition="by checking again"
  139.   while test $i -ne $service_startup_timeout ; do
  140.     case "$verb" in
  141.       'created')
  142.         # wait for a PID-file to pop into existence.
  143.         test -s $pid_file && i='' && break
  144.         ;;
  145.       'removed')
  146.         # wait for this PID-file to disappear
  147.         test ! -s $pid_file && i='' && break
  148.         ;;
  149.       *)
  150.         echo "wait_for_pid () usage: wait_for_pid created|removed manager_pid"
  151.         exit 1
  152.         ;;
  153.     esac
  154.     # if manager isn't running, then pid-file will never be updated
  155.     if test -n "$manager_pid"; then
  156.       if kill -0 "$manager_pid" 2>/dev/null; then
  157.         :  # the manager still runs
  158.       else
  159.         # The manager may have exited between the last pid-file check and now.  
  160.         if test -n "$avoid_race_condition"; then
  161.           avoid_race_condition=""
  162.           continue  # Check again.
  163.         fi
  164.         # there's nothing that will affect the file.
  165.         log_failure_msg "Manager of pid-file quit without updating file."
  166.         return 1  # not waiting any more.
  167.       fi
  168.     fi
  169.     echo $echo_n ".$echo_c"
  170.     i=`expr $i + 1`
  171.     sleep 1
  172.   done
  173.   if test -z "$i" ; then
  174.     log_success_msg
  175.     return 0
  176.   else
  177.     log_failure_msg
  178.     return 1
  179.   fi
  180. }
  181. # Get arguments from the my.cnf file,
  182. # the only group, which is read from now on is [mysqld]
  183. if test -x ./bin/my_print_defaults
  184. then
  185.   print_defaults="./bin/my_print_defaults"
  186. elif test -x $bindir/my_print_defaults
  187. then
  188.   print_defaults="$bindir/my_print_defaults"
  189. elif test -x $bindir/mysql_print_defaults
  190. then
  191.   print_defaults="$bindir/mysql_print_defaults"
  192. else
  193.   # Try to find basedir in /etc/my.cnf
  194.   conf=/etc/my.cnf
  195.   print_defaults=
  196.   if test -r $conf
  197.   then
  198.     subpat='^[^=]*basedir[^=]*=\(.*\)$'
  199.     dirs=`sed -e "/$subpat/!d" -e 's//\1/' $conf`
  200.     for d in $dirs
  201.     do
  202.       d=`echo $d | sed -e 's/[  ]//g'`
  203.       if test -x "$d/bin/my_print_defaults"
  204.       then
  205.         print_defaults="$d/bin/my_print_defaults"
  206.         break
  207.       fi
  208.       if test -x "$d/bin/mysql_print_defaults"
  209.       then
  210.         print_defaults="$d/bin/mysql_print_defaults"
  211.         break
  212.       fi
  213.     done
  214.   fi
  215.   # Hope it's in the PATH ... but I doubt it
  216.   test -z "$print_defaults" && print_defaults="my_print_defaults"
  217. fi
  218. #
  219. # Read defaults file from 'basedir'.   If there is no defaults file there
  220. # check if it's in the old (depricated) place (datadir) and read it from there
  221. #
  222. extra_args=""
  223. if test -r "$basedir/my.cnf"
  224. then
  225.   extra_args="-e $basedir/my.cnf"
  226. else
  227.   if test -r "$datadir/my.cnf"
  228.   then
  229.     extra_args="-e $datadir/my.cnf"
  230.   fi
  231. fi
  232. parse_server_arguments `$print_defaults $extra_args mysqld server mysql_server mysql.server`
  233. # Look for the pidfile
  234. parse_manager_arguments `$print_defaults $extra_args manager`
  235. #
  236. # Set pid file if not given
  237. #
  238. if test -z "$pid_file"
  239. then
  240.   pid_file=$datadir/mysqlmanager-`/bin/hostname`.pid
  241. else
  242.   case "$pid_file" in
  243.     /* ) ;;
  244.     * )  pid_file="$datadir/$pid_file" ;;
  245.   esac
  246. fi
  247. if test -z "$server_pid_file"
  248. then
  249.   server_pid_file=$datadir/`/bin/hostname`.pid
  250. else
  251.   case "$server_pid_file" in
  252.     /* ) ;;
  253.     * )  server_pid_file="$datadir/$server_pid_file" ;;
  254.   esac
  255. fi
  256. case "$mode" in
  257.   'start')
  258.     # Start daemon
  259.     # Safeguard (relative paths, core dumps..)
  260.     cd $basedir
  261.     manager=$bindir/mysqlmanager
  262.     if test -x $libexecdir/mysqlmanager
  263.     then
  264.       manager=$libexecdir/mysqlmanager
  265.     elif test -x $sbindir/mysqlmanager
  266.     then
  267.       manager=$sbindir/mysqlmanager
  268.     fi
  269.     echo $echo_n "Starting MySQL"
  270.     if test -x $manager -a "$use_mysqld_safe" = "0"
  271.     then
  272.       if test -n "$other_args"
  273.       then
  274.         log_failure_msg "MySQL manager does not support options '$other_args'"
  275.         exit 1
  276.       fi
  277.       # Give extra arguments to mysqld with the my.cnf file. This script may
  278.       # be overwritten at next upgrade.
  279.       "$manager" \
  280.         --mysqld-safe-compatible \
  281.         --user="$user" \
  282.         --pid-file="$pid_file" >/dev/null 2>&1 &
  283.       wait_for_pid created $!; return_value=$?
  284.       # Make lock for RedHat / SuSE
  285.       if test -w /var/lock/subsys
  286.       then
  287.         touch /var/lock/subsys/mysqlmanager
  288.       fi
  289.       exit $return_value
  290.     elif test -x $bindir/mysqld_safe
  291.     then
  292.       # Give extra arguments to mysqld with the my.cnf file. This script
  293.       # may be overwritten at next upgrade.
  294.       pid_file=$server_pid_file
  295.       $bindir/mysqld_safe --datadir=$datadir --pid-file=$server_pid_file $other_args >/dev/null 2>&1 &
  296.       wait_for_pid created $!; return_value=$?
  297.       # Make lock for RedHat / SuSE
  298.       if test -w /var/lock/subsys
  299.       then
  300.         touch /var/lock/subsys/mysql
  301.       fi
  302.       exit $return_value
  303.     else
  304.       log_failure_msg "Couldn't find MySQL manager ($manager) or server ($bindir/mysqld_safe)"
  305.     fi
  306.     ;;
  307.   'stop')
  308.     # Stop daemon. We use a signal here to avoid having to know the
  309.     # root password.
  310.     # The RedHat / SuSE lock directory to remove
  311.     lock_dir=/var/lock/subsys/mysqlmanager
  312.     # If the manager pid_file doesn't exist, try the server's
  313.     if test ! -s "$pid_file"
  314.     then
  315.       pid_file=$server_pid_file
  316.       lock_dir=/var/lock/subsys/mysql
  317.     fi
  318.     if test -s "$pid_file"
  319.     then
  320.       mysqlmanager_pid=`cat $pid_file`
  321.       
  322.       if (kill -0 $mysqlmanager_pid 2>/dev/null)
  323.       then
  324.         echo $echo_n "Shutting down MySQL"
  325.         kill $mysqlmanager_pid
  326.         # mysqlmanager should remove the pid_file when it exits, so wait for it.
  327.         wait_for_pid removed "$mysqlmanager_pid"; return_value=$?
  328.       else
  329.         log_failure_msg "MySQL manager or server process #$mysqlmanager_pid is not running!"
  330.         rm $pid_file
  331.       fi
  332.       
  333.       # delete lock for RedHat / SuSE
  334.       if test -f $lock_dir
  335.       then
  336.         rm -f $lock_dir
  337.       fi
  338.       exit $return_value
  339.     else
  340.       log_failure_msg "MySQL manager or server PID file could not be found!"
  341.     fi
  342.     ;;
  343.   'restart')
  344.     # Stop the service and regardless of whether it was
  345.     # running or not, start it again.
  346.     if $0 stop  $other_args; then
  347.       $0 start $other_args
  348.     else
  349.       log_failure_msg "Failed to stop running server, so refusing to try to start."
  350.       exit 1
  351.     fi
  352.     ;;
  353.   'reload'|'force-reload')
  354.     if test -s "$server_pid_file" ; then
  355.       read mysqld_pid <  $server_pid_file
  356.       kill -HUP $mysqld_pid && log_success_msg "Reloading service MySQL"
  357.       touch $server_pid_file
  358.     else
  359.       log_failure_msg "MySQL PID file could not be found!"
  360.       exit 1
  361.     fi
  362.     ;;
  363.   'status')
  364.     # First, check to see if pid file exists
  365.     if test -s "$server_pid_file" ; then
  366.       read mysqld_pid < $server_pid_file
  367.       if kill -0 $mysqld_pid 2>/dev/null ; then
  368.         log_success_msg "MySQL running ($mysqld_pid)"
  369.         exit 0
  370.       else
  371.         log_failure_msg "MySQL is not running, but PID file exists"
  372.         exit 1
  373.       fi
  374.     else
  375.       # Try to find appropriate mysqld process
  376.       mysqld_pid=`pidof $libexecdir/mysqld`
  377.       if test -z $mysqld_pid ; then
  378.         if test "$use_mysqld_safe" = "0" ; then
  379.           lockfile=/var/lock/subsys/mysqlmanager
  380.         else
  381.           lockfile=/var/lock/subsys/mysql
  382.         fi
  383.         if test -f $lockfile ; then
  384.           log_failure_msg "MySQL is not running, but lock exists"
  385.           exit 2
  386.         fi
  387.         log_failure_msg "MySQL is not running"
  388.         exit 3
  389.       else
  390.         log_failure_msg "MySQL is running but PID file could not be found"
  391.         exit 4
  392.       fi
  393.     fi
  394.     ;;
  395.     *)
  396.       # usage
  397.       echo "Usage: $0  {start|stop|restart|reload|force-reload|status}  [ MySQL server options ]"
  398.       exit 1
  399.     ;;
  400. esac
  401. exit 0

附件: mysql.zip (5 K) 下载次数:66
[url=http://www.phpwind.com]phpwind[/url]
快速回复
限100 字节
如果您在写长篇帖子又不马上发表,建议存为草稿
 
提到某人:
选择好友
上一个 下一个