A few days ago, a development colleague found me saying that the mongodb of his test environment often hung up and asked me to write a monitor or resurrection script. I think it is strange that the test environment is not a load, often hanging off must have unconventional reasons.
Ran past looked at the log, found that there is a stop record, I was puzzled, no one to operate his own stop. This is obviously not dead, so went to history to see the next colleague's start command:
Copy Code code as follows:
/usr/local/mongodb/bin/mongod--dbpath=/usr/local/mongodb/data/--logpath=/data/mongodb.log--logappend &
I see! Because he did not start with nohup, so as long as his terminal offline or shut down, MongoDB will automatically quit! The solution is simple, starting with the following:
Copy Code code as follows:
Nohup/usr/local/mongodb/bin/mongod--dbpath=/usr/local/mongodb/data/--logpath=/data/mongodb.log--logappend >/ Dev/null 2>&1 &
It's hard to knock orders, so it's much more comfortable to find a MongoDB service script from the Internet:
#!/bin/sh # #mongod-startup script for Mongod # chkconfig:-description:mongodb database. # processname:mongod # Source function library. /etc/rc.d/init.d/functions # Things from mongod.conf to there by Mongod reading it # options options= '--dbpath=/home/dat a/mongodb/--logpath=/home/data/mongodb/mongodb.log--logappend & "#mongod mongod="/usr/local/mongodb/bin/
Mongod "Lockfile=/var/lock/subsys/mongod start () {echo-n $" starting Mongod: Daemon $mongod $OPTIONS retval=$? echo [$RETVAL-eq 0] && Touch $lockfile} stop () {echo-n $ "stopping Mongod: Killproc $mongod-quit RET
Val=$? echo [$RETVAL-eq 0] && rm-f $lockfile} restart () {Stop start} ulimit-n 12000 retval=0 case "
"in Start" start;;
stop) stop;;
restart|reload|force-reload) restart;; Condrestart) [f $lockfile] && Restart | |
:
;;
Status $mongod retval=$?
;; *) echo "Usage: $ {start|stop|status|resTart|reload|force-reload|condrestart} "retval=1 Esac exit $RETVAL
Save the code to/ETC/INIT.D/MONGODB, and then use chmod +x/etc/init.d/mongodb to add execution permissions.
Now, you can use the Service command to control MongoDB:
Copy Code code as follows:
Service MongoDB Start|stop|restart
#或
/etc/init.d/mongodb Start|stop|restart
Very simple, posted to the blog to record, in case of a rainy moment.