利用Shell开发MySQL的启动脚本
- MySQL实例部署情况
01:MySQL程序安装目录:/data/apps/mysql
02:MySQL实例3306的配置文件为:/data/mysql/3306/my.cnf
03:MySQL实例3306的PID文件为:/data/mysql/3306/mysql.pid
04:MySQL实例3306的socket文件为:/data/mysql/3306/mysql.sock
- 脚本内容如下
#!/bin/bash # # Load OS variables source /etc/bashrc source /etc/prefile # Define variables RETVAL=0 Port=3306 User=root Pass=chenliang Pid=/data/mysql/3306/mysql.pid Sock=/data/mysql/3306/mysql.sock My=/data/mysql/3306/my.cnf Path=/data/apps/mysql/bin # Determine the user to execute if [ $UID -ne $RETVAL ];then echo "Must be root to run scripts" exit 1 fi # Load the local functions [ -f /etc/init.d/functions ] && source /etc/init.d/functions # Define functions start(){ if [ ! -f "$Pid" ];then $Path/mysqld_safe --defaults-file=$My >/dev/null 2>&1 & RETVAL=$? if [ $RETVAL -eq 0 ];then action "Start MySQL [3306]" /bin/true else action "Start MySQL [3306]" /bin/false fi else echo "MySQL 3306 is running" exit 1 fi return $RETVAL } stop(){ if [ -f "$Pid" ];then $Path/mysqladmin -u$User -p$Pass -S $Sock shutdown >/dev/null 2>&1 RETVAL=$? if [ $RETVAL -eq 0 ];then action "Stop MySQL[3306]" /bin/true else action "Stop MySQL[3306]" /bin/false fi else echo "MySQL [3306] is not running" exit 1 fi return $RETVAL } status(){ if [ -f "$Pid" ];then echo "MySQL [3306] is running" else echo "MySQL [3306] is not running" fi return $RETVAL } # Case call functions case "$1" in start) start RETVAL=$? ;; stop) stop RETVAL=$? ;; restart) stop sleep 5 start RETVAL=$? ;; status) status RETVAL=$? ;; *) echo "USAGE:$0{start|stop|restart|status}" exit 1 esac # Scripts return values exit $RETVAL