(1) 先在/etc/rc.d/init.d下用vi 新建文件 mongod,内容如下:
#!/bin/bash
#
#chkconfig: 2345 80 90
#description: mongodb
start() {
/opt/mongo/bin/mongod --config /opt/mongo/bin/mongodb.conf
}
stop() {
/opt/mongo/bin/mongod --config /opt/mongo/bin/mongodb.conf --shutdown
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo
$"Usage: $0 {start|stop|restart}"
exit 1
esac
(2) 修改/opt/mongo/bin/mongodb.conf,配置文件的内容如下
dbpath=/opt/mongo/data/db #最好使用data/db这个目录,我之前用了其他的不行,不知道为什么
logpath=/opt/mongo/log/mongodb.log
pidfilepath=/opt/mongo/db.pid
directoryperdb=true
logappend=true
bind_ip= localhost
port=27017
oplogSize=1000
fork=true
noprealloc=true
nojournal=true
smallfiles=true
(3) 增加服务并开机启动
chmod +x /etc/rc.d/init.d/mongod
chkconfig --add mongod
chkconfig --level 345 mongod on
chkconfig --list mongod
service mongod start
执行该脚本后,就可以开始start|stop|restart|list你的服务了。
以后关机再启动就会自动启动mongo了,如果在同一台机需要启动多个mongod照此方法即可。