zoukankan      html  css  js  c++  java
  • Mysqld_safe启动脚本

    #!/bin/sh
    # Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB
    # This file is public domain and comes with NO WARRANTY of any kind
    ##脚本用于启动Mysql 守护进程和mysqld异常die后重启
    # Script to start the MySQL daemon and restart it if it dies unexpectedly
    #需要在MYSQL base目录下执行, 使用你使用一个2进制安装,不是安装在编译时默认的目录
    # This should be executed in the MySQL base directory if you are using a
    # binary installation that is not installed in its compile-time default
    # location
    #
    # mysql.server works by first doing a cd to the base directory and from there
    # executing mysqld_safe
    #初始化脚本参数
    # Initialize script globals
    
    KILL_MYSQLD=1;#试图kill多余的mysqld_safe程序,1表示需要kill 
    MYSQLD=
    niceness=0  # 进程的调度优先级标识 
    mysqld_ld_preload=
    mysqld_ld_library_path=
    
    # Initial logging status: error log is not open, and not using syslog
    logging=init
    want_syslog=0 # 标识是否要使用syslog 
    syslog_tag=
    user='mysql'
    pid_file=
    err_log=
    
    syslog_tag_mysqld=mysqld
    syslog_tag_mysqld_safe=mysqld_safe
    
    # # 不允许程序在终端上被人打断(包括挂起,中断,退出,系统终止的情形) 
    trap '' 1 2 3 15			# we shouldn't let anyone kill us
    trap '' 13                              # not even SIGPIPE
    
    # MySQL-specific environment variable. First off, it's not really a umask,
    # it's the desired mode. Second, it follows umask(2), not umask(3) in that
    # octal needs to be explicit. Our shell might be a proper sh without printf,
    # multiple-base arithmetic, and binary arithmetic, so this will get ugly.
    # We reject decimal values to keep things at least half-sane.
    
    #MYSQL指定环境变量,首先 这个不是真正的umask,它只是一个需要的模式。第二,它遵循umask(2),不是umask(3)
    
    #八进制需要被显示,我们的shell可能是正常的sh 乜有printf
    
    #多个base计算,binary 计算 这样会变得丑陋
    
    #我们拒绝10进制只来保持
    
    umask 007                               # fallback
    
    #[mysql@master ~]$ UMASK="${UMASK-0640}"
    #[mysql@master ~]$ echo $UMASK
    #0640
    
    UMASK="${UMASK-0640}"
    
    ##把非0246替换为空
    fmode=`echo "$UMASK" | sed -e 's/[^0246]//g'`
    
    ##取第一个数字 
    #[mysql@master ~]$ echo $octalp
    #0
    
    octalp=`echo "$fmode"|cut -c1`
    
    #统计字符数 
    #[mysql@master ~]$ echo "$fmode"|wc -c|sed -e 's/ //g'
    #5
    fmlen=`echo "$fmode"|wc -c|sed -e 's/ //g'`
    ##或者
    if [ "x$octalp" != "x0" -o "x$UMASK" != "x$fmode" -o "x$fmlen" != "x5" ]
    then
      fmode=0640
      ## UMASK 必须是3位数字模式 开头是0来表明8进制
      echo "UMASK must be a 3-digit mode with an additional leading 0 to indicate octal." >&2
      echo "The first digit will be corrected to 6, the others may be 0, 2, 4, or 6." >&2
    fi
    
    ##[mysql@master ~]$ echo "$fmode"|cut -c3-4
    ##40
    fmode=`echo "$fmode"|cut -c3-4`
    
    fmode="6$fmode"
    if [ "x$UMASK" != "x0$fmode" ]
    then
      echo "UMASK corrected from $UMASK to 0$fmode ..."
    fi
    
    
    # defaults变量记载使用的配置文件的信息  --defaults-file=/etc/my.cnf 
    defaults=
    case "$1" in
        --no-defaults|--defaults-file=*|--defaults-extra-file=*)
          defaults="$1"; shift
          ;;
    esac
    
    usage () {
            cat <<EOF
    Usage: $0 [OPTIONS]
      --no-defaults              Don't read the system defaults file
      --defaults-file=FILE       Use the specified defaults file
      --defaults-extra-file=FILE Also use defaults from the specified file
      --ledir=DIRECTORY          Look for mysqld in the specified directory
      --open-files-limit=LIMIT   Limit the number of open files
      --core-file-size=LIMIT     Limit core files to the specified size
      --timezone=TZ              Set the system timezone
      --malloc-lib=LIB           Preload shared library LIB if available
      --mysqld=FILE              Use the specified file as mysqld
      --mysqld-version=VERSION   Use "mysqld-VERSION" as mysqld
      --nice=NICE                Set the scheduling priority of mysqld
      --plugin-dir=DIR           Plugins are under DIR or DIR/VERSION, if
                                 VERSION is given
      --skip-kill-mysqld         Don't try to kill stray mysqld processes
      --syslog                   Log messages to syslog with 'logger'
      --skip-syslog              Log messages to error log (default)
      --syslog-tag=TAG           Pass -t "mysqld-TAG" to 'logger'
    
    All other options are passed to the mysqld program.
    
    EOF
            exit 1
    }
    
    my_which ()
    {
      save_ifs="${IFS-UNSET}"  # 保存当前的内建分隔符,用于后面重置IFS 
      IFS=:  # 使用:来分割PATH中的路径
      ret=0
      for file  # 这种写法等同于for file in $@
      do
        for dir in $PATH
        do
          if [ -f "$dir/$file" ]
          then
            echo "$dir/$file"
            continue 2 # continue 第 2 层, 这里就是跳出本层for循环,跳到外层for循环
          fi
        done
    
    	ret=1  #signal an error
    	break
      done
    
      if [ "$save_ifs" = UNSET ]
      then
        unset IFS
      else
        IFS="$save_ifs"
      fi
      return $ret  # Success
    }
    # 日志输出函数,这是个原型,后面被log_error和log_notice函数引用 
    log_generic () {
    # priority 代表日志信息的分类,从后面的两个函数可知有:daemon.error和daemon.notice两种类别
      priority="$1"
      shift
    
      msg="`date +'%y%m%d %H:%M:%S'` mysqld_safe $*"
      echo "$msg"
      case $logging in
        init) ;;  #  初始化状态时,只在命令行输出msg信息,不记录日志 
        file) echo "$msg" >> "$err_log" ;; # 记录到err_log中 
        syslog) logger -t "$syslog_tag_mysqld_safe" -p "$priority" "$*" ;;# 使用logger记录到系统日志中 
        *)
          echo "Internal program error (non-fatal):" 
               " unknown logging method '$logging'" >&2
          ;;
      esac
    }
    
    log_error () {
      log_generic daemon.error "$@" >&2
    }
    
    log_notice () {
      log_generic daemon.notice "$@"
    }
    # 后面就是用它启动的mysqld,通过logging变量区分记录日志的类型,分错误日志和系统日志syslog两种 # 最后的eval命令会解析 $cmd 中的值并执行命令
    #$cmd is nohup /usr/local/mysql5.6/bin/mysqld  --basedir=/usr/local/mysql5.6 --datadir=/usr/local/mysql5.6/data --plugin-dir=/usr/local/mysql5.6/lib/plugin   --log-error=/usr/local/mysql5.6/data/master.err --pid-file=/usr/local/mysql5.6/data/master.pid < /dev/null
    
    eval_log_error () {
      cmd="$1"
      case $logging in
        file) cmd="$cmd >> "`shell_quote_string "$err_log"`" 2>&1" ;;
        syslog)
          # mysqld often prefixes its messages with a timestamp, which is
          # redundant when logging to syslog (which adds its own timestamp)
          # However, we don't strip the timestamp with sed here, because
          # sed buffers output (only GNU sed supports a -u (unbuffered) option)
          # which means that messages may not get sent to syslog until the
          # mysqld process quits.
          cmd="$cmd 2>&1 | logger -t '$syslog_tag_mysqld' -p daemon.error"
          ;;
        *)
          echo "Internal program error (non-fatal):" 
               " unknown logging method '$logging'" >&2
          ;;
      esac
    
      #echo "Running mysqld: [$cmd]"
      eval "$cmd"
    }
    
    shell_quote_string() {
      # This sed command makes sure that any special chars are quoted,
      # so the arg gets passed exactly to the server.
      echo "$1" | sed -e 's,([^a-zA-Z0-9/_.=-]),\1,g'
    }
    # 该函数用于解析配置文件中的选项,并赋值给相应的变量 
    parse_arguments() {
      # We only need to pass arguments through to the server if we don't
      # handle them here.  So, we collect unrecognized options (passed on
      # the command line) into the args variable.
    
      ##我们只能通过server 传递参数 如果我们不能处理他们。因此,我们收集不认识的选项
      ##通过命令行选项传递参数
    
      pick_args=
      if test "$1" = PICK-ARGS-FROM-ARGV
      then
        pick_args=1
        shift
      fi
    # 取出参数值,比如 --port=3306 结果为: val = 3306 注意这里sed中使用;来分割,等同于/ 
    
    #这里的for arg 相当于for arg in $@
      for arg do
        # the parameter after "=", or the whole $arg if no match
        val=`echo "$arg" | sed -e 's;^--[^=]*=;;'`
        # what's before "=", or the whole $arg if no match
        optname=`echo "$arg" | sed -e 's/^(--[^=]*)=.*$/1/'`
        # replace "_" by "-" ; mysqld_safe must accept "_" like mysqld does.
        optname_subst=`echo "$optname" | sed 's/_/-/g'`
        arg=`echo $arg | sed "s/^$optname/$optname_subst/"`
        case "$arg" in
          #  # 将参数值传递给对应的变量
          --basedir=*) MY_BASEDIR_VERSION="$val" ;;
          --datadir=*) DATADIR="$val" ;;
          --pid-file=*) pid_file="$val" ;;
          --plugin-dir=*) PLUGIN_DIR="$val" ;;
          --user=*) user="$val"; SET_USER=1 ;;
    
      # 有些值可能已经在my.cnf配置文件的[mysqld_safe]组下设置了   
      # 某些值会被命令行上指定的选项值覆盖
          --log-error=*) err_log="$val" ;;
          --port=*) mysql_tcp_port="$val" ;;
          --socket=*) mysql_unix_port="$val" ;;
    
          # mysqld_safe-specific options - must be set in my.cnf ([mysqld_safe])!
          
          # 接下来这几个特殊的选项在配置文件的[mysqld_safe]组中是必须设置的   
          # 我没配置这个组,所以就用不到了(使用mysqld中的默认)
    
          --core-file-size=*) core_file_size="$val" ;;
          --ledir=*) ledir="$val" ;;
          --malloc-lib=*) set_malloc_lib "$val" ;;
          --mysqld=*) MYSQLD="$val" ;;
          --mysqld-version=*)
            if test -n "$val"
            then
              MYSQLD="mysqld-$val"
              PLUGIN_VARIANT="/$val"
            else
              MYSQLD="mysqld"
            fi
            ;;
          --nice=*) niceness="$val" ;;
          --open-files-limit=*) open_files="$val" ;;
          --open_files_limit=*) open_files="$val" ;;
          --skip-kill-mysqld*) KILL_MYSQLD=0 ;;
          --syslog) want_syslog=1 ;;
          --skip-syslog) want_syslog=0 ;;
          --syslog-tag=*) syslog_tag="$val" ;;
          --timezone=*) TZ="$val"; export TZ; ;;
    
          --help) usage ;;
    
          *)
            if test -n "$pick_args"
            then
              append_arg_to_args "$arg"
            fi
            ;;
        esac
      done
    }
    
    
    # Add a single shared library to the list of libraries which will be added to
    # LD_PRELOAD for mysqld
    #
    # Since LD_PRELOAD is a space-separated value (for historical reasons), if a
    # shared lib's path contains spaces, that path will be prepended to
    # LD_LIBRARY_PATH and stripped from the lib value.
    add_mysqld_ld_preload() {
      lib_to_add="$1"
      log_notice "Adding '$lib_to_add' to LD_PRELOAD for mysqld"
    
      case "$lib_to_add" in
        *' '*)
          # Must strip path from lib, and add it to LD_LIBRARY_PATH
          lib_file=`basename "$lib_to_add"`
          case "$lib_file" in
            *' '*)
              # The lib file itself has a space in its name, and can't
              # be used in LD_PRELOAD
              log_error "library name '$lib_to_add' contains spaces and can not be used with LD_PRELOAD"
              exit 1
              ;;
          esac
          lib_path=`dirname "$lib_to_add"`
          lib_to_add="$lib_file"
          [ -n "$mysqld_ld_library_path" ] && mysqld_ld_library_path="$mysqld_ld_library_path:"
          mysqld_ld_library_path="$mysqld_ld_library_path$lib_path"
          ;;
      esac
    
      # LD_PRELOAD is a space-separated
      [ -n "$mysqld_ld_preload" ] && mysqld_ld_preload="$mysqld_ld_preload "
      mysqld_ld_preload="${mysqld_ld_preload}$lib_to_add"
    }
    
    
    # Returns LD_PRELOAD (and LD_LIBRARY_PATH, if needed) text, quoted to be
    # suitable for use in the eval that calls mysqld.
    #
    # All values in mysqld_ld_preload are prepended to LD_PRELOAD.
    mysqld_ld_preload_text() {
      text=
    
      if [ -n "$mysqld_ld_preload" ]; then
        new_text="$mysqld_ld_preload"
        [ -n "$LD_PRELOAD" ] && new_text="$new_text $LD_PRELOAD"
        text="${text}LD_PRELOAD="`shell_quote_string "$new_text"`' '
      fi
    
      if [ -n "$mysqld_ld_library_path" ]; then
        new_text="$mysqld_ld_library_path"
        [ -n "$LD_LIBRARY_PATH" ] && new_text="$new_text:$LD_LIBRARY_PATH"
        text="${text}LD_LIBRARY_PATH="`shell_quote_string "$new_text"`' '
      fi
    
      echo "$text"
    }
    
    
    mysql_config=
    get_mysql_config() {
      if [ -z "$mysql_config" ]; then
        mysql_config=`echo "$0" | sed 's,/[^/][^/]*$,/mysql_config,'`
        if [ ! -x "$mysql_config" ]; then
          log_error "Can not run mysql_config $@ from '$mysql_config'"
          exit 1
        fi
      fi
    
      "$mysql_config" "$@"
    }
    
    
    # set_malloc_lib LIB
    # - If LIB is empty, do nothing and return
    # - If LIB is 'tcmalloc', look for tcmalloc shared library in /usr/lib
    #   then pkglibdir.  tcmalloc is part of the Google perftools project.
    # - If LIB is an absolute path, assume it is a malloc shared library
    #
    # Put LIB in mysqld_ld_preload, which will be added to LD_PRELOAD when
    # running mysqld.  See ld.so for details.
    set_malloc_lib() {
      malloc_lib="$1"
    
      if [ "$malloc_lib" = tcmalloc ]; then
        pkglibdir=`get_mysql_config --variable=pkglibdir`
        malloc_lib=
        # This list is kept intentionally simple.  Simply set --malloc-lib
        # to a full path if another location is desired.
        for libdir in /usr/lib "$pkglibdir" "$pkglibdir/mysql"; do
          for flavor in _minimal '' _and_profiler _debug; do
            tmp="$libdir/libtcmalloc$flavor.so"
            #log_notice "DEBUG: Checking for malloc lib '$tmp'"
            [ -r "$tmp" ] || continue
            malloc_lib="$tmp"
            break 2
          done
        done
    
        if [ -z "$malloc_lib" ]; then
          log_error "no shared library for --malloc-lib=tcmalloc found in /usr/lib or $pkglibdir"
          exit 1
        fi
      fi
    
      # Allow --malloc-lib='' to override other settings
      [ -z  "$malloc_lib" ] && return
    
      case "$malloc_lib" in
        /*)
          if [ ! -r "$malloc_lib" ]; then
            log_error "--malloc-lib '$malloc_lib' can not be read and will not be used"
            exit 1
          fi
          ;;
        *)
          log_error "--malloc-lib must be an absolute path or 'tcmalloc'; " 
            "ignoring value '$malloc_lib'"
          exit 1
          ;;
      esac
    
      add_mysqld_ld_preload "$malloc_lib"
    }
    
    
    #
    # First, try to find BASEDIR and ledir (where mysqld is)
    #查找mysqld的basedir和mysqld所在的目录
    
    if echo '/usr/local/mysql5./share' | grep '^/usr/local/mysql5.' > /dev/null
    then
      relpkgdata=`echo '/usr/local/mysql5./share' | sed -e 's,^/usr/local/mysql5.,,' -e 's,^/,,' -e 's,^,./,'`
    else
      # pkgdatadir is not relative to prefix
      relpkgdata='/usr/local/mysql5./share'
    fi
    
    MY_PWD=`pwd`
    # Check for the directories we would expect from a binary release install
    if test -n "$MY_BASEDIR_VERSION" -a -d "$MY_BASEDIR_VERSION"
    then
      # BASEDIR is already overridden on command line.  Do not re-set.
    
      # Use BASEDIR to discover le.
      if test -x "$MY_BASEDIR_VERSION/libexec/mysqld"
      then
        ledir="$MY_BASEDIR_VERSION/libexec"
      elif test -x "$MY_BASEDIR_VERSION/sbin/mysqld"
      then
        ledir="$MY_BASEDIR_VERSION/sbin"
      else
        ledir="$MY_BASEDIR_VERSION/bin"
      fi
    elif test -f "$relpkgdata"/english/errmsg.sys -a -x "$MY_PWD/bin/mysqld"
    then
      MY_BASEDIR_VERSION="$MY_PWD"		# Where bin, share and data are
      ledir="$MY_PWD/bin"			# Where mysqld is
    
    # Check for the directories we would expect from a source install
    elif test -f "$relpkgdata"/english/errmsg.sys -a -x "$MY_PWD/libexec/mysqld"
    then
      MY_BASEDIR_VERSION="$MY_PWD"		# Where libexec, share and var are
      ledir="$MY_PWD/libexec"		# Where mysqld is
    elif test -f "$relpkgdata"/english/errmsg.sys -a -x "$MY_PWD/sbin/mysqld"
    then
      MY_BASEDIR_VERSION="$MY_PWD"		# Where sbin, share and var are
      ledir="$MY_PWD/sbin"			# Where mysqld is
    # Since we didn't find anything, used the compiled-in defaults
    else
      MY_BASEDIR_VERSION='/usr/local/mysql5.'
      ledir='/usr/local/mysql5./bin'
    fi
    
    
    #
    # Second, try to find the data directory
    #第2步找出数据目录
    
    # Try where the binary installs put it
    if test -d $MY_BASEDIR_VERSION/data/mysql
    then
      DATADIR=$MY_BASEDIR_VERSION/data
      if test -z "$defaults" -a -r "$DATADIR/my.cnf"
      then
        defaults="--defaults-extra-file=$DATADIR/my.cnf"
      fi
    # Next try where the source installs put it
    elif test -d $MY_BASEDIR_VERSION/var/mysql
    then
      DATADIR=$MY_BASEDIR_VERSION/var
    # Or just give up and use our compiled-in default
    else
      DATADIR=/usr/local/mysql5./data
    fi
    
    if test -z "$MYSQL_HOME"
    then 
      if test -r "$MY_BASEDIR_VERSION/my.cnf" && test -r "$DATADIR/my.cnf"
      then
        log_error "WARNING: Found two instances of my.cnf -
    $MY_BASEDIR_VERSION/my.cnf and
    $DATADIR/my.cnf
    IGNORING $DATADIR/my.cnf"
    
        MYSQL_HOME=$MY_BASEDIR_VERSION
      elif test -r "$DATADIR/my.cnf"
      then
        log_error "WARNING: Found $DATADIR/my.cnf
    
    The data directory is a deprecated location for my.cnf, please move it to
    $MY_BASEDIR_VERSION/my.cnf"
        MYSQL_HOME=$DATADIR
      else
        MYSQL_HOME=$MY_BASEDIR_VERSION
      fi
    fi
    export MYSQL_HOME
    
    
    # Get first arguments from the my.cnf file, groups [mysqld] and [mysqld_safe]
    # and then merge with the command line arguments
    
    ##从my.cnf文件中取得参数 组[mysqld]和[mysqld-safe]
    
    if test -x "$MY_BASEDIR_VERSION/bin/my_print_defaults"
    then
      print_defaults="$MY_BASEDIR_VERSION/bin/my_print_defaults"
    elif test -x `dirname $0`/my_print_defaults
    then
      print_defaults="`dirname $0`/my_print_defaults"
    elif test -x ./bin/my_print_defaults
    then
      print_defaults="./bin/my_print_defaults"
    elif test -x /usr/local/mysql5./bin/my_print_defaults
    then
      print_defaults="/usr/local/mysql5./bin/my_print_defaults"
    elif test -x /usr/local/mysql5./bin/mysql_print_defaults
    then
      print_defaults="/usr/local/mysql5./bin/mysql_print_defaults"
    else
      print_defaults="my_print_defaults"
    fi
    
    append_arg_to_args () {
      args="$args "`shell_quote_string "$1"`
    }
    
    args=
    
    SET_USER=2
    parse_arguments `$print_defaults $defaults --loose-verbose mysqld server`
    if test $SET_USER -eq 2
    then
      SET_USER=0
    fi
    
    parse_arguments `$print_defaults $defaults --loose-verbose mysqld_safe safe_mysqld`
    parse_arguments PICK-ARGS-FROM-ARGV "$@"
    
    #
    # Try to find the plugin directory
    #
    
    # Use user-supplied argument
    if [ -n "${PLUGIN_DIR}" ]; then
      plugin_dir="${PLUGIN_DIR}"
    else
      # Try to find plugin dir relative to basedir
      for dir in lib/mysql/plugin lib/plugin
      do
        if [ -d "${MY_BASEDIR_VERSION}/${dir}" ]; then
          plugin_dir="${MY_BASEDIR_VERSION}/${dir}"
          break
        fi
      done
      # Give up and use compiled-in default
      if [ -z "${plugin_dir}" ]; then
        plugin_dir='/usr/local/mysql5./lib/plugin'
      fi
    fi
    plugin_dir="${plugin_dir}${PLUGIN_VARIANT}"
    
    # Determine what logging facility to use
    
    # Ensure that 'logger' exists, if it's requested
    if [ $want_syslog -eq 1 ]
    then
      my_which logger > /dev/null 2>&1
      if [ $? -ne 0 ]
      then
        log_error "--syslog requested, but no 'logger' program found.  Please ensure that 'logger' is in your PATH, or do not specify the --syslog option to mysqld_safe."
        exit 1
      fi
    fi
    
    if [ -n "$err_log" -o $want_syslog -eq 0 ]
    then
      if [ -n "$err_log" ]
      then
        # mysqld adds ".err" if there is no extension on the --log-error
        # argument; must match that here, or mysqld_safe will write to a
        # different log file than mysqld
    
        # mysqld does not add ".err" to "--log-error=foo."; it considers a
        # trailing "." as an extension
        
        if expr "$err_log" : '.*.[^/]*$' > /dev/null
        then
            :
        else
          err_log="$err_log".err
        fi
    
        case "$err_log" in
          /* ) ;;
          * ) err_log="$DATADIR/$err_log" ;;
        esac
      else
        err_log=$DATADIR/`hostname`.err
      fi
    
      append_arg_to_args "--log-error=$err_log"
    
      if [ $want_syslog -eq 1 ]
      then
        # User explicitly asked for syslog, so warn that it isn't used
        log_error "Can't log to error log and syslog at the same time.  Remove all --log-error configuration options for --syslog to take effect."
      fi
    
      # Log to err_log file
      log_notice "Logging to '$err_log'."
      logging=file
    
      if [ ! -f "$err_log" ]; then                  # if error log already exists,
        touch "$err_log"                            # we just append. otherwise,
        chmod "$fmode" "$err_log"                   # fix the permissions here!
      fi
    
    else
      if [ -n "$syslog_tag" ]
      then
        # Sanitize the syslog tag
        syslog_tag=`echo "$syslog_tag" | sed -e 's/[^a-zA-Z0-9_-]/_/g'`
        syslog_tag_mysqld_safe="${syslog_tag_mysqld_safe}-$syslog_tag"
        syslog_tag_mysqld="${syslog_tag_mysqld}-$syslog_tag"
      fi
      log_notice "Logging to syslog."
      logging=syslog
    fi
    
    USER_OPTION=""
    if test -w / -o "$USER" = "root"
    then
      if test "$user" != "root" -o $SET_USER = 1
      then
        USER_OPTION="--user=$user"
      fi
      # Change the err log to the right user, if it is in use
      if [ $want_syslog -eq 0 ]; then
        touch "$err_log"
        chown $user "$err_log"
      fi
      if test -n "$open_files"
      then
        ulimit -n $open_files
      fi
    fi
    
    if test -n "$open_files"
    then
      append_arg_to_args "--open-files-limit=$open_files"
    fi
    
    safe_mysql_unix_port=${mysql_unix_port:-${MYSQL_UNIX_PORT:-/tmp/mysql.sock}}
    # Make sure that directory for $safe_mysql_unix_port exists
    mysql_unix_port_dir=`dirname $safe_mysql_unix_port`
    if [ ! -d $mysql_unix_port_dir ]
    then
      mkdir $mysql_unix_port_dir
      chown $user $mysql_unix_port_dir
      chmod 755 $mysql_unix_port_dir
    fi
    
    # If the user doesn't specify a binary, we assume name "mysqld"
    if test -z "$MYSQLD"
    then
      MYSQLD=mysqld
    fi
    
    if test ! -x "$ledir/$MYSQLD"
    then
      log_error "The file $ledir/$MYSQLD
    does not exist or is not executable. Please cd to the mysql installation
    directory and restart this script from there as follows:
    ./bin/mysqld_safe&
    See http://dev.mysql.com/doc/mysql/en/mysqld-safe.html for more information"
      exit 1
    fi
    
    if test -z "$pid_file"
    then
      pid_file="$DATADIR/`hostname`.pid"
    else
      case "$pid_file" in
        /* ) ;;
        * )  pid_file="$DATADIR/$pid_file" ;;
      esac
    fi
    append_arg_to_args "--pid-file=$pid_file"
    
    if test -n "$mysql_unix_port"
    then
      append_arg_to_args "--socket=$mysql_unix_port"
    fi
    if test -n "$mysql_tcp_port"
    then
      append_arg_to_args "--port=$mysql_tcp_port"
    fi
    
    if test $niceness -eq 0
    then
      NOHUP_NICENESS="nohup"
    else
      NOHUP_NICENESS="nohup nice -$niceness"
    fi
    
    # Using nice with no args to get the niceness level is GNU-specific.
    # This check could be extended for other operating systems (e.g.,
    # BSD could use "nohup sh -c 'ps -o nice -p $$' | tail -1").
    # But, it also seems that GNU nohup is the only one which messes
    # with the priority, so this is okay.
    if nohup nice > /dev/null 2>&1
    then
        normal_niceness=`nice`
        nohup_niceness=`nohup nice 2>/dev/null`
    
        numeric_nice_values=1
        for val in $normal_niceness $nohup_niceness
        do
            case "$val" in
                -[0-9] | -[0-9][0-9] | -[0-9][0-9][0-9] | 
                 [0-9] |  [0-9][0-9] |  [0-9][0-9][0-9] )
                    ;;
                * )
                    numeric_nice_values=0 ;;
            esac
        done
    
        if test $numeric_nice_values -eq 1
        then
            nice_value_diff=`expr $nohup_niceness - $normal_niceness`
            if test $? -eq 0 && test $nice_value_diff -gt 0 && 
                nice --$nice_value_diff echo testing > /dev/null 2>&1
            then
                # nohup increases the priority (bad), and we are permitted
                # to lower the priority with respect to the value the user
                # might have been given
                niceness=`expr $niceness - $nice_value_diff`
                NOHUP_NICENESS="nice -$niceness nohup"
            fi
        fi
    else
        if nohup echo testing > /dev/null 2>&1
        then
            :
        else
            # nohup doesn't work on this system
            NOHUP_NICENESS=""
        fi
    fi
    
    # Try to set the core file size (even if we aren't root) because many systems
    # don't specify a hard limit on core file size.
    if test -n "$core_file_size"
    then
      ulimit -c $core_file_size
    fi
    
    #
    # If there exists an old pid file, check if the daemon is already running
    # Note: The switches to 'ps' may depend on your operating system
    if test -f "$pid_file"
    then
      PID=`cat "$pid_file"`
      if kill -0 $PID > /dev/null 2> /dev/null
      then
        if ps wwwp $PID | grep -v mysqld_safe | grep -- $MYSQLD > /dev/null
        then    # The pid contains a mysqld process
          log_error "A mysqld process already exists"
          exit 1
        fi
      fi
      rm -f "$pid_file"
      if test -f "$pid_file"
      then
        log_error "Fatal error: Can't remove the pid file:
    $pid_file
    Please remove it manually and start $0 again;
    mysqld daemon not started"
        exit 1
      fi
    fi
    
    #
    # Uncomment the following lines if you want all tables to be automatically
    # checked and repaired during startup. You should add sensible key_buffer
    # and sort_buffer values to my.cnf to improve check performance or require
    # less disk space.
    # Alternatively, you can start mysqld with the "myisam-recover" option. See
    # the manual for details.
    #
    # echo "Checking tables in $DATADIR"
    # $MY_BASEDIR_VERSION/bin/myisamchk --silent --force --fast --medium-check $DATADIR/*/*.MYI
    # $MY_BASEDIR_VERSION/bin/isamchk --silent --force $DATADIR/*/*.ISM
    
    # Does this work on all systems?
    #if type ulimit | grep "shell builtin" > /dev/null
    #then
    #  ulimit -n 256 > /dev/null 2>&1		# Fix for BSD and FreeBSD systems
    #fi
    
    cmd="`mysqld_ld_preload_text`$NOHUP_NICENESS"
    
    for i in  "$ledir/$MYSQLD" "$defaults" "--basedir=$MY_BASEDIR_VERSION" 
      "--datadir=$DATADIR" "--plugin-dir=$plugin_dir" "$USER_OPTION"
    do
      cmd="$cmd "`shell_quote_string "$i"`
    done
    cmd="$cmd $args"
    # Avoid 'nohup: ignoring input' warning
    test -n "$NOHUP_NICENESS" && cmd="$cmd < /dev/null"
    
    log_notice "Starting $MYSQLD daemon with databases from $DATADIR"
    
    # variable to track the current number of "fast" (a.k.a. subsecond) restarts
    fast_restart=0
    # maximum number of restarts before trottling kicks in
    max_fast_restarts=5
    # flag whether a usable sleep command exists
    have_sleep=1
    
    while true
    do
      rm -f $safe_mysql_unix_port "$pid_file"	# Some extra safety
    
      start_time=`date +%M%S`
    
      eval_log_error "$cmd"
    
      if [ $want_syslog -eq 0 -a ! -f "$err_log" ]; then
        touch "$err_log"                    # hypothetical: log was renamed but not
        chown $user "$err_log"              # flushed yet. we'd recreate it with
        chmod "$fmode" "$err_log"           # wrong owner next time we log, so set
      fi                                    # it up correctly while we can!
    
      end_time=`date +%M%S`
    
      if test ! -f "$pid_file"		# This is removed if normal shutdown
      then
        break
      fi
    
    
      # sanity check if time reading is sane and there's sleep
      if test $end_time -gt 0 -a $have_sleep -gt 0
      then
        # throttle down the fast restarts
        if test $end_time -eq $start_time
        then
          fast_restart=`expr $fast_restart + 1`
          if test $fast_restart -ge $max_fast_restarts
          then
            log_notice "The server is respawning too fast. Sleeping for 1 second."
            sleep 1
            sleep_state=$?
            if test $sleep_state -gt 0
            then
              log_notice "The server is respawning too fast and no working sleep command. Turning off trottling."
              have_sleep=0
            fi
    
            fast_restart=0
          fi
        else
          fast_restart=0
        fi
      fi
    
      if true && test $KILL_MYSQLD -eq 1
      then
        # Test if one process was hanging.
        # This is only a fix for Linux (running as base 3 mysqld processes)
        # but should work for the rest of the servers.
        # The only thing is ps x => redhat 5 gives warnings when using ps -x.
        # kill -9 is used or the process won't react on the kill.
        numofproces=`ps xaww | grep -v "grep" | grep "$ledir/$MYSQLD>" | grep -c "pid-file=$pid_file"`
    
        log_notice "Number of processes running now: $numofproces"
        I=1
        while test "$I" -le "$numofproces"
        do 
          PROC=`ps xaww | grep "$ledir/$MYSQLD>" | grep -v "grep" | grep "pid-file=$pid_file" | sed -n '$p'` 
    
          for T in $PROC
          do
            break
          done
          #    echo "TEST $I - $T **"
          if kill -9 $T
          then
            log_error "$MYSQLD process hanging, pid $T - killed"
          else
            break
          fi
          I=`expr $I + 1`
        done
      fi
      log_notice "mysqld restarted"
    done
    
    log_notice "mysqld from pid file $pid_file ended"
    
    
    
    
    
     [mysql@master mysql5.6]$ pwd
    /usr/local/mysql5.6
    [mysql@master mysql5.6]$ mysqld_safe --defaults-file=/etc/my.cnf --user=mysql 
    150309 10:35:40 mysqld_safe Logging to '/usr/local/mysql5.6/data/master.err'.
    150309 10:35:40 mysqld_safe A mysqld process already exists
    [mysql@master mysql5.6]$ rm /usr/local/mysql5.6/data/master.pid
    [mysql@master mysql5.6]$ mysqld_safe --defaults-file=/etc/my.cnf --user=mysql 
    150309 10:35:59 mysqld_safe Logging to '/usr/local/mysql5.6/data/master.err'.
    150309 10:35:59 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql5.6/data
    $cmd is nohup /usr/local/mysql5.6/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql5.6 --datadir=/usr/local/mysql5.6/data --plugin-dir=/usr/local/mysql5.6/lib/plugin   --log-error=/usr/local/mysql5.6/data/master.err --pid-file=/usr/local/mysql5.6/data/master.pid < /dev/null
    
    必须在mysql basedir下执行
    
    [mysql@master mysql5.6]$ mysqld_safe                                          
    150309 10:36:38 mysqld_safe Logging to '/usr/local/mysql5.6/data/master.err'.
    150309 10:36:38 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql5.6/data
    $cmd is nohup /usr/local/mysql5.6/bin/mysqld  --basedir=/usr/local/mysql5.6 --datadir=/usr/local/mysql5.6/data --plugin-dir=/usr/local/mysql5.6/lib/plugin   --log-error=/usr/local/mysql5.6/data/master.err --pid-file=/usr/local/mysql5.6/data/master.pid < /dev/null
    
    
    [mysql@master ~]$ mysqld_safe --defaults-file=/etc/my.cnf --user=mysql 
    150309 10:41:19 mysqld_safe Logging to '/usr/local/mysql5./data/master.err'.
    touch: cannot touch `/usr/local/mysql5./data/master.err': No such file or directory
    chmod: cannot access `/usr/local/mysql5./data/master.err': No such file or directory
    150309 10:41:19 mysqld_safe The file /usr/local/mysql5./bin/mysqld
    does not exist or is not executable. Please cd to the mysql installation
    directory and restart this script from there as follows:
    ./bin/mysqld_safe&
    See http://dev.mysql.com/doc/mysql/en/mysqld-safe.html for more information
    /usr/local/mysql5.6/bin/mysqld_safe: line 129: /usr/local/mysql5./data/master.err: No such file or directory
    
    [mysql@master bin]$ my_print_defaults 
    my_print_defaults  Ver 1.6 for Linux at x86_64
    This software comes with ABSOLUTELY NO WARRANTY. This is free software,
    and you are welcome to modify and redistribute it under the GPL license
    
    Prints all arguments that is give to some program using the default files
    Usage: my_print_defaults [OPTIONS] groups
      -c, --config-file=name 
                          Deprecated, please use --defaults-file instead. Name of
                          config file to read; if no extension is given, default
                          extension (e.g., .ini or .cnf) will be added
      -#, --debug[=#]     This is a non-debug version. Catch this and exit
      -c, --defaults-file=name 
                          Like --config-file, except: if first option, then read
                          this file only, do not read global or per-user config
                          files; should be the first option
      -e, --defaults-extra-file=name 
                          Read this file after the global config file and before
                          the config file in the users home directory; should be
                          the first option
      -g, --defaults-group-suffix=name 
                          In addition to the given groups, read also groups with
                          this suffix
      -e, --extra-file=name 
                          Deprecated. Synonym for --defaults-extra-file.
      -n, --no-defaults   Ignore reading of default option file(s), except for
                          login file.
      -l, --login-path=name 
                          Path to be read from under the login file.
      -?, --help          Display this help message and exit.
      -v, --verbose       Increase the output level
      -V, --version       Output version information and exit.
    
    Default options are read from the following files in the given order:
    /etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql5./etc/my.cnf ~/.my.cnf 
    
    Variables (--variable-name=value)
    and boolean options {FALSE|TRUE}  Value (after reading options)
    --------------------------------- ----------------------------------------
    config-file                       my
    defaults-file                     my
    defaults-extra-file               (No default value)
    defaults-group-suffix             (No default value)
    extra-file                        (No default value)
    login-path                        (No default value)
    
    Example usage:
    my_print_defaults --defaults-file=example.cnf client mysql
    
    默认读取的顺序;
    /etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql5./etc/my.cnf ~/.my.cnf 
    

  • 相关阅读:
    2.WSDL 文档
    SQL SERVER取分组数据第一条:查出每个班级的成绩第一名
    生成随机字符串
    js返回上一页并刷新的几种方法
    SQL 单表查询多个计算的值
    SQL 从字符串中提取数字
    SQL 视图和表
    WebSrevice (2)
    WebSrevice (1)
    CSS中如何选择ul下li的奇数、偶数行
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13351783.html
Copyright © 2011-2022 走看看