MySQL启动脚本(改编官方启动脚本)

7 篇文章 0 订阅

MySQL二进制及编译安装启动脚本

[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"

  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.
       echo "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
    echo "success"
    return 0
  else
    echo "faile"
    return 1
  fi
}


# 启动选择

mode=$1    # start or stop

[ $# -ge 1 ] && shift

case  "$mode" in
	'start')
	# 启动MySQL
	cd $basedir

	echo "start MySQL"
	if test -x $bindir/mysqld_safe
	then
		$bindir/mysqld_safe --defaults-file="$cnf_file" --datadir="$datadir" >/dev/null &
		wait_for_pid created "$!" "$pid_file"; return_value=$?
	else
		echo "Couldn't find MySQL server ($bindir/mysqld_safe)"
	fi
	;;

	'stop')
	# 关闭MySQL
	mysqld_pid=`cat "$pid_file"`
	if (kill -0 $mysqld_pid 2>/dev/null)
	then
		echo "Shutting down MySQL"
		kill $mysqld_pid
	else
		echo "MySQL server process #$mysqld_pid is not running!"	
	fi
	;;

	'status')
	if test -s "$pid_file";then
		read mysqld_pid < "$pid_file"
		if kill -0 "$mysqld_pid" 2> /dev/null;then
			echo "MySQL running ($mysqld_pid)"
			exit 0
		else
		    echo "MySQL is not running, but PID file exists"
		    exit 1
		 fi   
	else
		echo "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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值