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:


and if no error message appears – have fun!

A Morning with MongoDB – Milan

Yesterday I’ve attended “A Morning with MongoDB“, the first Italian MongoDB event officialy promoted by 10gen (here is a list of past/upcoming MongoDB events throughout the world).

It’s been a nice way for Italian SW developerss and managers to get acquainted with MongoDB features, as well as to assess its spread potential into Italian IT market. Successful case histories have been shown by Italian companies.

For me, it has been a good time to get in touch with a few of the presenters and to strenghten my MongoDB knowledge, as I’m going to need it to solve problems at work. I was also particularly interested in understanding MongoDB’s capabilities as regards geographic datasets storage and querying and if there is any OGC-compliant map server that can be backed by a document-oriented datastore such as Mongo.

The latter question still remains unanswered to me.

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
tar xvf mongodb-linux-i686-2.0.3.tgz
mv 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/ :

source /etc/rc.d/init.d/functions

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 -eq 0 ] && touch /var/lock/subsys/$prog
  return $RETVAL

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

case "$1" in
  status $mongod
  echo $"Usage: $0 {start|stop|restart|status}"

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

and everything was fine.