MySQL启动脚本改装,支持多配置文件
2012-11-26 11:06
656 查看
相信很多童鞋都不止维护一个MySQL实例,MySQL自带的启动脚本不太好使,所以改装了一下,把指定的配置文件名传入即可
使用示例:
?
只要把mysql的配置文件名作为参数传入即可,配置文件名的后辍支持.cnf和.cfg
修改MySQL的安装目录所在位置,如果安装在/usr/local/mysql的话,不用修改
mysqld文件第46行
?
在第48行加入默认配置文件
?
查找“mode=$1”
在下一行加入
?
在[$#-ge1]&&shift的上一行加入,这个不能少了,否则会把传进来的第二个参数作为mysqld自身参数,会出错。
?
在任何位置加入下面的函数,注意:不要在函数内加。
该函数作用是查找mysql的配置文件位置,可以根据自己的需求加入
?
查找“parse_server_arguments"的函数调用,在之前加入
?
最后,在"start"分支里修改
?
修改后完整mysqld脚本代码(红色的是被改过
使用示例:
修改MySQL的安装目录所在位置,如果安装在/usr/local/mysql的话,不用修改
mysqld文件第46行
在下一行加入
该函数作用是查找mysql的配置文件位置,可以根据自己的需求加入
#!/bin/sh #CopyrightAbandoned1996TCXDataKonsultAB&MontyProgramKB&DetronHB #ThisfileispublicdomainandcomeswithNOWARRANTYofanykind #################### ##Modifybyskyoo61@gmail.com ##2012-03-26 #################### #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=<path-to-mysql-installation-directory> #-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= config_file='/etc/my.cnf' #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=/data/mysql 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 server=$2#defaultmy,configfilenamenotsuffix,defaultsuffix".cnf" [$#-ge2]&&shift&&shift [$#-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/^[^=]*=//'`;; --port=*)port=`echo"$arg"|sed-e's/^[^=]*=//'`;; --log-error=*)log_error_path=`echo"$arg"|sed-e's/^[^=]*=//'`;; --server-id=*)server_id=`echo"$arg"|sed-e's/^[^=]*=//'`;; --socket=*)socket=`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 } get_cnf_file(){ cfg_name=$1 if[-n$cfg_name];then cfg_file="/etc/$cfg_name.cnf" cfg_file2="/etc/$cfg_name.cfg" cfg_file3="$basedir/$cfg_name.cnf" cfg_file4="$basedir/$cfg_name.cfg" cfg_file5="$basedir/etc/$cfg_name.cnf" cfg_file6="$basedir/etc/$cfg_name.cfg" if[-f$cfg_file];then config_file=$cfg_file elif[-f$cfg_file2];then config_file=$cfg_file2 elif[-f$cfg_file3];then config_file=$cfg_file3 elif[-f$cfg_file4];then config_file=$cfg_file4 elif[-f$cfg_file5];then config_file=$cfg_file5 elif[-f$cfg_file6];then config_file=$cfg_file6 fi 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 if[-n$server] then get_cnf_file$server extra_args="-c$config_file" lock_file_path="$lockdir/$server" 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. if[-n$server];then $bindir/mysqld_safe--defaults-file="$config_file"--pid-file="$mysqld_pid_file_path"$other_args>/dev/null2>&1& else $bindir/mysqld_safe--datadir="$datadir"--pid-file="$mysqld_pid_file_path"$other_args>/dev/null2>&1& fi 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` iftest-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}[configname.\"my.cnf\"example\"my\"(defaultmy)][MySQLserveroptions]" exit1 ;; esac exit0
转自:/article/5536675.html
相关文章推荐
- MySQL启动脚本改装,支持多配置文件
- 改进uwsgi启动脚本,使其支持多个独立配置文件
- mysql启动脚本——指定数据目录并指定配置文件
- linux配置开机启动脚本的文件
- 嵌入式Linux启动配置文件及脚本分…
- 嵌入式Linux启动配置文件及脚本
- Mycat使用篇: Mycat启动脚本及脚本配置文件详解
- 操作3 mongodb和mysql 开启慢查询日志 ,以及mongodb从配置文件启动
- MySQL指定mysqld启动时所加载的配置文件
- MySQL读取配置文件的顺序、启动方式、启动原理
- Linux mysql安装无法启动 /etc下无mysql配置文件,mysql 无法启动,/var/run/mysqld/mysqld.sock找不到
- 启动mysql配置文件的启动方式
- MySQL中配置文件my.cnf因权限问题导致无法启动的解决方法
- nginx配置文件和启动脚本报错
- mysql 在启动时配置文件的查找方式
- MySql绿色版安装步骤和方法,以及配置文件修改,Mysql服务器启动
- Linux下Nginx+PHP+Mysql安装,Nginx配置文件支持PHP
- SUSE开机启动脚本配置文件
- 脚本文件启动tomcat(替换配置文件)
- beagelboard-xm上dvsdk移植4:sd卡启动脚本以及NFS文件系统的制作和配置