[root@localhost ~]# more /etc/init.d/mysqld #!/bin/bash# MySQL启动脚本# by lumia98@vip.qq.com#安装目录
basedir='/opt/app/mysql-3306'#MySQL的bin目录
bindir='/opt/app/mysql-3306/bin'#MySQL数据存放目录
datadir='/databases/mysql-3306/data'#MySQL的配置文件
cnf_file='/opt/app/mysql-3306/etc/my.cnf'#MySQL的pid文件
pid_file='/opt/app/mysql-3306/socket/mysql.pid'# 环境变量
PATH="/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin"export PATH
service_startup_timeout=900
# 创建id
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"whiletest$i -ne $service_startup_timeout;docase"$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 disappeartest! -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 updatediftest -n "$pid";thenifkill -0 "$pid" 2>/dev/null;then:# the server still runselse# The server may have exited between the last pid-file check and now.iftest -n "$avoid_race_condition";then
avoid_race_condition=""continue# Check again.fi# there's nothing that will affect the file.echo"The server quit without updating PID file ($pid_file_path)."return 1 # not waiting any more.fifiecho$echo_n".$echo_c"
i=`expr $i + 1`sleep 1
doneiftest -z "$i";thenecho"success"return 0
elseecho"faile"return 1
fi}# 启动选择
mode=$1# start or stop[ $# -ge 1 ] && shiftcase"$mode"in'start')# 启动MySQLcd$basedirecho"start MySQL"iftest -x $bindir/mysqld_safe
then$bindir/mysqld_safe --defaults-file="$cnf_file" --datadir="$datadir">/dev/null &
wait_for_pid created "$!""$pid_file"; return_value=$?elseecho"Couldn't find MySQL server ($bindir/mysqld_safe)"fi;;'stop')# 关闭MySQL
mysqld_pid=`cat"$pid_file"`if(kill -0 $mysqld_pid 2>/dev/null)thenecho"Shutting down MySQL"kill$mysqld_pidelseecho"MySQL server process #$mysqld_pid is not running!"fi;;'status')iftest -s "$pid_file";thenread mysqld_pid <"$pid_file"ifkill -0 "$mysqld_pid" 2> /dev/null;thenecho"MySQL running ($mysqld_pid)"exit 0
elseecho"MySQL is not running, but PID file exists"exit 1
fielseecho"MySQL is not running"exit 2
fi;;
*)# usage
basename=`basename"$0"`echo"Usage: $basename {start|stop|status} [ MySQL server options ]"exit 1
;;
esac
exit 0