1,启动脚本
保存在 /etc/init.d/mongodb
- Helder RibeiroFreedom on all levels.
- Installing MongoDB on Gentoo
- So I had some trouble running the good old “sudo emerge mongodb -va” due to some issues. After installing it by hand I found this little portage overlay that handles things better, but it has an outdated version of MongoDB, so I’ll show you how to get the latest and greatest. It’s all very basic, but since I’m no sysadmin, I still had to wrestle with things a bit to get it working, so I hope to relieve others of the same effort.
- Since the problem was with one of the dependencies (spidermonkey), you’ll need the static version, that is, the one that comes with all the dependencies compiled in. Below is the one I used, but make sure to find the latest tgz for your platform here (choose the “legacy-static”).
- wget http://downloads.mongodb.org/linux/mongodb-linux-i686-static-1.4.2.tgz
- After that, basically all you have to do is follow the instructions on this guide. It’s in German, but you can get what you have to do from the commands it tells you to run (the automatic translation is also not so bad).
- The only thing you’re not supposed to do is copy the script in the section “Start-Stop Script”. It is meant for Debian distributions and won’t work on Gentoo because of some missing commands. So instead, copy the script below into /etc/init.d/mongodb, give it +x permission and follow the rest of the guide as usual. Let me know if you run into any troubles!
- #! /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