深度解析MySQL启动时报“The server quit without updating PID file”错误的原因
很多童鞋在启动mysql的时候,碰到过这个错误,
首先,澄清一点,出现这个错误的前提是:通过服务脚本来启动mysql。通过mysqld_safe或mysqld启动mysql实例并不会报这个错误。
那么,出现这个错误的原因具体是什么呢?
哈哈,对分析过程不care的童鞋可直接跳到文末的总结部分~
总结
下面,来分析下mysql的服务启动脚本
脚本完整内容如下:
#!/bin/sh #CopyrightAbandoned1996TCXDataKonsultAB&MontyProgramKB&DetronHB #ThisfileispublicdomainandcomeswithNOWARRANTYofanykind #MySQLdaemonstart/stopscript. #Usuallythisisputin/etc/init.d(atleastonmachinesSYSVR4based #systems)andlinkedto/etc/rc3.d/S99mysqland/etc/rc0.d/K01mysql. #Whenthisisdonethemysqlserverwillbestartedwhenthemachineis #startedandshutdownwhenthesystemsgoesdown. #CommentstosupportchkconfigonRedHatLinux #chkconfig:23456436 #description:AveryfastandreliableSQLdatabaseengine. #CommentstosupportLSBinitscriptconventions ###BEGININITINFO #Provides:mysql #Required-Start:$local_fs$network$remote_fs #Should-Start:ypbindnscdldapntpdxntpd #Required-Stop:$local_fs$network$remote_fs #Default-Start:2345 #Default-Stop:016 #Short-Description:startandstopMySQL #Description:MySQLisaveryfastandreliableSQLdatabaseengine. ###ENDINITINFO #IfyouinstallMySQLonsomeotherplacesthan/usr/local/mysql,thenyou #havetodooneofthefollowingthingsforthisscripttowork: # #-RunthisscriptfromwithintheMySQLinstallationdirectory #-Createa/etc/my.cnffilewiththefollowinginformation: #[mysqld] #basedir=#-Addtheabovetoanyotherconfigurationfile(forexample~/.my.ini) #andcopymy_print_defaultsto/usr/bin #-Addthepathtothemysql-installation-directorytothebasedirvariable #below. # #IfyouwanttoaffectotherMySQLvariables,youshouldmakeyourchanges #inthe/etc/my.cnf,~/.my.cnforotherMySQLconfigurationfiles. #Ifyouchangebasedir,youmustalsochangedatadir.Thesemayget #overwrittenbysettingsintheMySQLconfigurationfiles. basedir= datadir= #Defaultvalue,inseconds,afterwhichthescriptshouldtimeoutwaiting #forserverstart. #Valuehereisoverridenbyvalueinmy.cnf. #0meansdon'twaitatall #Negativenumbersmeantowaitindefinitely service_startup_timeout=900 #LockdirectoryforRedHat/SuSE. lockdir='/var/lock/subsys' lock_file_path="$lockdir/mysql" #Thefollowingvariablesareonlysetforlettingmysql.serverfindthings. #Setsomedefaults mysqld_pid_file_path= iftest-z"$basedir" then basedir=/usr/local/mysql bindir=/usr/local/mysql/bin iftest-z"$datadir" then datadir=/usr/local/mysql/data fi sbindir=/usr/local/mysql/bin libexecdir=/usr/local/mysql/bin else bindir="$basedir/bin" iftest-z"$datadir" then datadir="$basedir/data" fi sbindir="$basedir/sbin" libexecdir="$basedir/libexec" fi #datadir_setisusedtodetermineifdatadirwasset(andsoshouldbe #*not*setinsideofthe--basedir=handler.) datadir_set= # #UseLSBinitscriptfunctionsforprintingmessages,ifpossible # lsb_functions="/lib/lsb/init-functions" iftest-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" exportPATH mode=$1#startorstop [$#-ge1]&&shift other_args="$*"#uncommon,butneededwhencalledfromanRPMupgradeaction #Expected:"--skip-networking--skip-grant-tables" #Theyarenotcheckedhere,intentionally,asitistheresposibility #ofthe"spec"fileauthortogivecorrectargumentsonly. case`echo"testing\c"`,`echo-ntesting`in *c*,-n*)echo_n=echo_c=;; *c*,*)echo_n=-necho_c=;; *)echo_n=echo_c='\c';; esac parse_server_arguments(){ forargdo case"$arg"in --basedir=*)basedir=`echo"$arg"|sed-e's/^[^=]*=//'` bindir="$basedir/bin" iftest-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"#processIDoftheprogramoperatingonthepid-file pid_file_path="$3"#pathtothePIDfile. i=0 avoid_race_condition="bycheckingagain" whiletest$i-ne$service_startup_timeout;do case"$verb"in 'created') #waitforaPID-filetopopintoexistence. test-s"$pid_file_path"&&i=''&&break ;; 'removed') #waitforthisPID-filetodisappear test!-s"$pid_file_path"&&i=''&&break ;; *) echo"wait_for_pid()usage:wait_for_pidcreated|removedpidpid_file_path" exit1 ;; esac #ifserverisn'trunning,thenpid-filewillneverbeupdated iftest-n"$pid";then ifkill-0"$pid"2>/dev/null;then :#theserverstillruns else #Theservermayhaveexitedbetweenthelastpid-filecheckandnow. iftest-n"$avoid_race_condition";then avoid_race_condition="" continue#Checkagain. fi #there'snothingthatwillaffectthefile. log_failure_msg"TheserverquitwithoutupdatingPIDfile($pid_file_path)." return1#notwaitinganymore. fi fi echo$echo_n".$echo_c" i=`expr$i+1` sleep1 done iftest-z"$i";then log_success_msg return0 else log_failure_msg return1 fi } #Getargumentsfromthemy.cnffile, #theonlygroup,whichisreadfromnowonis[mysqld] iftest-x./bin/my_print_defaults then print_defaults="./bin/my_print_defaults" eliftest-x$bindir/my_print_defaults then print_defaults="$bindir/my_print_defaults" eliftest-x$bindir/mysql_print_defaults then print_defaults="$bindir/mysql_print_defaults" else #Trytofindbasedirin/etc/my.cnf conf=/etc/my.cnf print_defaults= iftest-r$conf then subpat='^[^=]*basedir[^=]*=\(.*\)$' dirs=`sed-e"/$subpat/!d"-e's//\1/'$conf` fordin$dirs do d=`echo$d|sed-e's/[]//g'` iftest-x"$d/bin/my_print_defaults" then print_defaults="$d/bin/my_print_defaults" break fi iftest-x"$d/bin/mysql_print_defaults" then print_defaults="$d/bin/mysql_print_defaults" break fi done fi #Hopeit'sinthePATH...butIdoubtit test-z"$print_defaults"&&print_defaults="my_print_defaults" fi # #Readdefaultsfilefrom'basedir'.Ifthereisnodefaultsfilethere #checkifit'sintheold(depricated)place(datadir)andreaditfromthere # extra_args="" iftest-r"$basedir/my.cnf" then extra_args="-e$basedir/my.cnf" else iftest-r"$datadir/my.cnf" then extra_args="-e$datadir/my.cnf" fi fi parse_server_arguments`$print_defaults$extra_argsmysqldservermysql_servermysql.server` # #Setpidfileifnotgiven # iftest-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') #Startdaemon #Safeguard(relativepaths,coredumps..) cd$basedir echo$echo_n"StartingMySQL" iftest-x$bindir/mysqld_safe then #Giveextraargumentstomysqldwiththemy.cnffile.Thisscript #maybeoverwrittenatnextupgrade. $bindir/mysqld_safe--datadir="$datadir"--pid-file="$mysqld_pid_file_path"$other_args>/dev/null2>&1& wait_for_pidcreated"$!""$mysqld_pid_file_path";return_value=$? #MakelockforRedHat/SuSE iftest-w"$lockdir" then touch"$lock_file_path" fi exit$return_value else log_failure_msg"Couldn'tfindMySQLserver($bindir/mysqld_safe)" fi ;; 'stop') #Stopdaemon.Weuseasignalheretoavoidhavingtoknowthe #rootpassword. iftest-s"$mysqld_pid_file_path" then mysqld_pid=`cat"$mysqld_pid_file_path"` if(kill-0$mysqld_pid2>/dev/null) then echo$echo_n"ShuttingdownMySQL" kill$mysqld_pid #mysqldshouldremovethepidfilewhenitexits,sowaitforit. wait_for_pidremoved"$mysqld_pid""$mysqld_pid_file_path";return_value=$? else log_failure_msg"MySQLserverprocess#$mysqld_pidisnotrunning!" rm"$mysqld_pid_file_path" fi #DeletelockforRedHat/SuSE iftest-f"$lock_file_path" then rm-f"$lock_file_path" fi exit$return_value else log_failure_msg"MySQLserverPIDfilecouldnotbefound!" fi ;; 'restart') #Stoptheserviceandregardlessofwhetheritwas #runningornot,startitagain. if$0stop$other_args;then $0start$other_args else log_failure_msg"Failedtostoprunningserver,sorefusingtotrytostart." exit1 fi ;; 'reload'|'force-reload') iftest-s"$mysqld_pid_file_path";then readmysqld_pid<"$mysqld_pid_file_path" kill-HUP$mysqld_pid&&log_success_msg"ReloadingserviceMySQL" touch"$mysqld_pid_file_path" else log_failure_msg"MySQLPIDfilecouldnotbefound!" exit1 fi ;; 'status') #First,checktoseeifpidfileexists iftest-s"$mysqld_pid_file_path";then readmysqld_pid<"$mysqld_pid_file_path" ifkill-0$mysqld_pid2>/dev/null;then log_success_msg"MySQLrunning($mysqld_pid)" exit0 else log_failure_msg"MySQLisnotrunning,butPIDfileexists" exit1 fi else #Trytofindappropriatemysqldprocess mysqld_pid=`pidof$libexecdir/mysqld` #testifmultiplepidsexist pid_count=`echo$mysqld_pid|wc-w` iftest$pid_count-gt1;then log_failure_msg"MultipleMySQLrunningbutPIDfilecouldnotbefound($mysqld_pid)" exit5 eliftest-z$mysqld_pid;then iftest-f"$lock_file_path";then log_failure_msg"MySQLisnotrunning,butlockfile($lock_file_path)exists" exit2 fi log_failure_msg"MySQLisnotrunning" exit3 else log_failure_msg"MySQLisrunningbutPIDfilecouldnotbefound" exit4 fi fi ;; *) #usage basename=`basename"$0"` echo"Usage:$basename{start|stop|restart|reload|force-reload|status}[MySQLserveroptions]" exit1 ;; esac exit0
首先,定义相关参数
basedir= datadir= #Defaultvalue,inseconds,afterwhichthescriptshouldtimeoutwaiting #forserverstart. #Valuehereisoverridenbyvalueinmy.cnf. #0meansdon'twaitatall #Negativenumbersmeantowaitindefinitely service_startup_timeout=900 #LockdirectoryforRedHat/SuSE. lockdir='/var/lock/subsys' lock_file_path="$lockdir/mysql"
其中,
basedir指的二进制压缩包解压后所在的目录,譬如/usr/local/mysql。
datadir指的是数据目录
service_startup_timeout=900定义mysql服务启动的时间限制,如果在900s中没有启动成功,则该脚本会退出。
lockdir='/var/lock/subsys'
关于/var/lock/subsys,网上的解释如下,后续会用到。
总的来说,系统关闭的过程(发出关闭信号,调用服务自身的进程)中会检查/var/lock/subsys下的文件,逐一关闭每个服务,如果某一运行的服务在/var/lock/subsys下没有相应的选项。在系统关闭的时候,会像杀死普通进程一样杀死这个服务。
通过察看/etc/rc.d/init.d下的脚本,可以发现每个服务自己操纵时都会去查看/var/lock/subsys下相应的服务。
很多程序需要判断是否当前已经有一个实例在运行,这个目录就是让程序判断是否有实例运行的标志,比如说xinetd,如果存在这个文件,表示已经有xinetd在运行了,否则就是没有,当然程序里面还要有相应的判断措施来真正确定是否有实例在运行。通常与该目录配套的还有/var/run目录,用来存放对应实例的PID,如果你写脚本的话,会发现这2个目录结合起来可以很方便的判断出许多服务是否在运行,运行的相关信息等等。
判断basedir和datadir
#Setsomedefaults mysqld_pid_file_path= iftest-z"$basedir" then basedir=/usr/local/mysql bindir=/usr/local/mysql/bin iftest-z"$datadir" then datadir=/usr/local/mysql/data fi sbindir=/usr/local/mysql/bin libexecdir=/usr/local/mysql/bin else bindir="$basedir/bin" iftest-z"$datadir" then datadir="$basedir/data" fi sbindir="$basedir/sbin" libexecdir="$basedir/libexec" fi
其中,
mysqld_pid_file_path指定pid文件的路径
-zstring判断字符串是否为空
如果basedir没有显示设置,则默认为/usr/local/mysql,这也是为什么很多mysql安装教程都推荐将mysql相关文件放到/usr/local/mysql下。
如果datadir没有显示设置,则默认为$basedir/data。
定义log_success_msg()和log_failure_msg()函数
首先,判断/lib/lsb/init-functions文件是否存在,如果存在,则使定义在init-functions文件中的所有shell函数在当前脚本中生效。
如果没有,则定义两个函数,一个用于打印成功日志,一个是打印错误日志。
在RHCS6.7中,该文件并不存在,已被/etc/init.d/functions所替代。
# #UseLSBinitscriptfunctionsforprintingmessages,ifpossible # lsb_functions="/lib/lsb/init-functions" iftest-f$lsb_functions;then .$lsb_functions else log_success_msg() { echo"SUCCESS!$@" } log_failure_msg() { echo"ERROR!$@" } fi
传递参数
将第一个参数传递给mode,剩下的参数传递给other_args
PATH="/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin" exportPATH mode=$1#startorstop [$#-ge1]&&shift other_args="$*"#uncommon,butneededwhencalledfromanRPMupgradeaction #Expected:"--skip-networking--skip-grant-tables" #Theyarenotcheckedhere,intentionally,asitistheresposibility #ofthe"spec"fileauthortogivecorrectargumentsonly. case`echo"testing\c"`,`echo-ntesting`in *c*,-n*)echo_n=echo_c=;; *c*,*)echo_n=-necho_c=;; *)echo_n=echo_c='\c';; esac
解析配置文件中的参数
这个函数在脚本后面会涉及到。
主要涉及如下参数:--basedir,--datadir,--pid-file,--service-startup-timeout。
parse_server_arguments(){ forargdo case"$arg"in --basedir=*)basedir=`echo"$arg"|sed-e's/^[^=]*=//'` bindir="$basedir/bin" iftest-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的位置
首先,它判断当前路径下的bin目录中是否存在该可执行文件,如果不存在,则再判断$bindir(通常指的是$basedir/bin)目录下是否存在。
如果还是没有,则会判断/etc/my.cnf是否存在并且可读,如果是,则判断该配置文件中是否指定了basedir参数,
如果指定了,则取出该参数的值,并判断该值对应的目录中是否存在bin/my_print_defaults可执行文件
最后一步,如果在上述目录中实在没发现my_print_defaults文件,
索性就将print_defaults设置为"my_print_defaults",寄希望于该命令在当前的PATH环境中。
#Getargumentsfromthemy.cnffile, #theonlygroup,whichisreadfromnowonis[mysqld] iftest-x./bin/my_print_defaults then print_defaults="./bin/my_print_defaults" eliftest-x$bindir/my_print_defaults then print_defaults="$bindir/my_print_defaults" eliftest-x$bindir/mysql_print_defaults then print_defaults="$bindir/mysql_print_defaults" else #Trytofindbasedirin/etc/my.cnf conf=/etc/my.cnf print_defaults= iftest-r$conf then subpat='^[^=]*basedir[^=]*=\(.*\)$' dirs=`sed-e"/$subpat/!d"-e's//\1/'$conf` fordin$dirs do d=`echo$d|sed-e's/[]//g'` iftest-x"$d/bin/my_print_defaults" then print_defaults="$d/bin/my_print_defaults" break fi iftest-x"$d/bin/mysql_print_defaults" then print_defaults="$d/bin/mysql_print_defaults" break fi done fi #Hopeit'sinthePATH...butIdoubtit test-z"$print_defaults"&&print_defaults="my_print_defaults" fi
查找默认的配置文件
-rfile如果文件可读,则为真
# #Readdefaultsfilefrom'basedir'.Ifthereisnodefaultsfilethere #checkifit'sintheold(depricated)place(datadir)andreaditfromthere # extra_args="" iftest-r"$basedir/my.cnf" then extra_args="-e$basedir/my.cnf" else iftest-r"$datadir/my.cnf" then extra_args="-e$datadir/my.cnf" fi fi
解析配置文件中的参数
my_print_defaults的用法如下:
my_print_defaults--defaults-file=example.cnfclientmysql
即读取配置文件中,client和mysql部分的参数配置,
具体在本脚本中,是读取mysqld,server,mysql_server,mysql.server四个部分的配置参数。
parse_server_arguments`$print_defaults$extra_argsmysqldservermysql_servermysql.server`
设置pidfile的路径
-zstring判断字符串是否为空
如果--pid-file没有在读取到的配置文件中设置或者脚本刚开始的mysqld_pid_file_path参数没有设置,
则pidfile默认设置在datadir下,以主机名.pid命名。
如果该参数设置了,还需要进一步判断
如果该参数中带有斜杠,则代表给定的值带有路径,可直接使用。
如果该参数中没带路径,则代表给定的值只是pid的文件名,可将其设在datadir下。
# #Setpidfileifnotgiven # iftest-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
服务脚本start选项
首先,切换到$basedir中
其次,判断$basedir/bin中的mysqld_safe是否是可执行文件,如果是,则启动mysqld实例,如果不是,则报错退出。
那么,启动流程又是如何实现的呢?
首先,执行$bindir/mysqld_safe--datadir="$datadir"--pid-file="$mysqld_pid_file_path"$other_args>/dev/null2>&1&命令,启动mysqld实例。
注意到没有,mysqld_safe其实是在basedir中执行的,包括mysql初始化脚本mysql_install_db,也建议在basedir中执行,具体可参考:
分析MariaDB初始化脚本mysql_install_db
然后通过wait_for_pid函数进行判断,具体可见下文对于wait_for_pid函数的分析
判断完毕后,
查看$lockdir目录是否可写,可写的话,则在目录上创建一个文件。
case"$mode"in 'start') #Startdaemon #Safeguard(relativepaths,coredumps..) cd$basedir echo$echo_n"StartingMySQL" iftest-x$bindir/mysqld_safe then #Giveextraargumentstomysqldwiththemy.cnffile.Thisscript #maybeoverwrittenatnextupgrade. $bindir/mysqld_safe--datadir="$datadir"--pid-file="$mysqld_pid_file_path"$other_args>/dev/null2>&1& wait_for_pidcreated"$!""$mysqld_pid_file_path";return_value=$? #MakelockforRedHat/SuSE iftest-w"$lockdir" then touch"$lock_file_path" fi exit$return_value else log_failure_msg"Couldn'tfindMySQLserver($bindir/mysqld_safe)" fi ;;
wait_for_pid函数
在利用mysqld_safe启动mysql实例后,会调用该参数
wait_for_pidcreated"$!""$mysqld_pid_file_path";return_value=$?
其中$!在shell中用于获取最后运行的后台Process的PID,具体在本例中,是mysqld_safe进程的pid。
因为第一个参数是created,所以会执行test-s"$pid_file_path"&&i=''&&break命令。
-sfile如果文件的长度不为零,则为真
该命令的意思是如果pid文件存在,则将变量i设置为空,并退出while循环。
然后执行如下判断,
iftest-z"$i";then log_success_msg return0 else log_failure_msg return1 fi
如果$i为空,则打印成功日志,并退出脚本,很显然,在pid文件存在的情况下,会将变量i设置为空。
再来看看pid文件不存在的情况
首先,会判断$pid是否不为空(即iftest-n"$pid")
如果不为空,则代表在执行完mysqld_safe后,已经捕捉到了该进程的pid。
在这种情况下,进一步通过kill-0"$pid"确认该进程是否存在。
kill-0就是不发送任何信号,但是系统会进行错误检查,所以经常用来检查一个进程是否存在,当进程不存在时,kill-0pid会返回错误
如果该进程存在,则不执行任何操作,直接跳到如下操作
echo$echo_n".$echo_c" i=`expr$i+1` sleep1
将变量i加1,并sleep1s。
然后,继续while循环,之所以这样做,是考虑到mysqld_safe已经执行,但是mysqld实例还在启动过程中,还没创建好pid文件。
一直到$1达到$service_startup_timeout定义的时长。
如果在while循环的过程中,通过kill-0"$pid"判断到进程已经不存在了,
则会再判断一次,如果这次判断的结果依旧是pidfile不存在,且进程不存在,则会执行
log_failure_msg"TheserverquitwithoutupdatingPIDfile($pid_file_path)."
这就是大名鼎鼎的“TheserverquitwithoutupdatingPIDfile”的由来。
wait_for_pid(){ verb="$1"#created|removed pid="$2"#processIDoftheprogramoperatingonthepid-file pid_file_path="$3"#pathtothePIDfile. i=0 avoid_race_condition="bycheckingagain" whiletest$i-ne$service_startup_timeout;do case"$verb"in 'created') #waitforaPID-filetopopintoexistence. test-s"$pid_file_path"&&i=''&&break ;; 'removed') #waitforthisPID-filetodisappear test!-s"$pid_file_path"&&i=''&&break ;; *) echo"wait_for_pid()usage:wait_for_pidcreated|removedpidpid_file_path" exit1 ;; esac #ifserverisn'trunning,thenpid-filewillneverbeupdated iftest-n"$pid";then ifkill-0"$pid"2>/dev/null;then :#theserverstillruns else #Theservermayhaveexitedbetweenthelastpid-filecheckandnow. iftest-n"$avoid_race_condition";then avoid_race_condition="" continue#Checkagain. fi #there'snothingthatwillaffectthefile. log_failure_msg"TheserverquitwithoutupdatingPIDfile($pid_file_path)." return1#notwaitinganymore. fi fi echo$echo_n".$echo_c" i=`expr$i+1` sleep1 done iftest-z"$i";then log_success_msg return0 else log_failure_msg return1 fi }
服务脚本stop选项
首先,判断pid文件的长度是否不为零。
-sfile如果文件的长度不为零,则为真
此时,会通过pid文件获取mysqld进程的pid,注意,不是mysqld_safe进程的pid
然后,判断mysqld进程是否在正常运行,
如果是,则通过kill$mysqld_pid的方式来关闭mysqld进程
杀死进程最安全的方法是单纯使用kill命令,不加修饰符,不带标志。
标准的kill命令通常会终止有问题的进程,并把进程的资源释放给系统。然而,如果进程启动了子进程,只杀死父进程,子进程仍在运行,因此仍消耗资源。为了防止这些所谓的“僵尸进程”,应确保在杀死父进程之前,先杀死其所有的子进程。
然后,调用wait_for_pid函数进行判断,其实,wait_for_pid函数中设置avoid_race_condition变量的目的是为了stop选项,确实有可能出现,mysqld是在检查pidfile之后,检查进程是否存活之前退出的。
如果mysqld进程没有正常运行,在会打印“MySQLserverprocess#$mysqld_pidisnotrunning!”信息,并删除pid文件。
如果在执行stop的时候,判断pid文件的长度为0,则会打印"MySQLserverPIDfilecouldnotbefound!"信息。
所以,在pid文件不存在的情况下,通过服务脚本执行stop选项并不会关闭mysqld进程,这个时候,就可通过kill$mysqld_pid的方式来关闭mysqld进程。
'stop') #Stopdaemon.Weuseasignalheretoavoidhavingtoknowthe #rootpassword. iftest-s"$mysqld_pid_file_path" then mysqld_pid=`cat"$mysqld_pid_file_path"` if(kill-0$mysqld_pid2>/dev/null) then echo$echo_n"ShuttingdownMySQL" kill$mysqld_pid #mysqldshouldremovethepidfilewhenitexits,sowaitforit. wait_for_pidremoved"$mysqld_pid""$mysqld_pid_file_path";return_value=$? else log_failure_msg"MySQLserverprocess#$mysqld_pidisnotrunning!" rm"$mysqld_pid_file_path" fi #DeletelockforRedHat/SuSE iftest-f"$lock_file_path" then rm-f"$lock_file_path" fi exit$return_value else log_failure_msg"MySQLserverPIDfilecouldnotbefound!" fi ;;
服务脚本restart选项
首先,先执行stop操作,如果stop操作成功的话,则继续执行start操作。
如果stop操作失败的话,则会输出"Failedtostoprunningserver,sorefusingtotrytostart."信息,并退出脚本。
'restart') #Stoptheserviceandregardlessofwhetheritwas #runningornot,startitagain. if$0stop$other_args;then $0start$other_args else log_failure_msg"Failedtostoprunningserver,sorefusingtotrytostart." exit1 fi ;;
服务脚本reload选项
首先,判断pid文件的长度是否为0,如果不为0,则将该文件中的值设置为mysqld_pid变量的值。
然后对该进程执行kill-HUP操作。
kill-HUPpid
pid是进程标识。如果想要更改配置而不需停止并重新启动服务,请使用该命令。在对配置文件作必要的更改后,发出该命令以动态更新服务配置。
根据约定,当您发送一个挂起信号(信号1或HUP)时,大多数服务器进程(所有常用的进程)都会进行复位操作并重新加载它们的配置文件。
如果pid文件的长度为0,则输出"MySQLPIDfilecouldnotbefound!"。
'reload'|'force-reload') iftest-s"$mysqld_pid_file_path";then readmysqld_pid<"$mysqld_pid_file_path" kill-HUP$mysqld_pid&&log_success_msg"ReloadingserviceMySQL" touch"$mysqld_pid_file_path" else log_failure_msg"MySQLPIDfilecouldnotbefound!" exit1 fi ;;
服务脚本status选项
首先,判断pid文件长度是否为0,如果不是,则读取该文件中的值,并判断pid对应的进程是否运行正常,
如果运行正常,则输出"MySQLrunning"
如果不正常,则输出"MySQLisnotrunning,butPIDfileexists"
如果pid文件的长度为0,则试图通过mysqld的启动命令来获取其pid,
这个时候,可能存在一个mysqld程序启动了多个实例,这会导致pid_count=`echo$mysqld_pid|wc-w`大于1。
这个时候,会输出"MultipleMySQLrunningbutPIDfilecouldnotbefound"信息,并退出脚本。
如果mysqld_pid为空,则会继续判断"$lock_file_path"是否存在,如果存在,
则会输出"MySQLisnotrunning,butlockfile($lock_file_path)exists"信息。
如果"$lock_file_path"不存在,则会输出"MySQLisnotrunning"信息。
如果mysqld_pid等于1,则会输出"MySQLisrunningbutPIDfilecouldnotbefound"信息。
'status') #First,checktoseeifpidfileexists iftest-s"$mysqld_pid_file_path";then readmysqld_pid<"$mysqld_pid_file_path" ifkill-0$mysqld_pid2>/dev/null;then log_success_msg"MySQLrunning($mysqld_pid)" exit0 else log_failure_msg"MySQLisnotrunning,butPIDfileexists" exit1 fi else #Trytofindappropriatemysqldprocess mysqld_pid=`pidof$libexecdir/mysqld` #testifmultiplepidsexist pid_count=`echo$mysqld_pid|wc-w` iftest$pid_count-gt1;then log_failure_msg"MultipleMySQLrunningbutPIDfilecouldnotbefound($mysqld_pid)" exit5 eliftest-z$mysqld_pid;then iftest-f"$lock_file_path";then log_failure_msg"MySQLisnotrunning,butlockfile($lock_file_path)exists" exit2 fi log_failure_msg"MySQLisnotrunning" exit3 else log_failure_msg"MySQLisrunningbutPIDfilecouldnotbefound" exit4 fi fi ;;
服务脚本其它选项
如果脚本的第一个参数不是上述几个选项,则会输出Usage信息。
*) #usage basename=`basename"$0"` echo"Usage:$basename{start|stop|restart|reload|force-reload|status}[MySQLserveroptions]" exit1 ;;
至此,mysql的服务脚本分析完毕~
总结
在通过服务脚本启动mysql的过程中,报“TheserverquitwithoutupdatingPIDfile”错误,有两个条件
首先,pid文件不存在
其次,通过kill-0$pid检查到进程并不存在
这个时候,只能通过mysql数据库的错误日志来定位。
服务脚本如果不做任何调整的话,默认的basedir是/usr/local/mysql,datadir是/usr/local/mysql/data
如果自己的mysql服务均不是默认路径,
则需要在该脚本中显式设置
经测试,需设置如下几处:
1.设置basedir和添加conf变量
其中,conf指的是mysqld的配置文件,建议配置文件中显式指定basedir和datadir的值。
在这里,datadir可不设置,因为datadir可通过配置文件来获取。
但是basedir必须要指定,因为要首先根据basedir来判断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" iftest-r"$basedir/my.cnf" then extra_args="-e$basedir/my.cnf" else iftest-r"$datadir/my.cnf" then extra_args="-e$datadir/my.cnf" fi fi extra_args="-c$conf"
3.修改285行mysqld_safe的启动参数
将
$bindir/mysqld_safe--datadir="$datadir"--pid-file="$mysqld_pid_file_path"$other_args>/dev/null2>&1&
修改为,
$bindir/mysqld_safe--defaults-file="$conf"--datadir="$datadir"--pid-file="$mysqld_pid_file_path"$other_args>/dev/null2>&1&
主要是添加了--defaults-file选项
以上所述是小编给大家介绍的深度解析MySQL启动时报“TheserverquitwithoutupdatingPIDfile”错误的原因,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!