Chinaunix首页 | 论坛 | 博客
  • 博客访问: 668667
  • 博文数量: 220
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1961
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-04 21:54
文章分类

全部博文(220)

文章存档

2018年(1)

2015年(140)

2014年(79)

我的朋友

分类: Mysql/postgreSQL

2015-01-15 21:39:52

今天不忙,看了一下linux下Mysql的启动脚本,记录一下大体的过程,具体详细的命令的执行,没有写出

 

#1.变量初始化

#--------------------------------------------------------------------------------------------------------------------------------

basedir=
datadir=
service_startup_timeout=900
pid_file=
server_pid_file=
use_mysqld_safe=1
user=mysql

if test -z "$basedir"
then
  basedir=/usr/local/mysql
  bindir=./bin
  if test -z "$datadir"
  then
    datadir=/usr/local/mysql/data
  fi
  sbindir=./bin
  libexecdir=./bin
else
  bindir="$basedir/bin"
  if test -z "$datadir"
  then
    datadir="$basedir/data"
  fi
  sbindir="$basedir/sbin"
  libexecdir="$basedir/libexec"
fi

datadir_set=

#-----------------------------------------------------------------------------------------------------------------------

 

#2.函数定义

#--------------------------------------------------------------------------------------------------------------------------

lsb_functions="/lib/lsb/init-functions  #" 这是一个linux系统脚本,包含一些打印log函数"
if test -f $lsb_functions ; then
  . $lsb_functions  #如果存在改文件,将这些函数导入到该脚本,使其可以使用
else                        #否则自定义函数
  log_success_msg()
  {
    echo " SUCCESS! $@"
  }
  log_failure_msg()
  {
    echo " ERROR! $@"
  }
fi

PATH=/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin
export PATH

mode=$1    # start or stop  mysql.server 脚本的输入参数
shift  将$1删除
other_args="$*"   将$2,... $n  赋值给other_args

 

#系统对echo c 和 echo -n 的结果

#--------------------------------------------------------------

case `echo "testing\c"`,`echo -n testing` in  
    *c*,-n*) echo_n=   echo_c=     ;;
    *c*,*)   echo_n=-n echo_c=     ;;
    *)       echo_n=   echo_c='\c' ;;
esac

#-------------------------------------------------------

#该函数将my_print_defaults 命令分析出得参数赋值给相应的变量

#----------------------------------------------------------------------

parse_server_arguments() {
  for arg do
    case "$arg" in
      --basedir=*)  basedir=`echo "$arg" | sed -e 's/^[^=]*=//'`
                    bindir="$basedir/bin"
      if test -z "$datadir_set"; then
        datadir="$basedir/data"
      fi
      sbindir="$basedir/sbin"
      libexecdir="$basedir/libexec"
        ;;
      --datadir=*)  datadir=`echo "$arg" | sed -e 's/^[^=]*=//'`
      datadir_set=1
 ;;
      --user=*)  user=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
      --pid-file=*) server_pid_file=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
      --service-startup-timeout=*) service_startup_timeout=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
      --use-mysqld_safe) use_mysqld_safe=1;;
      --use-manager)     use_mysqld_safe=0;;
    esac
  done
}

#---------------------------------------------------------------------

#该函数用于my_print_defaults 命令分析出的参数赋值给相应的变量

parse_manager_arguments() {
  for arg do
    case "$arg" in
      --pid-file=*) pid_file=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
      --user=*)  user=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
    esac
  done
}

 

#该函数在启动和停止mysql服务时用于等待pid的生成

#-------------------------------------------------------------------------------

wait_for_pid () {
  verb="$1"
  manager_pid="$2"  # process ID of the program operating on the pid-file
  i=0
  avoid_race_condition="by checking again"
  while test $i -ne $service_startup_timeout ; do

    case "$verb" in
      'created')
        # wait for a PID-file to pop into existence.
        test -s $pid_file && i='' && break
        ;;
      'removed')
        # wait for this PID-file to disappear
        test ! -s $pid_file && i='' && break
        ;;
      *)
        echo "wait_for_pid () usage: wait_for_pid created|removed manager_pid"
        exit 1
        ;;
    esac

    # if manager isn't running, then pid-file will never be updated
    if test -n "$manager_pid"; then
      if kill -0 "$manager_pid" 2>/dev/null; then
        :  # the manager still runs
      else
        # The manager may have exited between the last pid-file check and now.  
        if test -n "$avoid_race_condition"; then
          avoid_race_condition=""
          continue  # Check again.
        fi

        # there's nothing that will affect the file.
        log_failure_msg "Manager of pid-file quit without updating file."
        return 1  # not waiting any more.
      fi
    fi

    echo $echo_n ".$echo_c"
    i=`expr $i + 1`
    sleep 1
  done

  if test -z "$i" ; then
    log_success_msg
    return 0
  else
    log_failure_msg
    return 1
  fi
}

#-------------------------------------------------------------------------------------------- 

#3 指定my_print_defaults命令的路径

#---------------------------------------------------------------------------------------

if test -x ./bin/my_print_defaults     #这个命令主要用来分析my.cnf的参数 使用方法 my_print_defaults   -e  my.cnf   mysqld (指的配置文件中[mysqld]下的内容)
then
  print_defaults="./bin/my_print_defaults"
elif test -x $bindir/my_print_defaults
then
  print_defaults="$bindir/my_print_defaults"
elif test -x $bindir/mysql_print_defaults
then
  print_defaults="$bindir/mysql_print_defaults"
else
  # Try to find basedir in /etc/my.cnf
  conf=/etc/my.cnf
  print_defaults=
  if test -r $conf
  then
    subpat='^[^=]*basedir[^=]*=\(.*\)$'
    dirs=`sed -e "/$subpat/!d" -e 's//\1/' $conf`
    for d in $dirs
    do
      d=`echo $d | sed -e 's/[  ]//g'`
      if test -x "$d/bin/my_print_defaults"
      then
        print_defaults="$d/bin/my_print_defaults"
        break
      fi
      if test -x "$d/bin/mysql_print_defaults"
      then
        print_defaults="$d/bin/mysql_print_defaults"
        break
      fi
    done
  fi

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

#----------------------------------------------------------------------------------------------------------

 

#4  指定配置文件路径

#---------------------------------------------------------------------------------------------------------

extra_args=""
if test -r "$basedir/my.cnf"
then
  extra_args="-e $basedir/my.cnf"
else
  if test -r "$datadir/my.cnf"
  then
    extra_args="-e $datadir/my.cnf"
  fi
fi

#-----------------------------------------------------------------------------------------------------------

 

#使用parse_server_arguments, parse_manager_arguments 函数和my_print_defaults 从my.cnf 中获取相应的参数并付给相应的变量

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

parse_manager_arguments `$print_defaults $extra_args manager`

 

#-----------------------------------------------------------------------------

 

指定mysqlmanager 启动时的pid_file的位置  和 mysql_safe启动时pid_file 的位置

if  test -z "$pid_file"
then
  pid_file=$datadir/mysqlmanager-`/bin/hostname`.pid
else
  case "$pid_file" in 
    /* ) ;;
    * )  pid_file="$datadir/$pid_file" ;;
  esac
fi
if test -z "$server_pid_file"
then
  server_pid_file=$datadir/`/bin/hostname`.pid
else
  case "$server_pid_file" in
    /* ) ;;
    * )  server_pid_file="$datadir/$server_pid_file" ;;
  esac
fi

#-------------------------------------------------------------------------------

启动和停止mysql server 的主体程序

case "$mode" in
  'start')  #开始启动服务
    cd $basedir

  #---------

   #指定mysqlmanager的位置

    manager=$bindir/mysqlmanager
    if test -x $libexecdir/mysqlmanager
    then
      manager=$libexecdir/mysqlmanager
    elif test -x $sbindir/mysqlmanager
    then
      manager=$sbindir/mysqlmanager
    fi

  #------------

    echo $echo_n "Starting MySQL"
    if test -x $manager -a "$use_mysqld_safe" = "0"  # 当use_mysqld_safe=0,这个参数从my.cnf中取得,使用mysqlmanager 启动mysql
    then
            if test -n "$other_args";then   #mysqlmanager 启动不支持有其他的参数,仅仅支持start参数,如果有其他参数则退出

            log_failure_msg "MySQL manager does not support options '$other_args'"
            exit 1
            fi
      # Give extra arguments to mysqld with the my.cnf file. This script may
      # be overwritten at next upgrade.
      $manager --user=$user --pid-file=$pid_file >/dev/null 2>&1 & 启动mysql 
      wait_for_pid created $!; return_value=$?   #wait_for_pid created 等待mysqlserver的启动

      # Make lock for RedHat / SuSE
      if test -w /var/lock/subsys 
      then
        touch /var/lock/subsys/mysqlmanager   #然后创建系统服务锁:/var/lock/subsys/mysqlmanager

      fi
      exit $return_value
    elif test -x $bindir/mysqld_safe   #使用mysqld_safe启动mysql
    then
        pid_file=$server_pid_file
        $bindir/mysqld_safe --datadir=$datadir --pid-file=$server_pid_file $other_args >/dev/null 2>&1 &
        wait_for_pid created $!; return_value=$?   #wait_for_pid created 等待mysqlserver的启动
        if test -w /var/lock/subsys
        then
           touch /var/lock/subsys/mysql  #然后创建系统服务锁:/var/lock/subsys/mysql
        fi
        exit $return_value
    else
      log_failure_msg "Couldn't find MySQL manager ($manager) or server ($bindir/mysqld_safe)"
    fi
    ;;

  'stop')    关闭mysql服务


    lock_dir=/var/lock/subsys/mysqlmanager
    if test ! -s "$pid_file"
    then
      pid_file=$server_pid_file
      lock_dir=/var/lock/subsys/mysql
    fi

    if test -s "$pid_file"
    then
      mysqlmanager_pid=`cat $pid_file`
      echo $echo_n "Shutting down MySQL"
      kill $mysqlmanager_pid
      wait_for_pid removed "$mysqlmanager_pid"; return_value=$?
      if test -f $lock_dir
      then
        rm -f $lock_dir
      fi
      exit $return_value
    else
      log_failure_msg "MySQL manager or server PID file could not be found!"
    fi
    ;;

  'restart')
    if $0 stop  $other_args; then
      $0 start $other_args
    else
      log_failure_msg "Failed to stop running server, so refusing to try to start."
      exit 1
    fi
    ;;

  'reload'|'force-reload')
    if test -s "$server_pid_file" ; then
      read mysqld_pid <  $server_pid_file
      kill -HUP $mysqld_pid && log_success_msg "Reloading service MySQL"
      touch $server_pid_file
    else
      log_failure_msg "MySQL PID file could not be found!"
      exit 1
    fi
    ;;
  'status')
    if test -s "$server_pid_file" ; then 
      read mysqld_pid < $server_pid_file
      if kill -0 $mysqld_pid 2>/dev/null ; then 
        log_success_msg "MySQL running ($mysqld_pid)"
        exit 0
      else
        log_failure_msg "MySQL is not running, but PID file exists"
        exit 1
      fi
    else
      # Try to find appropriate mysqld process
      mysqld_pid=`pidof $sbindir/mysqld`
      if test -z $mysqld_pid ; then 
        if test "$use_mysqld_safe" = "0" ; then 
          lockfile=/var/lock/subsys/mysqlmanager
        else
          lockfile=/var/lock/subsys/mysql
        fi 
        if test -f $lockfile ; then 
          log_failure_msg "MySQL is not running, but lock exists"
          exit 2
        fi 
        log_failure_msg "MySQL is not running"
        exit 3
      else
        log_failure_msg "MySQL is running but PID file could not be found"
        exit 4
      fi
    fi
    ;;
    *)
      # usage
      echo "Usage: $0  {start|stop|restart|reload|force-reload|status}  [ MySQL server options ]"
      exit 1
    ;;
esac

exit 0


http://blog.csdn.net/kobeyan/article/details/7591214

阅读(942) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~