免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4838 | 回复: 1
打印 上一主题 下一主题

多实例版mysql启动重启关闭脚本mysql.server [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-26 18:42 |只看该作者 |倒序浏览
MySQL自带了一个mysql.server脚本,用来启动、重启、关闭mysql服务

不过这个脚本监听默认的3306端口,以及只用/tmp/mysql.sock

在一台服务器上跑多个MySQL实例的情况下,使用这个脚本就会造成冲突。

对mysql.server脚本大概读了一下,将之修改成可以适用于一台服务器跑多个MySQL的环境

需要配置脚本中的开头basedir和datadir两个参数,脚本将优先从$datadir/my.cnf中读取配置,解析my.cnf中端口和socket配置,

my.cnf示例如下:

[mysqld]

port=3307

socket=/your/mysql/data/mysql.sock  #可以把mysql.sock放到$datadir所在目录中



  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 /usr/local/mysql, 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=/Data/apps/mysql-5.0.41-icc/
  40. datadir=/Data/apps/mysql-5.0.41-icc/data_23301

  41. # 脚本优先从$datadir所在目录中查找my.cnf
  42. # port和socket将从my.cnf中解析
  43. # [mysqld]
  44. # port=3307
  45. # socket=/your/mysql/data/mysql.sock  #可以把mysql.sock放到$datadir所在目录中


  46. # Default value, in seconds, afterwhich the script should timeout waiting
  47. # for server start.
  48. # Value here is overriden by value in my.cnf.
  49. # 0 means don't wait at all
  50. # Negative numbers mean to wait indefinitely
  51. service_startup_timeout=900

  52. # The following variables are only set for letting mysql.server find things.

  53. # Set some defaults
  54. pid_file=mysql.pid
  55. server_pid_file=mysql.pid
  56. use_mysqld_safe=1
  57. user=mysql
  58. if test -z "$basedir"
  59. then
  60.   basedir=/usr/local/mysql
  61.   bindir=./bin
  62.   if test -z "$datadir"
  63.   then
  64.     datadir=/usr/local/mysql/data
  65.   fi
  66.   sbindir=./bin
  67.   libexecdir=./bin
  68. else
  69.   bindir="$basedir/bin"
  70.   if test -z "$datadir"
  71.   then
  72.     datadir="$basedir/data"
  73.   fi
  74.   sbindir="$basedir/sbin"
  75.   libexecdir="$basedir/libexec"
  76. fi

  77. # datadir_set is used to determine if datadir was set (and so should be
  78. # *not* set inside of the --basedir= handler.)
  79. datadir_set=

  80. #
  81. # Use LSB init script functions for printing messages, if possible
  82. #
  83. lsb_functions="/lib/lsb/init-functions"
  84. if test -f $lsb_functions ; then
  85.   . $lsb_functions
  86. else
  87.   log_success_msg()
  88.   {
  89.     echo " SUCCESS! $@"
  90.   }
  91.   log_failure_msg()
  92.   {
  93.     echo " ERROR! $@"
  94.   }
  95. fi

  96. PATH=/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin
  97. export PATH

  98. mode=$1    # start or stop
  99. shift
  100. other_args="$*"   # uncommon, but needed when called from an RPM upgrade action
  101.            # Expected: "--skip-networking --skip-grant-tables"
  102.            # They are not checked here, intentionally, as it is the resposibility
  103.            # of the "spec" file author to give correct arguments only.

  104. case `echo "testing\c"`,`echo -n testing` in
  105.     *c*,-n*) echo_n=   echo_c=     ;;
  106.     *c*,*)   echo_n=-n echo_c=     ;;
  107.     *)       echo_n=   echo_c='\c' ;;
  108. esac

  109. parse_server_arguments() {
  110.   for arg do
  111.     case "$arg" in
  112.       --basedir=*)  basedir=`echo "$arg" | sed -e 's/^[^=]*=//'`
  113.                     bindir="$basedir/bin"
  114.             if test -z "$datadir_set"; then
  115.               datadir="$basedir/data"
  116.             fi
  117.             sbindir="$basedir/sbin"
  118.             libexecdir="$basedir/libexec"
  119.         ;;
  120.       --datadir=*)  datadir=`echo "$arg" | sed -e 's/^[^=]*=//'`
  121.             datadir_set=1
  122.     ;;
  123.       --user=*)  user=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
  124.       --port=*)  port=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
  125.       --socket=*)  socket=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
  126.       --pid-file=*) server_pid_file=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
  127.       --service-startup-timeout=*) service_startup_timeout=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
  128.       --use-mysqld_safe) use_mysqld_safe=1;;
  129.       --use-manager)     use_mysqld_safe=0;;
  130.     esac
  131.   done
  132. }

  133. parse_manager_arguments() {
  134.   for arg do
  135.     case "$arg" in
  136.       --pid-file=*) pid_file=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
  137.       --user=*)  user=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
  138.     esac
  139.   done
  140. }

  141. wait_for_pid () {
  142.   i=0
  143.   while test $i -ne $service_startup_timeout ; do
  144.     sleep 1
  145.     case "$1" in
  146.       'created')
  147.         test -s $pid_file && i='' && break
  148.         kill -0 $2 || break # if the program goes away, stop waiting
  149.         ;;
  150.       'removed')
  151.         test ! -s $pid_file && i='' && break
  152.         ;;
  153.       *)
  154.         echo "wait_for_pid () usage: wait_for_pid created|removed"
  155.         exit 1
  156.         ;;
  157.     esac
  158.     echo $echo_n ".$echo_c"
  159.     i=`expr $i + 1`
  160.   done

  161.   if test -z "$i" ; then
  162.     log_success_msg
  163.     return 0
  164.   else
  165.     log_failure_msg
  166.     return 1
  167.   fi
  168. }

  169. # Get arguments from the my.cnf file,
  170. # the only group, which is read from now on is [mysqld]
  171. if test -x ./bin/my_print_defaults
  172. then
  173.   print_defaults="./bin/my_print_defaults"
  174. elif test -x $bindir/my_print_defaults
  175. then
  176.   print_defaults="$bindir/my_print_defaults"
  177. elif test -x $bindir/mysql_print_defaults
  178. then
  179.   print_defaults="$bindir/mysql_print_defaults"
  180. else
  181.   # Try to find basedir in /etc/my.cnf
  182.   conf=/etc/my.cnf
  183.   print_defaults=
  184.   if test -r $conf
  185.   then
  186.     subpat='^[^=]*basedir[^=]*=\(.*\)$'
  187.     dirs=`sed -e "/$subpat/!d" -e 's//\1/' $conf`
  188.     for d in $dirs
  189.     do
  190.       d=`echo $d | sed -e 's/[     ]//g'`
  191.       if test -x "$d/bin/my_print_defaults"
  192.       then
  193.         print_defaults="$d/bin/my_print_defaults"
  194.         break
  195.       fi
  196.       if test -x "$d/bin/mysql_print_defaults"
  197.       then
  198.         print_defaults="$d/bin/mysql_print_defaults"
  199.         break
  200.       fi
  201.     done
  202.   fi

  203.   # Hope it's in the PATH ... but I doubt it
  204.   test -z "$print_defaults" && print_defaults="my_print_defaults"
  205. fi

  206. #
  207. # Read defaults file from 'basedir'.   If there is no defaults file there
  208. # check if it's in the old (depricated) place (datadir) and read it from there
  209. #

  210. extra_args=""
  211. if test -r "$basedir/my.cnf"
  212. then
  213.   extra_args="-e $basedir/my.cnf"
  214. else
  215.   if test -r "$datadir/my.cnf"
  216.   then
  217.     extra_args="-e $datadir/my.cnf"
  218.   fi
  219. fi

  220. parse_server_arguments `$print_defaults $extra_args mysqld server mysql_server mysql.server`

  221. # Look for the pidfile
  222. parse_manager_arguments `$print_defaults $extra_args manager`

  223. #
  224. # Set pid file if not given
  225. #
  226. if test -z "$pid_file"
  227. then
  228.   pid_file=$datadir/mysqlmanager-`/bin/hostname`.pid
  229. else
  230.   case "$pid_file" in
  231.     /* ) ;;
  232.     * )  pid_file="$datadir/$pid_file" ;;
  233.   esac
  234. fi
  235. if test -z "$server_pid_file"
  236. then
  237.   server_pid_file=$datadir/`/bin/hostname`.pid
  238. else
  239.   case "$server_pid_file" in
  240.     /* ) ;;
  241.     * )  server_pid_file="$datadir/$server_pid_file" ;;
  242.   esac
  243. fi

  244. # Safeguard (relative paths, core dumps..)
  245. cd $basedir

  246. case "$mode" in
  247.   'start')
  248.     # Start daemon

  249.     manager=$bindir/mysqlmanager
  250.     if test -x $libexecdir/mysqlmanager
  251.     then
  252.       manager=$libexecdir/mysqlmanager
  253.     elif test -x $sbindir/mysqlmanager
  254.     then
  255.       manager=$sbindir/mysqlmanager
  256.     fi

  257.     echo $echo_n "Starting MySQL"
  258.     if test -x $manager -a "$use_mysqld_safe" = "0"
  259.     then
  260.       if test -n "$other_args"
  261.       then
  262.         log_failure_msg "MySQL manager does not support options '$other_args'"
  263.         exit 1
  264.       fi
  265.       # Give extra arguments to mysqld with the my.cnf file. This script may
  266.       # be overwritten at next upgrade.
  267.       $manager --user=$user --pid-file=$pid_file >/dev/null 2>&1 &
  268.       wait_for_pid created $!; return_value=$?

  269.       # Make lock for RedHat / SuSE
  270.       if test -w /var/lock/subsys
  271.       then
  272.         touch /var/lock/subsys/mysqlmanager
  273.       fi
  274.       exit $return_value
  275.     elif test -x $bindir/mysqld_safe
  276.     then
  277.       # Give extra arguments to mysqld with the my.cnf file. This script
  278.       # may be overwritten at next upgrade.
  279.       pid_file=$server_pid_file
  280.       #echo "$bindir/mysqld_safe --datadir=$datadir --port=$port --pid-file=$server_pid_file $other_args >/dev/null 2>&1 &"
  281.       $bindir/mysqld_safe --datadir=$datadir --port=$port --socket=$socket --pid-file=$server_pid_file $other_args >/dev/null 2>&1 &
  282.       wait_for_pid created $!; return_value=$?

  283.       # Make lock for RedHat / SuSE
  284.       if test -w /var/lock/subsys
  285.       then
  286.         touch /var/lock/subsys/mysql
  287.       fi
  288.       exit $return_value
  289.     else
  290.       log_failure_msg "Couldn't find MySQL manager or server"
  291.     fi
  292.     ;;

  293.   'stop')
  294.     # Stop daemon. We use a signal here to avoid having to know the
  295.     # root password.

  296.     # The RedHat / SuSE lock directory to remove
  297.     lock_dir=/var/lock/subsys/mysqlmanager

  298.     # If the manager pid_file doesn't exist, try the server's
  299.     if test ! -s "$pid_file"
  300.     then
  301.       pid_file=$server_pid_file
  302.       lock_dir=/var/lock/subsys/mysql
  303.     fi

  304.     if test -s "$pid_file"
  305.     then
  306.       mysqlmanager_pid=`cat $pid_file`
  307.       echo $echo_n "Shutting down MySQL"
  308.       kill $mysqlmanager_pid
  309.       # mysqlmanager should remove the pid_file when it exits, so wait for it.
  310.       wait_for_pid removed; return_value=$?

  311.       # delete lock for RedHat / SuSE
  312.       if test -f $lock_dir
  313.       then
  314.         rm -f $lock_dir
  315.       fi
  316.       exit $return_value
  317.     else
  318.       log_failure_msg "MySQL manager or server PID file could not be found!"
  319.     fi
  320.     ;;

  321.   'restart')
  322.     # Stop the service and regardless of whether it was
  323.     # running or not, start it again.
  324.     script_full_path=$(cd "$(dirname "$0")"; pwd)
  325.     if $0 stop  $other_args; then
  326.       $0 start $other_args
  327.     #if $script_full_path stop  $other_args; then
  328.     #  $script_full_path start $other_args
  329.     else
  330.       log_failure_msg "Failed to stop running server, so refusing to try to start."
  331.       exit 1
  332.     fi
  333.     ;;

  334.   'reload')
  335.     if test -s "$server_pid_file" ; then
  336.       mysqld_pid=`cat $server_pid_file`
  337.       kill -HUP $mysqld_pid && log_success_msg "Reloading service MySQL"
  338.       touch $server_pid_file
  339.     else
  340.       log_failure_msg "MySQL PID file could not be found!"
  341.     fi
  342.     ;;

  343.   *)
  344.     # usage
  345.     echo "Usage: $0  {start|stop|restart|reload}  [ MySQL server options ]"
  346.     exit 1
  347.     ;;
  348. esac

  349. exit 0
复制代码



最后在每个MySQL实例的data目录下配置一个相应的my.cnf
然后再扔一个这个脚本,改一下脚本开头的datadir、basedir,就可以用这个脚本进行启动、重启、关闭了。

已知问题:
在当前目录下运行重启会报脚本找不着,而启动和关闭没问题。脚本所在目录执行也不会有报错。
如: ./mysql.3307.server restart 报错
./data_3307/mysql.3307.server 正常

[ 本帖最后由 leo_ss_pku 于 2009-8-26 18:50 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2009-09-17 17:31 |只看该作者
这样更简单:
/usr/local/mysql/bin/mysqladmin -h 127.0.0.1 -u root -p -P 3410 shutdown
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP