很多童鞋在啟動(dòng)mysql的時(shí)候,碰到過(guò)這個(gè)錯(cuò)誤,
首先,澄清一點(diǎn),出現(xiàn)這個(gè)錯(cuò)誤的前提是:通過(guò)服務(wù)腳本來(lái)啟動(dòng)mysql。通過(guò)mysqld_safe或mysqld啟動(dòng)mysql實(shí)例并不會(huì)報(bào)這個(gè)錯(cuò)誤。
那么,出現(xiàn)這個(gè)錯(cuò)誤的原因具體是什么呢?
哈哈,對(duì)分析過(guò)程不care的童鞋可直接跳到文末的總結(jié)部分~
總結(jié)
下面,來(lái)分析下mysql的服務(wù)啟動(dòng)腳本
腳本完整內(nèi)容如下:
#!/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 daemon start/stop script.
# Usually this is put in /etc/init.d (at least on machines SYSV R4 based
# systems) and linked to /etc/rc3.d/S99mysql and /etc/rc0.d/K01mysql.
# When this is done the mysql server will be started when the machine is
# started and shut down when the systems goes down.
# Comments to support chkconfig on RedHat Linux
# chkconfig: 2345 64 36
# description: A very fast and reliable SQL database engine.
# Comments to support LSB init script conventions
### BEGIN INIT INFO
# Provides: mysql
# Required-Start: $local_fs $network $remote_fs
# Should-Start: ypbind nscd ldap ntpd xntpd
# Required-Stop: $local_fs $network $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop MySQL
# Description: MySQL is a very fast and reliable SQL database engine.
### END INIT INFO
# If you install MySQL on some other places than /usr/local/mysql, then you
# have to do one of the following things for this script to work:
#
# - Run this script from within the MySQL installation directory
# - Create a /etc/my.cnf file with the following information:
# [mysqld]
# basedir=path-to-mysql-installation-directory>
# - Add the above to any other configuration file (for example ~/.my.ini)
# and copy my_print_defaults to /usr/bin
# - Add the path to the mysql-installation-directory to the basedir variable
# below.
#
# If you want to affect other MySQL variables, you should make your changes
# in the /etc/my.cnf, ~/.my.cnf or other MySQL configuration files.
# If you change base dir, you must also change datadir. These may get
# overwritten by settings in the MySQL configuration files.
basedir=
datadir=
# Default value, in seconds, afterwhich the script should timeout waiting
# for server start.
# Value here is overriden by value in my.cnf.
# 0 means don't wait at all
# Negative numbers mean to wait indefinitely
service_startup_timeout=900
# Lock directory for RedHat / SuSE.
lockdir='/var/lock/subsys'
lock_file_path="$lockdir/mysql"
# The following variables are only set for letting mysql.server find things.
# Set some defaults
mysqld_pid_file_path=
if test -z "$basedir"
then
basedir=/usr/local/mysql
bindir=/usr/local/mysql/bin
if test -z "$datadir"
then
datadir=/usr/local/mysql/data
fi
sbindir=/usr/local/mysql/bin
libexecdir=/usr/local/mysql/bin
else
bindir="$basedir/bin"
if test -z "$datadir"
then
datadir="$basedir/data"
fi
sbindir="$basedir/sbin"
libexecdir="$basedir/libexec"
fi
# datadir_set is used to determine if datadir was set (and so should be
# *not* set inside of the --basedir= handler.)
datadir_set=
#
# Use LSB init script functions for printing messages, if possible
#
lsb_functions="/lib/lsb/init-functions"
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
[ $# -ge 1 ] shift
other_args="$*" # uncommon, but needed when called from an RPM upgrade action
# Expected: "--skip-networking --skip-grant-tables"
# They are not checked here, intentionally, as it is the resposibility
# of the "spec" file author to give correct arguments only.
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
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
;;
--pid-file=*) mysqld_pid_file_path=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
--service-startup-timeout=*) service_startup_timeout=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
esac
done
}
wait_for_pid () {
verb="$1" # created | removed
pid="$2" # process ID of the program operating on the pid-file
pid_file_path="$3" # path to 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_path" i='' break
;;
'removed')
# wait for this PID-file to disappear
test ! -s "$pid_file_path" i='' break
;;
*)
echo "wait_for_pid () usage: wait_for_pid created|removed pid pid_file_path"
exit 1
;;
esac
# if server isn't running, then pid-file will never be updated
if test -n "$pid"; then
if kill -0 "$pid" 2>/dev/null; then
: # the server still runs
else
# The server 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 "The server quit without updating PID file ($pid_file_path)."
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
}
# Get arguments from the my.cnf file,
# the only group, which is read from now on is [mysqld]
if test -x ./bin/my_print_defaults
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
#
# Read defaults file from 'basedir'. If there is no defaults file there
# check if it's in the old (depricated) place (datadir) and read it from there
#
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 `$print_defaults $extra_args mysqld server mysql_server mysql.server`
#
# Set pid file if not given
#
if test -z "$mysqld_pid_file_path"
then
mysqld_pid_file_path=$datadir/`hostname`.pid
else
case "$mysqld_pid_file_path" in
/* ) ;;
* ) mysqld_pid_file_path="$datadir/$mysqld_pid_file_path" ;;
esac
fi
case "$mode" in
'start')
# Start daemon
# Safeguard (relative paths, core dumps..)
cd $basedir
echo $echo_n "Starting MySQL"
if test -x $bindir/mysqld_safe
then
# Give extra arguments to mysqld with the my.cnf file. This script
# may be overwritten at next upgrade.
$bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null 2>1
wait_for_pid created "$!" "$mysqld_pid_file_path"; return_value=$?
# Make lock for RedHat / SuSE
if test -w "$lockdir"
then
touch "$lock_file_path"
fi
exit $return_value
else
log_failure_msg "Couldn't find MySQL server ($bindir/mysqld_safe)"
fi
;;
'stop')
# Stop daemon. We use a signal here to avoid having to know the
# root password.
if test -s "$mysqld_pid_file_path"
then
mysqld_pid=`cat "$mysqld_pid_file_path"`
if (kill -0 $mysqld_pid 2>/dev/null)
then
echo $echo_n "Shutting down MySQL"
kill $mysqld_pid
# mysqld should remove the pid file when it exits, so wait for it.
wait_for_pid removed "$mysqld_pid" "$mysqld_pid_file_path"; return_value=$?
else
log_failure_msg "MySQL server process #$mysqld_pid is not running!"
rm "$mysqld_pid_file_path"
fi
# Delete lock for RedHat / SuSE
if test -f "$lock_file_path"
then
rm -f "$lock_file_path"
fi
exit $return_value
else
log_failure_msg "MySQL server PID file could not be found!"
fi
;;
'restart')
# Stop the service and regardless of whether it was
# running or not, start it again.
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 "$mysqld_pid_file_path" ; then
read mysqld_pid "$mysqld_pid_file_path"
kill -HUP $mysqld_pid log_success_msg "Reloading service MySQL"
touch "$mysqld_pid_file_path"
else
log_failure_msg "MySQL PID file could not be found!"
exit 1
fi
;;
'status')
# First, check to see if pid file exists
if test -s "$mysqld_pid_file_path" ; then
read mysqld_pid "$mysqld_pid_file_path"
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 $libexecdir/mysqld`
# test if multiple pids exist
pid_count=`echo $mysqld_pid | wc -w`
if test $pid_count -gt 1 ; then
log_failure_msg "Multiple MySQL running but PID file could not be found ($mysqld_pid)"
exit 5
elif test -z $mysqld_pid ; then
if test -f "$lock_file_path" ; then
log_failure_msg "MySQL is not running, but lock file ($lock_file_path) 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
basename=`basename "$0"`
echo "Usage: $basename {start|stop|restart|reload|force-reload|status} [ MySQL server options ]"
exit 1
;;
esac
exit 0
首先,定義相關(guān)參數(shù)
basedir=
datadir=
# Default value, in seconds, afterwhich the script should timeout waiting
# for server start.
# Value here is overriden by value in my.cnf.
# 0 means don't wait at all
# Negative numbers mean to wait indefinitely
service_startup_timeout=900
# Lock directory for RedHat / SuSE.
lockdir='/var/lock/subsys'
lock_file_path="$lockdir/mysql"
其中,
basedir 指的二進(jìn)制壓縮包解壓后所在的目錄,譬如/usr/local/mysql。
datadir 指的是數(shù)據(jù)目錄
service_startup_timeout=900 定義mysql服務(wù)啟動(dòng)的時(shí)間限制,如果在900s中沒(méi)有啟動(dòng)成功,則該腳本會(huì)退出。
lockdir='/var/lock/subsys'
關(guān)于/var/lock/subsys,網(wǎng)上的解釋如下,后續(xù)會(huì)用到。
總的來(lái)說(shuō),系統(tǒng)關(guān)閉的過(guò)程(發(fā)出關(guān)閉信號(hào),調(diào)用服務(wù)自身的進(jìn)程)中會(huì)檢查/var/lock/subsys下的文件,逐一關(guān)閉每個(gè)服務(wù),如果某一運(yùn)行的服務(wù)在/var/lock/subsys下沒(méi)有相應(yīng)的選項(xiàng)。在系統(tǒng)關(guān)閉的時(shí)候,會(huì)像殺死普通進(jìn)程一樣殺死這個(gè)服務(wù)。
通過(guò)察看/etc/rc.d/init.d下的腳本,可以發(fā)現(xiàn)每個(gè)服務(wù)自己操縱時(shí)都會(huì)去查看/var/lock/subsys下相應(yīng)的服務(wù)。
很多程序需要判斷是否當(dāng)前已經(jīng)有一個(gè)實(shí)例在運(yùn)行,這個(gè)目錄就是讓程序判斷是否有實(shí)例運(yùn)行的標(biāo)志,比如說(shuō)xinetd,如果存在這個(gè)文件,表示已經(jīng)有xinetd在運(yùn)行了,否則就是沒(méi)有,當(dāng)然程序里面還要有相應(yīng)的判斷措施來(lái)真正確定是否有實(shí)例在運(yùn)行。通常與該目錄配套的還有/var/run目錄,用來(lái)存放對(duì)應(yīng)實(shí)例的PID,如果你寫腳本的話,會(huì)發(fā)現(xiàn)這2個(gè)目錄結(jié)合起來(lái)可以很方便的判斷出許多服務(wù)是否在運(yùn)行,運(yùn)行的相關(guān)信息等等。
判斷basedir和datadir
# Set some defaults
mysqld_pid_file_path=
if test -z "$basedir"
then
basedir=/usr/local/mysql
bindir=/usr/local/mysql/bin
if test -z "$datadir"
then
datadir=/usr/local/mysql/data
fi
sbindir=/usr/local/mysql/bin
libexecdir=/usr/local/mysql/bin
else
bindir="$basedir/bin"
if test -z "$datadir"
then
datadir="$basedir/data"
fi
sbindir="$basedir/sbin"
libexecdir="$basedir/libexec"
fi
其中,
mysqld_pid_file_path 指定pid文件的路徑
-z string 判斷字符串是否為空
如果basedir沒(méi)有顯示設(shè)置,則默認(rèn)為/usr/local/mysql,這也是為什么很多mysql安裝教程都推薦將mysql相關(guān)文件放到/usr/local/mysql下。
如果datadir沒(méi)有顯示設(shè)置,則默認(rèn)為$basedir/data。
定義log_success_msg()和log_failure_msg()函數(shù)
首先,判斷/lib/lsb/init-functions文件是否存在,如果存在,則使定義在init-functions文件中的所有shell函數(shù)在當(dāng)前腳本中生效。
如果沒(méi)有,則定義兩個(gè)函數(shù),一個(gè)用于打印成功日志,一個(gè)是打印錯(cuò)誤日志。
在RHCS 6.7中,該文件并不存在,已被/etc/init.d/functions所替代。
#
# Use LSB init script functions for printing messages, if possible
#
lsb_functions="/lib/lsb/init-functions"
if test -f $lsb_functions ; then
. $lsb_functions
else
log_success_msg()
{
echo " SUCCESS! $@"
}
log_failure_msg()
{
echo " ERROR! $@"
}
fi
傳遞參數(shù)
將第一個(gè)參數(shù)傳遞給mode,剩下的參數(shù)傳遞給other_args
PATH="/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin"
export PATH
mode=$1 # start or stop
[ $# -ge 1 ] shift
other_args="$*" # uncommon, but needed when called from an RPM upgrade action
# Expected: "--skip-networking --skip-grant-tables"
# They are not checked here, intentionally, as it is the resposibility
# of the "spec" file author to give correct arguments only.
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
解析配置文件中的參數(shù)
這個(gè)函數(shù)在腳本后面會(huì)涉及到。
主要涉及如下參數(shù):--basedir,--datadir,--pid-file,--service-startup-timeout。
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
;;
--pid-file=*) mysqld_pid_file_path=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
--service-startup-timeout=*) service_startup_timeout=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
esac
done
}
判斷my_print_defaults的位置
首先,它判斷當(dāng)前路徑下的bin目錄中是否存在該可執(zhí)行文件,如果不存在,則再判斷$bindir(通常指的是$basedir/bin)目錄下是否存在。
如果還是沒(méi)有,則會(huì)判斷/etc/my.cnf是否存在并且可讀,如果是,則判斷該配置文件中是否指定了basedir參數(shù),
如果指定了,則取出該參數(shù)的值,并判斷該值對(duì)應(yīng)的目錄中是否存在bin/my_print_defaults可執(zhí)行文件
最后一步,如果在上述目錄中實(shí)在沒(méi)發(fā)現(xiàn)my_print_defaults文件,
索性就將print_defaults設(shè)置為"my_print_defaults",寄希望于該命令在當(dāng)前的PATH環(huán)境中。
# Get arguments from the my.cnf file,
# the only group, which is read from now on is [mysqld]
if test -x ./bin/my_print_defaults
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
查找默認(rèn)的配置文件
-r file 如果文件可讀,則為真
#
# Read defaults file from 'basedir'. If there is no defaults file there
# check if it's in the old (depricated) place (datadir) and read it from there
#
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
解析配置文件中的參數(shù)
my_print_defaults的用法如下:
my_print_defaults --defaults-file=example.cnf client mysql
即讀取配置文件中,client和mysql部分的參數(shù)配置,
具體在本腳本中,是讀取mysqld,server,mysql_server,mysql.server四個(gè)部分的配置參數(shù)。
parse_server_arguments `$print_defaults $extra_args mysqld server mysql_server mysql.server`
設(shè)置pid file的路徑
-z string 判斷字符串是否為空
如果--pid-file沒(méi)有在讀取到的配置文件中設(shè)置或者腳本剛開(kāi)始的mysqld_pid_file_path參數(shù)沒(méi)有設(shè)置,
則pid file默認(rèn)設(shè)置在datadir下,以主機(jī)名.pid命名。
如果該參數(shù)設(shè)置了,還需要進(jìn)一步判斷
如果該參數(shù)中帶有斜杠,則代表給定的值帶有路徑,可直接使用。
如果該參數(shù)中沒(méi)帶路徑,則代表給定的值只是pid的文件名,可將其設(shè)在datadir下。
#
# Set pid file if not given
#
if test -z "$mysqld_pid_file_path"
then
mysqld_pid_file_path=$datadir/`hostname`.pid
else
case "$mysqld_pid_file_path" in
/* ) ;;
* ) mysqld_pid_file_path="$datadir/$mysqld_pid_file_path" ;;
esac
fi
服務(wù)腳本start選項(xiàng)
首先,切換到$basedir中
其次,判斷$basedir/bin中的mysqld_safe是否是可執(zhí)行文件,如果是,則啟動(dòng)mysqld實(shí)例,如果不是,則報(bào)錯(cuò)退出。
那么,啟動(dòng)流程又是如何實(shí)現(xiàn)的呢?
首先,執(zhí)行$bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null 2>1 命令,啟動(dòng)mysqld實(shí)例。
注意到?jīng)]有,mysqld_safe其實(shí)是在basedir中執(zhí)行的,包括mysql初始化腳本mysql_install_db,也建議在basedir中執(zhí)行,具體可參考:
分析MariaDB初始化腳本mysql_install_db
然后通過(guò)wait_for_pid函數(shù)進(jìn)行判斷,具體可見(jiàn)下文對(duì)于wait_for_pid函數(shù)的分析
判斷完畢后,
查看$lockdir目錄是否可寫,可寫的話,則在目錄上創(chuàng)建一個(gè)文件。
case "$mode" in
'start')
# Start daemon
# Safeguard (relative paths, core dumps..)
cd $basedir
echo $echo_n "Starting MySQL"
if test -x $bindir/mysqld_safe
then
# Give extra arguments to mysqld with the my.cnf file. This script
# may be overwritten at next upgrade.
$bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null 2>1
wait_for_pid created "$!" "$mysqld_pid_file_path"; return_value=$?
# Make lock for RedHat / SuSE
if test -w "$lockdir"
then
touch "$lock_file_path"
fi
exit $return_value
else
log_failure_msg "Couldn't find MySQL server ($bindir/mysqld_safe)"
fi
;;
wait_for_pid函數(shù)
在利用mysqld_safe啟動(dòng)mysql實(shí)例后,會(huì)調(diào)用該參數(shù)
wait_for_pid created "$!" "$mysqld_pid_file_path"; return_value=$?
其中$!在shell中用于獲取最后運(yùn)行的后臺(tái)Process的PID,具體在本例中,是mysqld_safe進(jìn)程的pid。
因?yàn)榈谝粋€(gè)參數(shù)是created,所以會(huì)執(zhí)行test -s "$pid_file_path" i='' break
命令。
-s file 如果文件的長(zhǎng)度不為零,則為真
該命令的意思是如果pid文件存在,則將變量i設(shè)置為空,并退出while循環(huán)。
然后執(zhí)行如下判斷,
if test -z "$i" ; then
log_success_msg
return 0
else
log_failure_msg
return 1
fi
如果$i為空,則打印成功日志,并退出腳本,很顯然,在pid文件存在的情況下,會(huì)將變量i設(shè)置為空。
再來(lái)看看pid文件不存在的情況
首先,會(huì)判斷$pid是否不為空(即if test -n "$pid")
如果不為空,則代表在執(zhí)行完mysqld_safe后,已經(jīng)捕捉到了該進(jìn)程的pid。
在這種情況下,進(jìn)一步通過(guò)kill -0 "$pid"確認(rèn)該進(jìn)程是否存在。
kill -0就是不發(fā)送任何信號(hào),但是系統(tǒng)會(huì)進(jìn)行錯(cuò)誤檢查,所以經(jīng)常用來(lái)檢查一個(gè)進(jìn)程是否存在,當(dāng)進(jìn)程不存在時(shí), kill -0 pid會(huì)返回錯(cuò)誤
如果該進(jìn)程存在,則不執(zhí)行任何操作,直接跳到如下操作
echo $echo_n ".$echo_c"
i=`expr $i + 1`
sleep 1
將變量i加1,并sleep 1s。
然后,繼續(xù)while循環(huán),之所以這樣做,是考慮到mysqld_safe已經(jīng)執(zhí)行,但是mysqld實(shí)例還在啟動(dòng)過(guò)程中,還沒(méi)創(chuàng)建好pid文件。
一直到$1達(dá)到$service_startup_timeout定義的時(shí)長(zhǎng)。
如果在while循環(huán)的過(guò)程中,通過(guò)kill -0 "$pid"判斷到進(jìn)程已經(jīng)不存在了,
則會(huì)再判斷一次,如果這次判斷的結(jié)果依舊是pid file不存在,且進(jìn)程不存在,則會(huì)執(zhí)行
log_failure_msg "The server quit without updating PID file ($pid_file_path)."
這就是大名鼎鼎的“The server quit without updating PID file”的由來(lái)。
wait_for_pid () {
verb="$1" # created | removed
pid="$2" # process ID of the program operating on the pid-file
pid_file_path="$3" # path to 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_path" i='' break
;;
'removed')
# wait for this PID-file to disappear
test ! -s "$pid_file_path" i='' break
;;
*)
echo "wait_for_pid () usage: wait_for_pid created|removed pid pid_file_path"
exit 1
;;
esac
# if server isn't running, then pid-file will never be updated
if test -n "$pid"; then
if kill -0 "$pid" 2>/dev/null; then
: # the server still runs
else
# The server 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 "The server quit without updating PID file ($pid_file_path)."
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
}
服務(wù)腳本stop選項(xiàng)
首先,判斷pid文件的長(zhǎng)度是否不為零。
-s file 如果文件的長(zhǎng)度不為零,則為真
此時(shí),會(huì)通過(guò)pid文件獲取mysqld進(jìn)程的pid,注意,不是mysqld_safe進(jìn)程的pid
然后,判斷mysqld進(jìn)程是否在正常運(yùn)行,
如果是,則通過(guò)kill $mysqld_pid的方式來(lái)關(guān)閉mysqld進(jìn)程
殺死進(jìn)程最安全的方法是單純使用kill命令,不加修飾符,不帶標(biāo)志。
標(biāo)準(zhǔn)的kill命令通常會(huì)終止有問(wèn)題的進(jìn)程,并把進(jìn)程的資源釋放給系統(tǒng)。然而,如果進(jìn)程啟動(dòng)了子進(jìn)程,只殺死父進(jìn)程,子進(jìn)程仍在運(yùn)行,因此仍消耗資源。為了防止這些所謂的“僵尸進(jìn)程”,應(yīng)確保在殺死父進(jìn)程之前,先殺死其所有的子進(jìn)程。
然后,調(diào)用wait_for_pid函數(shù)進(jìn)行判斷,其實(shí),wait_for_pid函數(shù)中設(shè)置avoid_race_condition變量的目的是為了stop選項(xiàng),確實(shí)有可能出現(xiàn),mysqld是在檢查pid file之后,檢查進(jìn)程是否存活之前退出的。
如果mysqld進(jìn)程沒(méi)有正常運(yùn)行,在會(huì)打印“MySQL server process #$mysqld_pid is not running!”信息,并刪除pid文件。
如果在執(zhí)行stop的時(shí)候,判斷pid文件的長(zhǎng)度為0,則會(huì)打印"MySQL server PID file could not be found!"信息。
所以,在pid文件不存在的情況下,通過(guò)服務(wù)腳本執(zhí)行stop選項(xiàng)并不會(huì)關(guān)閉mysqld進(jìn)程,這個(gè)時(shí)候,就可通過(guò)kill $mysqld_pid的方式來(lái)關(guān)閉mysqld進(jìn)程。
'stop')
# Stop daemon. We use a signal here to avoid having to know the
# root password.
if test -s "$mysqld_pid_file_path"
then
mysqld_pid=`cat "$mysqld_pid_file_path"`
if (kill -0 $mysqld_pid 2>/dev/null)
then
echo $echo_n "Shutting down MySQL"
kill $mysqld_pid
# mysqld should remove the pid file when it exits, so wait for it.
wait_for_pid removed "$mysqld_pid" "$mysqld_pid_file_path"; return_value=$?
else
log_failure_msg "MySQL server process #$mysqld_pid is not running!"
rm "$mysqld_pid_file_path"
fi
# Delete lock for RedHat / SuSE
if test -f "$lock_file_path"
then
rm -f "$lock_file_path"
fi
exit $return_value
else
log_failure_msg "MySQL server PID file could not be found!"
fi
;;
服務(wù)腳本restart選項(xiàng)
首先,先執(zhí)行stop操作,如果stop操作成功的話,則繼續(xù)執(zhí)行start操作。
如果stop操作失敗的話,則會(huì)輸出"Failed to stop running server, so refusing to try to start."信息,并退出腳本。
'restart')
# Stop the service and regardless of whether it was
# running or not, start it again.
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
;;
服務(wù)腳本reload選項(xiàng)
首先,判斷pid文件的長(zhǎng)度是否為0,如果不為0,則將該文件中的值設(shè)置為mysqld_pid變量的值。
然后對(duì)該進(jìn)程執(zhí)行kill -HUP操作。
pid 是進(jìn)程標(biāo)識(shí)。如果想要更改配置而不需停止并重新啟動(dòng)服務(wù),請(qǐng)使用該命令。在對(duì)配置文件作必要的更改后,發(fā)出該命令以動(dòng)態(tài)更新服務(wù)配置。
根據(jù)約定,當(dāng)您發(fā)送一個(gè)掛起信號(hào)(信號(hào) 1 或 HUP)時(shí),大多數(shù)服務(wù)器進(jìn)程(所有常用的進(jìn)程)都會(huì)進(jìn)行復(fù)位操作并重新加載它們的配置文件。
如果pid文件的長(zhǎng)度為0,則輸出"MySQL PID file could not be found!"。
'reload'|'force-reload')
if test -s "$mysqld_pid_file_path" ; then
read mysqld_pid "$mysqld_pid_file_path"
kill -HUP $mysqld_pid log_success_msg "Reloading service MySQL"
touch "$mysqld_pid_file_path"
else
log_failure_msg "MySQL PID file could not be found!"
exit 1
fi
;;
服務(wù)腳本status選項(xiàng)
首先,判斷pid文件長(zhǎng)度是否為0,如果不是,則讀取該文件中的值,并判斷pid對(duì)應(yīng)的進(jìn)程是否運(yùn)行正常,
如果運(yùn)行正常,則輸出"MySQL running"
如果不正常,則輸出"MySQL is not running, but PID file exists"
如果pid文件的長(zhǎng)度為0,則試圖通過(guò)mysqld的啟動(dòng)命令來(lái)獲取其pid,
這個(gè)時(shí)候,可能存在一個(gè)mysqld程序啟動(dòng)了多個(gè)實(shí)例,這會(huì)導(dǎo)致pid_count=`echo $mysqld_pid | wc -w`大于1。
這個(gè)時(shí)候,會(huì)輸出"Multiple MySQL running but PID file could not be found"信息,并退出腳本。
如果mysqld_pid為空,則會(huì)繼續(xù)判斷"$lock_file_path"是否存在,如果存在,
則會(huì)輸出"MySQL is not running, but lock file ($lock_file_path) exists"信息。
如果"$lock_file_path"不存在,則會(huì)輸出"MySQL is not running"信息。
如果mysqld_pid等于1,則會(huì)輸出"MySQL is running but PID file could not be found"信息。
'status')
# First, check to see if pid file exists
if test -s "$mysqld_pid_file_path" ; then
read mysqld_pid "$mysqld_pid_file_path"
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 $libexecdir/mysqld`
# test if multiple pids exist
pid_count=`echo $mysqld_pid | wc -w`
if test $pid_count -gt 1 ; then
log_failure_msg "Multiple MySQL running but PID file could not be found ($mysqld_pid)"
exit 5
elif test -z $mysqld_pid ; then
if test -f "$lock_file_path" ; then
log_failure_msg "MySQL is not running, but lock file ($lock_file_path) 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
;;
服務(wù)腳本其它選項(xiàng)
如果腳本的第一個(gè)參數(shù)不是上述幾個(gè)選項(xiàng),則會(huì)輸出Usage信息。
*)
# usage
basename=`basename "$0"`
echo "Usage: $basename {start|stop|restart|reload|force-reload|status} [ MySQL server options ]"
exit 1
;;
至此,mysql的服務(wù)腳本分析完畢~
總結(jié)
在通過(guò)服務(wù)腳本啟動(dòng)mysql的過(guò)程中,報(bào)“The server quit without updating PID file”錯(cuò)誤,有兩個(gè)條件
首先,pid文件不存在
其次,通過(guò)kill -0 $pid檢查到進(jìn)程并不存在
這個(gè)時(shí)候,只能通過(guò)mysql數(shù)據(jù)庫(kù)的錯(cuò)誤日志來(lái)定位。
服務(wù)腳本如果不做任何調(diào)整的話,默認(rèn)的basedir是/usr/local/mysql,datadir是/usr/local/mysql/data
如果自己的mysql服務(wù)均不是默認(rèn)路徑,
則需要在該腳本中顯式設(shè)置
經(jīng)測(cè)試,需設(shè)置如下幾處:
1. 設(shè)置basedir和添加conf變量
其中,conf指的是mysqld的配置文件,建議配置文件中顯式指定basedir和datadir的值。
在這里,datadir可不設(shè)置,因?yàn)閐atadir可通過(guò)配置文件來(lái)獲取。
但是basedir必須要指定,因?yàn)橐紫雀鶕?jù)basedir來(lái)判斷my_print_deefauts命令
basedir=/usr/local/mysql-advanced-5.6.23-linux-glibc2.5-x86_64
datadir=
conf=/usr/local/mysql-advanced-5.6.23-linux-glibc2.5-x86_64/my_3308.cnf
2. 第256行,添加extra_args=" -c $conf"
extra_args=" -e $basedir/my.cnf.bak"
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
extra_args=" -c $conf"
3. 修改285行mysqld_safe的啟動(dòng)參數(shù)
將
$bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null 2>1
修改為,
$bindir/mysqld_safe --defaults-file="$conf" --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null 2>1
主要是添加了--defaults-file選項(xiàng)
以上所述是小編給大家介紹的深度解析MySQL啟動(dòng)時(shí)報(bào)“The server quit without updating PID file”錯(cuò)誤的原因,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
您可能感興趣的文章:- Python聊天室?guī)Ы缑鎸?shí)現(xiàn)的示例代碼(tkinter,Mysql,Treading,socket)
- ERROR 2002 (HY000): Can''t connect to local MySQL server through socket ''/tmp/mysql.sock''
- Can''t connect to local MySQL through socket ''/tmp/mysql.sock''解決方法
- java+jdbc+mysql+socket搭建局域網(wǎng)聊天室
- Java Socket+mysql實(shí)現(xiàn)簡(jiǎn)易文件上傳器的代碼
- mysql socket文件作用詳解
- MySQL錯(cuò)誤ERROR 2002 (HY000): Can''t connect to local MySQL server through socket
- MySQL HandlerSocket插件安裝配置教程
- 解決MySQL因不能創(chuàng)建 PID 導(dǎo)致無(wú)法啟動(dòng)的方法
- mysql啟動(dòng)報(bào)錯(cuò)MySQL server PID file could not be found
- Mysql掛掉后無(wú)法重啟報(bào)pid文件丟失的解決方法
- MySQL下PID文件丟失的相關(guān)錯(cuò)誤的解決方法
- mysql:Can''t start server: can''t create PID file: No space left on device
- MySQL提示:The server quit without updating PID file問(wèn)題的解決辦法
- linux mysql 報(bào)錯(cuò):MYSQL:The server quit without updating PID file
- Starting MySQL.Manager of pid-file quit without updating file.[FAILED]的解決方法
- 詳解MySQL中的pid與socket