程序员

MongoDB 启停脚本(绿色安装)

2017-06-19  本文已影响208人  非典型程序员

引言

当我们使用官方提供的编译好的绿色版 MongoDB 时,官方并没有提供用于启动、停止实例的管理脚本。因此,本文提供一个基于 Red Hat 操作系统的 MongoDB 启停管理脚本。此外,该脚本略作改动还能作为将 MongoDB 注册成系统服务的 init 脚本。

使用方法

# 当然目录结构
[mongo@Knight mongodb]$ ls
data  dump  logs  mongod-server.sh  mongod.conf  tmp  var
#查看实例状态
[mongo@Knight mongodb]$ ./mongod-server.sh status
Stoped [103561]
#启动实例
[mongo@Knight mongodb]$ ./mongod-server.sh  start
about to fork child process, waiting until server is ready for connections.
forked process: 18012
child process started successfully, parent exiting
Started [18012]
#关闭实例
[mongo@Knight mongodb]$ ./mongod-server.sh  stop
killing process with pid: 18012
Stopped [[18012]]

此脚本默认检查当前目录是否存在 mongod.conf 配置文件,如果用有,则使用该配置启动实例,等价于:

[mongo@Knight mongodb]$ mongod -f mongod.conf

如果当前目录下没有文件,则等价于:

[mongo@Knight mongodb]$ mongod 

此外该脚本还检查了日志目录文件,数据库目录以及 PID文件是否具有相应的操作权限。

脚本源码

#!/bin/bash

source /etc/profile
source ~/.bash_profile

# MongoDB 需要将 LC_ALL 设置为 C
export LC_ALL=C

# 判断是否开启 debug 模式。如果开启了 debug 模式,则通过 set -x 设置显示执行的命令
[[ -n "$DEBUG" ]] && set -x

#ANSI Colors
echoRed() { echo $'\e[0;31m'"$1"$'\e[0m'; }
echoGreen() { echo $'\e[0;32m'"$1"$'\e[0m'; }
echoYellow() { echo $'\e[0;33m'"$1"$'\e[0m'; }

mongodexe="/DATA/mongodb-3.4.5/rhel-3.4.5/bin/mongod"

workdir=$(pwd)
cd $(dirname $0)
mongo_conf=$(pwd)/mongod.conf
cd $workdir

[[ -f "$mongo_conf" ]] && source $mongo_conf

[[ -n "$dbpath" ]] || dbpath=/data/db
[[ -n "$logpath" ]] || logpath=/var/logs/mongo.log
[[ -n "$pidfilepath" ]] || pidfilepath=/var/run/mongo.pid

# 检查权限
checkPermission() {
  # 只有 test 命令可以和多种系统运算符一起使用
  test -d $dbpath -a -r $dbpath -a -w $dbpath  || { echoRed "[$dbpath] not found or permission denied"; return 1; }
  test -f $logpath -a -r $logpath -a -w $logpath  || { echoRed "[$logpath] not found or permission denied"; return 1; }
  test -f "$pidfilepath" -a -r $pidfilepath -a -w $pidfilepath || { echoRed "[$pidfilepath] not found or permission denied"; return 1; }
  return 0
}

# 并获取 PID
pid=$(cat "$pidfilepath")


# 检查进程是否存在
isRunning() {
  ps -p "$1" &> /dev/null
}


status() {
  # 判断进程是否存在  
  isRunning "$pid" || { echoRed "Stoped [$pid]"; return 0; }
  echoGreen "Running [$pid]"
  return 0
}

do_start() {
  if [[ -f "$mongo_conf" ]]; then
    /bin/sh -c "$mongodexe -f $mongo_conf"
  else
    /bin/sh -c "$mongodexe"
  fi

  pid=$(cat "$pidfilepath")

  
  # 判断进程是否存在  
  isRunning "$pid" || { echoRed "Failed to start"; return 1; }
  echoGreen "Started [$pid]"
  return 0
}

start() {
  # 判断进程是否存在  
  isRunning "$pid" && { echoYellow " Running [$pid]"; return 1; }
  # 检查权限
  checkPermission || return 1
  # 启动
  do_start
}

do_stop() {
 if [[ -f "$mongo_conf" ]]; then
   /bin/sh -c "$mongodexe -f $mongo_conf --shutdown"
 else
   /bin/sh -c "$mongodexe --shutdown"
 fi
   
 # 判断进程是否存在  
 isRunning "$pid" && { echoRed "Unable to kill process $pid"; return 1; }
 echoGreen "Stopped [[$pid]]"
 return 0
}

stop() {
 # 判断进程是否存在  
 isRunning "$pid" || { echoYellow "Not running (process ${pid} not found)"; return 1;}
 do_stop
}

# Call the appropriate action function
case "$1" in
start)
  start ; exit $?;;
stop)
  stop ; exit $?;;
status)
  status ; exit $?;;
restart)
  stop; start; exit $?;;
*)
  echoRed "Usage: $0 {start|stop|status|restart}"; exit 1;
esac

exit 0

上一篇下一篇

猜你喜欢

热点阅读