Running MongoDB 2.0.1 in Fedora 15 from sources

MongoDB: the document-oriented database

MongoDB is one of the major trends today on the NoSQL world; with a great list of custormers like Foursquare, Boxed Ice, Etsy, SourceForge, Justin.Tv, GitHub, Disqus, and many more. The completed list can be found on the MongoDB official site.

For that reason, on May, 2011, we begun a excellent project that we called Naire (elephant dominator in Indian), focused on the building of a web platform for PostgreSQL servers monitoring, and we opted for MongoDB for storing all metrics and logs, and we saw that it was a great choice.

The completed stack is: Python/Django, RabbitMQ and Celery for background tasks processing, Nginx like Http Server with uWSGI, and Varnish-Cache for HTTP request caching and HTTP accelerator.

But, in that time, we didn’t have the amazing RPM packages built by the 10gen’s software engineers (the company behind of the commercial support for MongoDB), so, we had to compile MongoDB for Fedora from sources, and that’s the objective today with this post.

You have noticed that I´m a big fan of Fedora Linux, and of course, my laptop (A modest Acer Aspire 5251–1805) runs that operating system. It´s a amazing platform to develop Open Source applications, because there are a lot of packages and suites available for it. Pick one, and you will find it on the official repositories, or in the non-official but very useful too:

  • PostgreSQL (my favorite relational database system)
  • The amazing Python programming language, one of the key reasons why I use Fedora
  • Django, the well known web framework for perfectionists
  • Ruby on Rails, one of the most used web frameworks today
  • PHP, on the most used programming languages for web development today
  • Java, the platform by excellence for Enterprise Software Development
  • A lot of projects of the Apache Software Foundation including Http Server, Hadoop and its related projects
  • Nginx, one of the most well known HTTP servers today for its amazing performance

All these packages are available on Fedora: on the official repositories or 3rd party repositories. Remi, FreshRPM, are some of the 3rd party repositories more used.

MongoDB’s RPM packages are built now by 10gen engineers, so you can search all this information on the official site, but, like I said, we are going to install MongoDB from sources.

Step 1: Create the necessary directories for MongoDB

In Fedora and Red Hat Enterprise Linux, which is the main sponsor of the project; there are some conventions for Database applications and servers; so we want to adjust to these rules:

  • The data directory should be on the /var/lib/service_name/data
  • The binary directory should be on the /usr/local/service_name/bin
  • Every service should have its own user

So, we should create the required group and user for MongoDB, and its related directories on these directories:

 $ mkdir -p /usr/local/mongodb/bin 
 $ mkdir -p /var/lib/mongodb/data 
 $ mkdir -p /var/lib/mongodb/logs 
 $ groupadd mongodb 
 $ useradd mongodb -d /var/lib/mongodb -c “MongoDB Administration User” -g mongodb -s /bin/bash 
 $ chown -R mongodb:mongodb /usr/local/mongodb/bin 
 $ chown -R mongodb:mongodb /var/lib/mongodb/data 
 $ chown -R mongodb:mongodb /var/lib/mongodb/logs

Step 2: Download MongoDB for your architecture

Then, we are going to download MongoDB from its official site. In my case, I use Fedora 15 for 64 bits (x86_64), so, I’m going to download the mongodb-linux-x86_64–2.0.1.tgz version. Then, you should decompress the .tgz file, and move the bin/ content to /usr/local/mongodb/bin, and check the permissions for those binary files:

 $ tar xvf mongodb-linux-x86_64–2.0.1.tgz -C /tmp 
 $ cd /tmp/mongodb-linux-x86_64–2.0.1 
 $ mv bin/* /usr/local/mongodb/bin 
 $ chmod -R 700 /usr/local/mongodb/bin 
 $ chmod -R 700 /var/lib/mongodb/data 
 $ chmod -R 700 /var/lib/mongodb/logs

Step 3: Run MongoDB

The last step is to run MongoDB using the data directory and the logs directory:

 $ PATH=$PATH:/usr/local/mongodb/bin 
 $ export PATH 
 $ su mongodb 
 $ mongod — dbpath /var/lib/mongodb/data/ — logpath /var/lib/mongodb/logs/mongodb-2.0.1.log 
 all output going to: /var/lib/mongodb/data/mongodb-2.0.1.log

Step 4: Connect to MongoDB with shell

Then you can run the shell command (mongo) to connect to the database:

 $ mongo — host -port 27017 

 The output in my machine is:

 MongoDB shell version: 2.0.1 
 connecting to: 

 And a short piece of the log’s output here in my laptop is this:

 Mon Feb 13 15:55:46 [initandlisten] MongoDB starting : pid=7016 port=27017 dbpath=/var/lib/mongodb/data 64-bit host=unknown 
 Mon Feb 13 15:55:46 [initandlisten] db version v2.0.1, pdfile version 4.5
 Mon Feb 13 15:55:46 [initandlisten] git version: 3a5cf0e2134a830d38d2d1aae7e88cac31bdd684
 Mon Feb 13 15:55:46 [initandlisten] build info: Linux–2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOS
 Mon Feb 13 15:55:46 [initandlisten] options: { dbpath: “/var/lib/mongodb/data/”, logpath: “/var/lib/mongodb/logs/mongodb-2.0.1.log” }
 Mon Feb 13 15:55:46 [initandlisten] journal dir=/var/lib/mongodb/data/journal
 Mon Feb 13 15:55:46 [initandlisten] recover : no journal files present, no recovery needed
 Mon Feb 13 15:55:46 [initandlisten] waiting for connections on port 27017
 Mon Feb 13 15:55:46 [websvr] admin web console waiting for connections on port 28017
 Mon Feb 13 15:56:46 [clientcursormon] mem (MB) res:17 virt:612 mapped:0
 Mon Feb 13 15:57:47 [initandlisten] connection accepted from #1
 Mon Feb 13 15:58:00 [conn1] end connection
 Mon Feb 13 15:58:49 [initandlisten] connection accepted from #2
 Mon Feb 13 16:01:46 [clientcursormon] mem (MB) res:17 virt:613 mapped:0

Final Thoughts

Well, with these steps, you can use MongoDB for development process in your personal workstation, but if you don’t want to complicate you, use your Package Manager in your distribution; but if you can actually learn how MongoDB works; this is a useful way to find it. I let you a challenge for you: Write a init script compatible with chkconfig in Fedora for MongoDB. This will be my next blog post, but I want to let you to think and find a good solution for it.

Happy Hacking !!!

  • Marcos Ortiz Valmaseda
  • Sr. Software Engineer (UCI)
  • Linkedin’s profile
  • @marcosluis2186


Marcos Ortiz

Marcos Ortiz