設(shè)置開(kāi)機(jī)啟動(dòng)需要如下幾個(gè)步驟:
- 編寫配置腳本 [** 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,打開(kāi)后臺(tái)運(yùn)行選項(xiàng)
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
- 修改文件執(zhí)行權(quán)限
chmod +x /etc/init.d/mongodb
- 設(shè)置開(kāi)機(jī)啟動(dòng)
# 嘗試啟動(dòng)或停止 mongodb
service mongodb start
service mongodb stop
# 開(kāi)啟服務(wù)自啟動(dòng)
chkconfig mongodb on
5.異常處理
A. 執(zhí)行 [ service mongodb start ] 提示服務(wù)不支持 chkconfig诗良,在開(kāi)機(jī)腳本前添加如下內(nèi)容:
#chkconfig: 2345 80 90
#description:auto_run
B. 如果在Windows下編輯的開(kāi)機(jī)腳本帕膜,由于Windows中的換行符為CRLF, 而Unix(或Linux)換行符為L(zhǎng)F,會(huì)導(dǎo)致開(kāi)機(jī)腳本執(zhí)行報(bào)錯(cuò),把腳本通過(guò)notepad++轉(zhuǎn)化為Unix格式。