How to install MongoDB on Windows as a service

I’m showing you a quick procedure to install MongoDB as a service on Windows platforms (I simply gathered the hints explained in the official documentation page and adapted the whole stuff to my specific case).

In my example, I’m installing MongoDB version 2.2.0 on a Windows Server 2003 R2 machine and my goal is to have it available as a service.

The procedure is pretty straightforward: all you need is to setup the path in which MongoDB will physically store the data collections, to provide a logfile for the service we’re going to create and finally to tell the mongod daemon to run as a service.

Here we go (be sure to enclose all the paths in double quotes if they contain spaces):


:: 1. Download the installation package (a .zip archive) and decompress it into folder C:\mongodb-2.2.0\

:: 2. Create the data path folders (in my example, C:\mongodb-2.2.0\data\db):
md C:\mongodb-2.2.0\data
md C:\mongodb-2.2.0\data\db
set datapath = C:\mongodb-2.2.0\data\db

:: 3. Setup mongod configuration file path (my file is C:\mongodb-2.2.0\mongod.cfg):
set configpath = C:\mongodb-2.2.0\mongod.cfg

:: 4. Create the folder that will host the service's log file (my folder is C:\mongodb-2.2.0\log) and write its path into the config file:
md C:\mongodb-2.2.0\log
echo logpath = C:\mongodb-2.2.0\log\mongod.log > C:\mongodb-2.2.0\mongod.cfg

:: 5. install mongod as a service:
C:\mongodb-2.2.0\bin\mongod.exe --config %configpath% --dbpath %dbpath% --install

Now you can start/stop/remove the MongoDB service via the services administration graphical interface, or via the command line using the following commands:


:: Start service
net start MongoDB

:: Stop service
net stop MongoDB

:: Uninstall the service
C:\mongodb-2.2.0\bin\mongod.exe --remove

Wanna test out your installation? Just call the MongoDB Javascript shell:


C:\mongodb-2.2.0\bin\mongo.exe

and if no error message appears – have fun!

Advertisements

How to install MongoDB on Ubuntu

This is a quick-guide to install MongoDB on Ubuntu 12.04 (if you want to know how to install it also on Fedora Core 11, please check out my old post.

Here is all you have to do:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
sudo touch /etc/apt/sources.list.d/10gen.list
line="$(echo "deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen")"
sudo echo "$line" > /etc/apt/sources.list.d/10gen.list
sudo apt-get update

And then you can install MongoDB with

sudo apt-get install mongodb-10gen

Enjoy!

How to install MongoDB on Fedora

During the last days, I’ve been delving into NoSQL datastores study and now I’ve got the chance to use MongoDB for a real-life need (of course, something relating to work issues). In order to reach my target, I needed a fresh installation of one of the most promising NoSQL technologies: the document-oriented datastore MongoDB.

Here is how I managed to install MongoDB 2.0.3 on a Fedora Core 11 host. You can easily adapt the steps I went through to your operational environment.

We’re about to issue every command as root user.

First, I downloaded, extracted and placed the Mongo stuff (I didn’t use 10gen repo, but just a tar.gz from MongoDB website):

cd /opt
wget http://fastdl.mongodb.org/linux/mongodb-linux-i686-2.0.3.tgz
tar xvf mongodb-linux-i686-2.0.3.tgz
mv mongodb-linux-i686-2.0.3.tg mongodb-2.0.3

I decided that my databases would go under /usr/data/mongodb and that MongoDB log file would be /var/log/mongodb.log

As I wanted my MongoDB server instance to be started/stopped as a demon, I prepared the following init.d script named “mongodb” and placed it under /etc/init.d/ :

#!/bin/bash
source /etc/rc.d/init.d/functions
prog="mongod"
mongod="/opt/mongodb-2.0.3/bin/mongod"
RETVAL=0

start() {
  echo -n $"Starting $prog: "
  #This is the fundamental call to start the MongoDB server instance
  daemon $mongod "--fork --journal --dbpath /usr/data/mongodb \
  --logpath /var/log/mongodb.log \
  --logappend 2>&1 >>/var/log/mongodb.log"
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
  return $RETVAL
 }

stop() {
  echo -n $"Stopping $prog: "
  killproc $prog
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
  return $RETVAL
}

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

exit $RETVAL

Then, I opened my Iptables firewall’s INPUT chain so that port 27017 (the port MongoDB server is listening to) is not blocked: I opened the file /etc/sysconfig/iptables and added the following rule before of the COMMIT statement:

-A INPUT -p tcp -m tcp -m multiport --ports 27017 -j ACCEPT

and restarted iptables with:

service iptables restart

That’s it. Finally, I started the server instance with:

service mongodb start

and tested the whole thing opening the Mongo Javascript shell like this:

cd /opt/mongodb-2.0.3/bin
./mongo

and everything was fine.