作者:娜一刻冻杰幸福 | 来源:互联网 | 2014-05-28 16:53
1,启动脚本保存在/etc/init.d/mongodb#!/bin/sh#start/stopscriptformongodb###BEGININITINFO#Provides:mongod#Required-Start:\$remote_fs\$syslog#Required-Stop:\$remote_fs\$s
1,启动脚本
保存在 /etc/init.d/
mongodb
#! /bin/sh
# start / stop script for mongodb
### BEGIN INIT INFO
# Provides: mongod
# Required-Start: \$remote_fs \$syslog
# Required-Stop: \$remote_fs \$syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start mongod at boot time
# Description: Enable service provided by mongod.
### END INIT INFO
# Source function library.
#. /lib/lsb/init-functions
retval=0
pidfile=/var/run/mongodb.pid
exec="/bin/mongod"
prog="mongod"
cOnfig="/etc/mongodb/mongodb.conf"
lockfile="/var/lock/mongod"
[ -e $config ] && . $config
start() {
if [ ! -x $exec ]
then
echo $exec not found
exit 5
fi
echo "Starting mongoDB daemon"
echo $prog
start-stop-daemon --start --pidfile $pidfile -m -c $MONGO_USER \
--exec $exec -- $MONGO_OPTS run > /dev/null 2>&1 &
retval=$?
if [ $retval -eq 0 ]
then
echo 0
else
echo 1
fi
return $retval
}
stop() {
echo "Stopping mongoDB daemon"
echo $prog
start-stop-daemon --stop --pidfile $pidfile --retry 10 \
--exec $exec
retval=$?
if [ $retval -eq 0 ] && rm -f $lockfile
then
echo 0
else
echo 1
fi
rm -f $pidfile
return $retval
}
restart() {
stop
start
}
reload() {
restart
}
# See how we were called.
case "$1" in
start)
$1
;;
stop)
$1
;;
restart)
$1
;;
reload)
$1
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 2
esac
exit $?
2,下载自己需要的版本,我这用的是mongodb-linux-x86_64-0.9.2.tgz
# cd /tmp/
# wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-0.9.2.tgz
# tar -xzf mongodb-linux-x86_64-0.9.2.tgz
3,创建mongodb目录,和数据库目录。
# mv mongodb-linux-x86_64-0.9.2 /opt/mongodb
# mkdir -p /data/mongodb
4,增加mongodb用户
# useradd mongod -s /bin/false
# chown -R mongod:mongod /data/mongodb
5,编写配置文件
# mkdir /etc/mongodb
# cat << EOF > /etc/mongodb/mongodb.conf
MONGO_USER="mongod"
MONGO_OPTS="--dbpath /data/mongodb/"
EOF