设置开机启动需要如下几个步骤:
- 编写配置脚本 [** vim /etc/init.d/mongodb** ]
#!/bin/sh
#
# Simple mongodb init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
#chkconfig: 2345 80 90
#description:auto_run
MONGODBPORT=27000
EXEC=/usr/local/bin/mongodb/bin/mongod
PIDFILE=/usr/local/bin/mongodb/mongodb.pid
CONF="/usr/local/bin/mongodb/mongodb.conf"
OPTIONS="--config $CONF"
case "$1" in
start)
echo "Starting mongodb server..."
$EXEC $OPTIONS
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $MONGODBPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for mongodb to shutdown ..."
sleep 1
done
echo "mongodb stopped"
fi
;;
*)
echo "Please use start or stop as first argument"
;;
esac
- 修改mongodb.conf,打开后台运行选项
storage:
dbPath: /usr/local/bin/mongodb/data/db
journal:
enabled: true
net:
bindIp: 127.0.0.1
port: 27017
processManagement:
fork: true
pidFilePath: /usr/local/bin/mongodb/mongodb.pid
systemLog:
destination: file
path: "/usr/local/bin/mongodb/mongodb.log"
logAppend: true
- 修改文件执行权限
chmod +x /etc/init.d/mongodb
- 设置开机启动
# 尝试启动或停止 mongodb
service mongodb start
service mongodb stop
# 开启服务自启动
chkconfig mongodb on
5.异常处理
A. 执行 [ service mongodb start ] 提示服务不支持 chkconfig,在开机脚本前添加如下内容:
#chkconfig: 2345 80 90
#description:auto_run
B. 如果在Windows下编辑的开机脚本,由于Windows中的换行符为CRLF, 而Unix(或Linux)换行符为LF,会导致开机脚本执行报错,把脚本通过notepad++转化为Unix格式。
网友评论