Go to the /etc/init.d/ directory,
# cd /etc/init.d/
create and edit the mongodb file,
# vim mongodb
add the following code
#!/bin/sh
# chkconfig: 2345 10 90
# description: myservice ....
#
# ### BEGIN INIT INFO
# Short-Description: mongodb
# Description: mongo db server
### END INIT INFO
#注意这里的路径设置为你的mongod的位置
PROGRAM=/usr/bin/mongod
MONGOPID=`ps -ef | grep 'mongod' | grep -v grep | awk '{print $2}'`
test -x $PROGRAM || exit 0
case "$1" in
start)
ulimit -n 3000
echo "Starting MongoDB server"
#注意这里的路径设置为你的mongodb的配置文件的位置
$PROGRAM --fork --quiet -f /etc/mongod.conf
echo "Started MongoDB server"
;;
stop)
echo "Stopping MongoDB server"
if [ ! -z "$MONGOPID" ]; then
kill -15 $MONGOPID
fi
echo "Stopped MongoDB server"
;;
status)
;;
*)
echo "Usage: mongodb {start|stop|status}"
exit 1
esac
exit 0
After saving, just execute the following commands:
# chmod +x /etc/init.d/mongodb
# chkconfig --add mongodb
# chkconfig mongodb on
Then you can use the service to start mongodb.
#service mongodb start
Reference:
https://www.jianshu.com/p/9882745767fd
https://www.cnblogs.com/zydev/p/5771189.html