《Mysql應(yīng)用mysqld_safe啟動(dòng)腳本源碼閱讀、分析》要點(diǎn):
本文介紹了Mysql應(yīng)用mysqld_safe啟動(dòng)腳本源碼閱讀、分析,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
前幾天讀了下mysqld_safe腳本,個(gè)人感覺還是收獲蠻大的,其中細(xì)致的交代了MySQL數(shù)據(jù)庫(kù)的啟動(dòng)流程,包括查找MySQL相關(guān)目錄,解析配置文件以及最后如何調(diào)用mysqld程序來(lái)啟動(dòng)實(shí)例等,有著不錯(cuò)的參考價(jià)值;與此同時(shí),腳本中涉及了很多shell編程中的小技巧,像變量解析,sed替換轉(zhuǎn)義,進(jìn)程優(yōu)先級(jí)的判斷以及無(wú)處不在test結(jié)構(gòu)等等,當(dāng)作Linux shell的學(xué)習(xí)素材還是非常合適的,下面是我的環(huán)境:MYSQL實(shí)例
數(shù)據(jù)庫(kù)版本: MySQL 5.1.45
操作系統(tǒng)版本: Red Hat Enterprise Linux AS release 4 (Nahant Update 3)
MySQL基目錄: /usr/local/mysql3306
配置文件目錄: /usr/local/mysql3306/etcMYSQL實(shí)例
數(shù)據(jù)庫(kù)是安裝好了的,代碼如下:
MYSQL實(shí)例
#!/bin/sh # 一些狀態(tài)變量的定義 KILL_MYSQLD=1; # 試圖kill多余的mysqld_safe程序,1表示需要kill MYSQLD= # mysqld二進(jìn)制可執(zhí)行文件的名稱 niceness=0 # 進(jìn)程的調(diào)度優(yōu)先級(jí)標(biāo)識(shí) # 下面的變量主要用于標(biāo)識(shí)不使用錯(cuò)誤日志和syslog logging=init # 日志記錄狀態(tài),init代表初始化 want_syslog=0 # 標(biāo)識(shí)是否要使用syslog syslog_tag= user='mysql' # --user選項(xiàng)值 pid_file= # pid文件的路徑 err_log= # 錯(cuò)誤日志的路徑 # 這兩個(gè)都是定義的syslog中標(biāo)志位,在后面需要寫入日志到syslog中時(shí)使用 syslog_tag_mysqld=mysqld syslog_tag_mysqld_safe=mysqld_safe trap '' 1 2 3 15 # 不允許程序在終端上被人打斷(包括掛起,中斷,退出,系統(tǒng)終止的情形) umask 007 # 默認(rèn)權(quán)限770,其他組用戶對(duì)該程序創(chuàng)建的文件沒有任何權(quán)限 # defaults變量記載使用的配置文件的信息 defaults= case "$1" in --no-defaults|--defaults-file=*|--defaults-extra-file=*) defaults="$1"; shift ;; esac # usage()函數(shù):使用--help選項(xiàng)時(shí)輸出的使用幫助信息 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 --mysqld=FILE Use the specified file as mysqld --mysqld-version=VERSION Use "mysqld-VERSION" as mysqld --nice=NICE Set the scheduling priority of mysqld --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的作用相當(dāng)于which,通過(guò)檢索$PATH中的路徑,打印出命令的全路徑 # 這個(gè)函數(shù)就在后面一個(gè)地方用到了,就是my_which logger,意思等同于轉(zhuǎn)換logger為/usr/bin/logger my_which () { save_ifs="${IFS-UNSET}" # 保存當(dāng)前的內(nèi)建分隔符,用于后面重置IFS IFS=: # 使用 : 來(lái)分割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 層, 這里就是跳出外層循環(huán)了 fi done ret=1 # signal an error break done # 將設(shè)置過(guò)的IFS重置回去 if [ "$save_ifs" = UNSET ] then unset IFS else IFS="$save_ifs" fi return $ret # Success } # 日志輸出函數(shù),這是個(gè)原型,后面被log_error和log_notice函數(shù)引用 log_generic () { # priority 代表日志信息的分類,從后面的兩個(gè)函數(shù)可知有:daemon.error和daemon.notice兩種類別 priority="$1" shift # 日志中記錄的msg前綴格式: 時(shí)間 + mysqld_safe ,類似于系統(tǒng)日志的記錄格式 msg="`date +'%y%m%d %H:%M:%S'` mysqld_safe $*" echo "$msg" case $logging in init) ;; # 初始化狀態(tài)時(shí),只在命令行輸出msg信息,不記錄日志 file) echo "$msg" >> "$err_log" ;; # 記錄到err_log中 syslog) logger -t "$syslog_tag_mysqld_safe" -p "$priority" "$*" ;; # 使用logger記錄到系統(tǒng)日志中 *) echo "Internal program error (non-fatal):" \ " unknown logging method '$logging'" >&2 ;; esac } # 下面兩個(gè)函數(shù)是對(duì)log_generic函數(shù)中不同分類的引用 log_error () { log_generic daemon.error "$@" >&2 } log_notice () { log_generic daemon.notice "$@" } # 后面就是用它啟動(dòng)的mysqld,通過(guò)logging變量區(qū)分記錄日志的類型,分錯(cuò)誤日志和系統(tǒng)日志syslog兩種 # 最后的eval命令會(huì)解析 $cmd 中的值并執(zhí)行命令 eval_log_error () { cmd="$1" case $logging in file) cmd="$cmd >> "`shell_quote_string "$err_log"`" 2>&1" ;; syslog) 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" } # 轉(zhuǎn)義函數(shù),用于在非"a-z","A-Z","09",'/','_','.','=','-'的特殊字符前加上一個(gè)"\" # sed中的\1代表引用前面\(\)中匹配的值 shell_quote_string() { echo "$1" | sed -e 's,\([^a-zA-Z0-9/_.=-]\),\\\1,g' } # 該函數(shù)用于解析配置文件中的選項(xiàng),并賦值給相應(yīng)的變量 parse_arguments() { pick_args= if test "$1" = PICK-ARGS-FROM-ARGV then pick_args=1 shift fi for arg do # 取出參數(shù)值,比如 --port=3306 結(jié)果為: val = 3306 注意這里sed中使用;來(lái)分割,等同于/ val=`echo "$arg" | sed -e "s;--[^=]*=;;"` case "$arg" in # 將參數(shù)值傳遞給對(duì)應(yīng)的變量 --basedir=*) MY_BASEDIR_VERSION="$val" ;; --datadir=*) DATADIR="$val" ;; --pid-file=*) pid_file="$val" ;; --user=*) user="$val"; SET_USER=1 ;; # 有些值可能已經(jīng)在my.cnf配置文件的[mysqld_safe]組下設(shè)置了 # 某些值會(huì)被命令行上指定的選項(xiàng)值覆蓋 --log-error=*) err_log="$val" ;; --port=*) mysql_tcp_port="$val" ;; --socket=*) mysql_unix_port="$val" ;; # 接下來(lái)這幾個(gè)特殊的選項(xiàng)在配置文件的[mysqld_safe]組中是必須設(shè)置的 # 我沒配置這個(gè)組,所以就用不到了(使用mysqld中的默認(rèn)) --core-file-size=*) core_file_size="$val" ;; --ledir=*) ledir="$val" ;; --mysqld=*) MYSQLD="$val" ;; --mysqld-version=*) if test -n "$val" then MYSQLD="mysqld-$val" else MYSQLD="mysqld" fi ;; --nice=*) niceness="$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; ;; # 生效了一下時(shí)區(qū)設(shè)置 --help) usage ;; # 調(diào)用了usage函數(shù),輸出幫助信息 *) if test -n "$pick_args" then # 將其他命令行參數(shù)值附加到$arg的后面 append_arg_to_args "$arg" fi ;; esac done } ######################################## # 正式工作開始了!! ######################################## # # 下面兩段是在尋找基目錄和mysqld所在目錄 # # 找到/usr/local/mysql3306/share/mysql目錄,使用relpkgdata來(lái)記錄相對(duì)路徑和絕對(duì)路徑 # 這個(gè)grep其實(shí)應(yīng)該是想判斷一下share/mysql是不是顯示的絕對(duì)路徑,不知道這么寫的意義在哪里. if echo '/usr/local/mysql3306/share/mysql' | grep '^/usr/local/mysql3306' > /dev/null then # 一口氣用了三個(gè)替換,分別為: # 第一步:將/usr/local/mysql3306轉(zhuǎn)換為空 # 第二步:將/share/mysql開頭的/轉(zhuǎn)換為空 # 第三步:在share/mysql開頭加上./,結(jié)果即:./share/mysql relpkgdata=`echo '/usr/local/mysql3306/share/mysql' | sed -e 's,^/usr/local/mysql3306,,' -e 's,^/,,' -e 's,^,./,'` else relpkgdata='/usr/local/mysql3306/share/mysql' fi # 這一段都是在找mysqld文件,分別判斷了libexec和bin目錄 # 找不到就使用編譯時(shí)的默認(rèn)值 MY_PWD=`pwd` if test -n "$MY_BASEDIR_VERSION" -a -d "$MY_BASEDIR_VERSION" then if test -x "$MY_BASEDIR_VERSION/libexec/mysqld" then ledir="$MY_BASEDIR_VERSION/libexec" else ledir="$MY_BASEDIR_VERSION/bin" fi # 這里對(duì)errmsg.sys文件進(jìn)行了判斷,個(gè)人認(rèn)為這是為了確認(rèn)當(dāng)前目錄為一個(gè)mysql安裝基目錄 elif test -f "$relpkgdata"/english/errmsg.sys -a -x "$MY_PWD/bin/mysqld" then MY_BASEDIR_VERSION="$MY_PWD" ledir="$MY_PWD/bin" elif test -f "$relpkgdata"/english/errmsg.sys -a -x "$MY_PWD/libexec/mysqld" then MY_BASEDIR_VERSION="$MY_PWD" ledir="$MY_PWD/libexec" else MY_BASEDIR_VERSION='/usr/local/mysql3306' ledir='/usr/local/mysql3306/libexec' fi # # 接下來(lái)是找到配置文件和數(shù)據(jù)文件目錄 # # 找到配置文件目錄 # 我的是放在了etc/目錄下,mysqld程序是會(huì)讀取到的 # # 可以從my_print_defaults腳本中獲得默認(rèn)的讀取my.cnf順序,如下 # Default options are read from the following files in the given order: # /etc/my.cnf /etc/mysql/my.cnf /home/mysql/mysql_master/etc/my.cnf ~/.my.cnf # 或者可以使用strace -e open libexec/mysqld 2>&1 | grep my.cnf查看 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 # 接下來(lái)找到數(shù)據(jù)文件的目錄 elif test -d $MY_BASEDIR_VERSION/var/mysql then DATADIR=$MY_BASEDIR_VERSION/var # 找不到就用編譯時(shí)指定的默認(rèn)值 else DATADIR=/usr/local/mysql3306/var fi # 對(duì)存在兩個(gè)配置文件情況進(jìn)行沖突處理 if test -z "$MYSQL_HOME" then if test -r "$MY_BASEDIR_VERSION/my.cnf" && test -r "$DATADIR/my.cnf" then # 優(yōu)先考慮 $MY_BASEDIR_VERSION/my.cnf 文件 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 # # 下面是使用bin/my_print_defaults讀取my.cnf文件中的配置信息([mysqld] and [mysqld_safe]) # 并且和命令行中傳入的參數(shù)進(jìn)行合并 # 先是找到my_print_defaults執(zhí)行文件 又是各種路徑判斷 if test -x "$MY_BASEDIR_VERSION/bin/my_print_defaults" then print_defaults="$MY_BASEDIR_VERSION/bin/my_print_defaults" elif test -x ./bin/my_print_defaults then print_defaults="./bin/my_print_defaults" elif test -x /usr/local/mysql3306/bin/my_print_defaults then print_defaults="/usr/local/mysql3306/bin/my_print_defaults" elif test -x /usr/local/mysql3306/bin/mysql_print_defaults then print_defaults="/usr/local/mysql3306/bin/mysql_print_defaults" else print_defaults="my_print_defaults" fi # 這個(gè)函數(shù)可以將一個(gè)指定的參數(shù)附加到$arg中(在此同時(shí)執(zhí)行了轉(zhuǎn)義操作) append_arg_to_args () { args="$args "`shell_quote_string "$1"` } args= # 這里SET_USER=2是針對(duì)下面一條parse_arguments來(lái)說(shuō)的 # 因?yàn)槿绻诰o接著的parse_arugments函數(shù)中設(shè)置了--user的值,那么SET_USER就會(huì)變?yōu)?,表示--user以被配置 # 當(dāng)然如果沒有讀取到--user的值,就是說(shuō)--user沒有配置,那么會(huì)在后面的if結(jié)構(gòu)中設(shè)置SET_USER為0 # 這樣在后面的判斷結(jié)構(gòu)中,SET_USER的值 0代表沒有配置--user的值,1代表已經(jīng)配置 SET_USER=2 # 解析配置文件中的參數(shù),使用--loose-verbose來(lái)過(guò)濾[mysqld]和[server]組中的內(nèi)容 parse_arguments `$print_defaults $defaults --loose-verbose mysqld server` if test $SET_USER -eq 2 then SET_USER=0 fi # 又對(duì)[safe_mysqld]和[mysqld_safe]組中的內(nèi)容進(jìn)行了過(guò)濾讀取 # 在我的配置文件中已經(jīng)沒有這兩個(gè)組了,估計(jì)是為兼容舊版本的需要 parse_arguments `$print_defaults $defaults --loose-verbose mysqld_safe safe_mysqld` # 用命令行輸入選項(xiàng) $@ 來(lái)覆蓋配置文件中的選項(xiàng) 機(jī)智 parse_arguments PICK-ARGS-FROM-ARGV "$@" # # 下面是logging工具的使用 # # 判斷l(xiāng)ogger工具是否可用 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 # 給err_log改名字... if [ -n "$err_log" -o $want_syslog -eq 0 ] then if [ -n "$err_log" ] then # 下面是為err_log添加一個(gè).err后綴(如果現(xiàn)在名字沒有后綴) # 如果不設(shè)置這個(gè)后綴,mysqld_safe和mysqld程序會(huì)將日志寫入不同的文件中 # 因?yàn)樵?mysqld 程序中,它將識(shí)別帶有.的文件名為錯(cuò)誤日志(腳本注釋上說(shuō)的) # 這里的expr是識(shí)別文件名中“.”前面的字符總數(shù)量(包括.),如果沒有設(shè)置后綴,返回就是0了 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/`/bin/hostname`.err fi # 追加錯(cuò)誤日志的位置選項(xiàng) append_arg_to_args "--log-error=$err_log" # 發(fā)出錯(cuò)誤提示:不要使用syslog if [ $want_syslog -eq 1 ] then 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=files # 正式把logging改成files 使用錯(cuò)誤日志來(lái)記錄日志 # 這個(gè)分支就是使用syslog的方法了 else if [ -n "$syslog_tag" ] then # 設(shè)置各個(gè)syslog的使用標(biāo)志位 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 # 設(shè)置--user選項(xiàng) USER_OPTION="" if test -w / -o "$USER" = "root" # 根目錄是否可寫,或者當(dāng)前用戶為root then if test "$user" != "root" -o $SET_USER = 1 then USER_OPTION="--user=$user" fi # 創(chuàng)建錯(cuò)誤日志,并將日志授權(quán)給指定的用戶 if [ $want_syslog -eq 0 ]; then touch "$err_log" chown $user "$err_log" fi # 這里它還對(duì)當(dāng)前用戶做了ulimit設(shè)置,包括可以打開的文件數(shù)量--open_files-limit選項(xiàng) if test -n "$open_files" then ulimit -n $open_files append_arg_to_args "--open-files-limit=$open_files" fi fi safe_mysql_unix_port={mysql_unix_port:-${MYSQL_UNIX_PORT:-/usr/local/mysql3306/tmp/mysql.sock}} # 確保 $safe_mysql_unix_port 目錄是存在的 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 # 如果用戶沒有制定mysqld程序的名稱,這里就默認(rèn)賦值為mysqld if test -z "$MYSQLD" then MYSQLD=mysqld fi # 下面幾段分別是對(duì) mysqld , pid , port文件選項(xiàng)的檢查和設(shè)置,省略100個(gè)字 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/`/bin/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 # # 接下來(lái)是關(guān)于優(yōu)先級(jí)的設(shè)置 # if test $niceness -eq 0 then NOHUP_NICENESS="nohup" else NOHUP_NICENESS="nohup nice -$niceness" fi # 將當(dāng)前的默認(rèn)優(yōu)先級(jí)設(shè)置為0 if nohup nice > /dev/null 2>&1 then # normal_niceness記載默認(rèn)的調(diào)度優(yōu)先級(jí) normal_niceness=`nice` # nohup_niceness記載使用nohup執(zhí)行方式的調(diào)度優(yōu)先級(jí) nohup_niceness=`nohup nice 2>/dev/null` numeric_nice_values=1 # 這個(gè)for是為了檢查$normal_niceness $nohup_niceness兩個(gè)變量值的合法性 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 # 這個(gè)判斷結(jié)構(gòu)很重要 # 它保證了使用nohup執(zhí)行的mysqld程序在調(diào)度優(yōu)先級(jí)上不會(huì)低于直接執(zhí)行mysqld程序的方式 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 # 進(jìn)入分支說(shuō)明$nohup_niceness的值比$normal_niceness大,即nohup執(zhí)行方式調(diào)度優(yōu)先級(jí)比正常執(zhí)行方式低 # 這是不希望看到的,所以下面就人為的提升了nohup的優(yōu)先級(jí)(降低niceness的值) niceness=`expr $niceness - $nice_value_diff` NOHUP_NICENESS="nice -$niceness nohup" fi fi else # 下面是測(cè)試nohup在當(dāng)前系統(tǒng)中是否可用,不可用的話就置空NOHUP_NICENESS if nohup echo testing > /dev/null 2>&1 then : else NOHUP_NICENESS="" fi fi # 指定內(nèi)核文件大小 if test -n "$core_file_size" then ulimit -c $core_file_size fi # # 如果已經(jīng)存在一個(gè)pid文件,則檢查是否有已經(jīng)啟動(dòng)的mysqld_safe進(jìn)程 if test -f "$pid_file" then PID=`cat "$pid_file"` if /bin/kill -0 $PID > /dev/null 2> /dev/null then if /bin/ps wwwp $PID | grep -v " grep" | grep -v mysqld_safe | grep -- "$MYSQLD" > /dev/null then log_error "A mysqld process already exists" exit 1 fi fi # 下面是處理方法:刪除舊的pid文件并報(bào)錯(cuò) 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 # # 下面便是拼接執(zhí)行語(yǔ)句運(yùn)行了. # cmd="$NOHUP_NICENESS" # 檢查一下命令 并進(jìn)行轉(zhuǎn)義操作 for i in "$ledir/$MYSQLD" "$defaults" "--basedir=$MY_BASEDIR_VERSION" \ "--datadir=$DATADIR" "$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" # 后臺(tái)循環(huán) 執(zhí)行mysqld while true do rm -f $safe_mysql_unix_port "$pid_file" # 保險(xiǎn)起見,又刪除了一次pid文件 # 調(diào)用eval_log_error函數(shù),傳入$cmd參數(shù)的值,最后使用eval命令執(zhí)行了啟動(dòng)mysqld eval_log_error "$cmd" if test ! -f "$pid_file" # 沒有成功創(chuàng)建pid文件,則退出分支 then break fi # mysqld_safe已經(jīng)啟動(dòng)的處理方法,保證只有一個(gè)mysqld_safe程序啟動(dòng) if true && test $KILL_MYSQLD -eq 1 then # 統(tǒng)計(jì)啟動(dòng)的mysqld進(jìn)程的數(shù)目 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 # 這個(gè)PROC的數(shù)據(jù)即是ps mysqld_safe程序的輸出 第一個(gè)數(shù)字即為進(jìn)程ID PROC=`ps xaww | grep "$ledir/$MYSQLD\>" | grep -v "grep" | grep "pid-file=$pid_file" | sed -n '$p'` # 使用T來(lái)獲取進(jìn)程ID for T in $PROC do break done # kill掉該個(gè)mysqld_safe程序 if kill -9 $T then log_error "$MYSQLD process hanging, pid $T - killed" else break fi # 每干掉一個(gè)mysqld_safe就把I加一,這樣沒有多余的mysqld_safe時(shí)就可以跳出循環(huán)了 I=`expr $I + 1` done fi log_notice "mysqld restarted" done # 完結(jié)撒花 log_notice "mysqld from pid file $pid_file ended"
歡迎參與《Mysql應(yīng)用mysqld_safe啟動(dòng)腳本源碼閱讀、分析》討論,分享您的想法,維易PHP學(xué)院為您提供專業(yè)教程。
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.snjht.com/jiaocheng/10473.html