Installing MongoDB on Red Hat Enterprise Linux
MongoDB has evolved into one of the most popular open source “NoSQL” databases—so-called because they dispense with the tabular storage schema of relational databases like MySQL and Postgres. NoSQL databases offer a variety of advantages in many cases
The biggest advantage is that MongoDB databases don’t require developers to define schemas before adding data to a database. Instead, they use a flexible document-based model, similar to Python dictionaries or Ruby hashes. With MongoDB, you don’t need to spend time creating tables before you can process your data. That makes the NoSQL approach ideal for situations where you don’t know how much data you have to handle, what form it is in, or how quickly it is going to move around.
There’s a lot more to say about what makes MongoDB, and NoSQL in general, a better fit for some situations. (I could also write a great deal about when not to use NoSQL—and that’s important, because despite NoSQL’s current trendiness, it’s not better in all contexts.)
But that’s all fodder for a separate blog post. For now, let’s move onto the meat of this post, which is how to install MongoDB on Red Hat Enterprise Linux (RHEL) in order to take advantage of NoSQL databases.
Preliminary Notes on MongoDB Installation
Before delving into the installation steps, we should go over some prerequisites. First, know that this guide applies to systems running RHEL 6 or later. (MongoDB does not support RHEL 5 or earlier.) It probably works pretty well for CentOS and other RHEL derivatives, but I haven’t tested those.
I also assume you want to install MongoDB using RPM packages and the yum package manager, which ships with RHEL. You could also compile MongoDB from source, but that is a lot more complicated and, for my money, not worth the trouble (pace, Gentoo fans).
You should also know that MongoDB no longer supports 32-bit systems. While it’s technically possible to install MongoDB on a 32-bit RHEL system using outdated, archived versions of MongoDB packages, you probably don’t want to do that—certainly not if you have any intention of using MongoDB in a production environment. So your RHEL server needs to be running a 64-bit kernel to follow this guide.
Note also that this guide is designed to allow you to install the latest stable version of MongoDB on RHEL. At the time of this writing, that’s version 3.2, which debuted in early 2016 (and has had a few minor updates since). MongoDB 3.2 offers a number of features not present in earlier releases, such as a totally new default storage engine (WildTiger) and a lot of handy new programming operators. For details, check out the MongoDB 3.2 release notes.
Lastly, the steps below cover installation of both the Community Edition and Enterprise versions of MongoDB. (The Community Edition is free; the Enterprise option, which offers some extra features, requires a commercial license.) The installation steps are basically the same, but the specifics vary, as noted below.
Step 1. Prepare Your Package Manager
Now that the boring stuff is out of the way, let’s install MongoDB.
The first step is to configure the yum package manager. This is pretty simple. First, create a file called /etc/yum.repos.d/mongodb-org-3.2.repo (for the Community Edition of MongoDB) or /etc/yum.repos.d/mongodb-enterprise.repo (for Enterprise).
Next, open up that file in your favorite text editor (on RHEL, I prefer gedit when working from the GUI, or vi if I’m on the CLI), and populate it with the following lines for the Community Edition:
[mongodb-org-3.2] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc
Or, for the Enterprise version, fill the file with:
[mongodb-enterprise] name=MongoDB Enterprise Repository baseurl=https://repo.mongodb.com/yum/redhat/$releasever/mongodb-enterprise/stable/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc
Step 2: Install MongoDB
If you thought that was easy, the next step was even easier (thanks to the magic of yum). Just run one of the following yum commands as root. The first command will install the Community release; the second one is for Enterprise:
sudo yum install -y mongodb-org
sudo yum install -y mongodb-enterprise
This will download the latest stable version of MongoDB from the official MongoDB repositories.
Step 3: Enjoy
That’s it. Now you can fire up a mongo shell and see for yourself just how fun and useful NoSQL can be for your massively scalable development needs. Check out the MongoDB documentation for details.
You can also connect your apps directly to MongoDB storage both locally and over the network (in the latter case, you’ll need to configure the firewall as needed, of course), although this generally requires the installation of language-specific plugins. (The links in this sentence provide instructions for using MongoDB with Java, Python, Node.js and Ruby, to name just a few frameworks.)
Package Management Quirks
Before you go, here are a couple of quick tips you might want to know regarding package management and your MongoDB installation:
- For those readers visiting from the future, I should note that yum may be superseded in newer versions of RHEL with dnf, the next-generation version of yum. However, based on all indications available to us here in 2016, it looks like the dnf interface will behave very similarly to yum, so the commands above should work if you simply swap out the name of one tool for the other.
- If you install MongoDB using the steps above, yum will automatically upgrade MongoDB to a newer version whenever one appears. If you don’t want this to happen, there’s an easy solution—simply “pin” the MongoDB packages, which tells yum not to upgrade them automatically. You do this by adding a line like the following to your /etc/yum.conf:
This will pin the Community Edition packages for MongoDB. If you’re working with MongoDB Enterprise, just modify that line with the appropriate package names: mongodb-enterprise instead of mongodb-org, and so on.
Another recommended route for installing MongoDB on RHEL is to use Red Hat Software Collections (RHSCL), which simplifies package management, and allows developers to run multiple versions of packages on the same system. Learn how to install MongoDB on RHEL with RHSCL, and learn more about installing Red Hat Software Collections itself.
Hemant Jain is the founder and owner of Rapidera Technologies, a full service software development shop. He and his team focus a lot on modern software delivery techniques and tools. Prior to Rapidera he managed large scale enterprise development projects at Autodesk and Deloitte.
Join the Red Hat Developer Program (it’s free) and get access to related cheat sheets, books, and product downloads.