Sunday, April 29, 2012

Setup MongoDB on Fedora 14


MongoDB is an open source document-oriented NoSQL database system. It is used in many apps especially when it is a startup what should be cloud ready.

VMware's Cloud Found provides MongoDB out of the box and in time of writing this their Micro Edition proposes version 1.8.

To start develop with Cloud Foundry we need (it's recommended) to have the same versions of the services as on the cloud but locally.

Unfortunately Fedora 14 can propose only MongoDB 1.6, so we have to do the installation for the version 1.8 manually and there is a good description of steps we need - Use MongoDB to Store Application Data on Fedora 14 - but file locations will differ.

First ensure our system is up to date: yum update.

MongoDB will be installed in /opt/mongodb folder and data with logs will be in /var/lib/mongodb:
mkdir -p /var/lib/mongodb
mkdir data1
mkdir logs
touch /var/lib/mongodb/logs/data1.log

Create user mongodb with its home dir in our server folder:
useradd -M -r --home-dir /opt/mongodb mongodb
And make it able to access our data folder:
chown -R mongodb:mongodb /var/lib/mongodb

Download and unarchive MongoDB:
cd /opt/
wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-1.8.0.tgz
tar xvf mongodb-linux-x86_64-1.8.0.tgz
ln -s ./mongodb-linux-x86_64-1.8.0 ./mongodb

We linking actual directory to /opt/mongodb this will let us to switch between versions of MongoDB in case of a need in future.

Basic configuration of MongoDB is very simple. Need point to a listening address and set data and logs folders. Our configuration will be in /opt/mongodb/config/mongod.conf file:
# Configuration Options for MongoDB
#
# For More Information, Consider:
# - Configuration Parameters: http://www.mongodb.org/display/DOCS/Command+Line+Parameters
# - File Based Configuration: http://www.mongodb.org/display/DOCS/File+Based+Configuration

dbpath = /var/lib/mongodb/data1
logpath = /var/lib/mongodb/logs/data1.log
logappend = true

bind_ip = 127.0.0.1
port = 27017
fork = true

auth = true
# noauth = true

Create service control script /etc/rc.d/init.d/mongodb:
#! /bin/sh
#
# mongodb – this script starts and stops the mongodb daemon
#
# chkconfig: - 85 15
# description: MongoDB is a non-relational database storage system.
# processname: mongodb
# config: /optg/mongodb/config/mongod.conf
# pidfile: /opt/mongodb/mongo.pid

# Source function library
. /etc/rc.d/init.d/functions

user=mongodb
prog="mongod"
mongod="/opt/mongodb/bin/$prog --fork --config /opt/mongodb/config/mongod.conf"

RETVAL=0

start() {
  echo -n "Starting MongoDB... "
  daemon --user "$user" $mongod
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && touch /opt/mongodb/$prog.lock
}

stop() {
  echo -n "Stopping MongoDB"
  killproc $prog
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && rm -f /opt/mongodb/$prog.lock
}

restart() {
  stop
  start
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart|reload|force-reload)
        restart
        ;;
  status)
        status $mongod
        RETVAL=$?
        ;;
      *)
        echo "Usage: $0 {start|stop|status|restart|reload|force-reload}"
        RETVAL=1
esac

exit $RETVAL
After this we can start MongoDB:
service mongodb start

And checking if it works
cd /opt/mongodb/bin
./mongo
MongoDB shell version: 1.8.0
connecting to: test
> 

MongoDB is up and running.


No comments: