How to Install MongoDB on CentOS 7

Try it in our public cloud & Get $5 Credit
CLAIM NOW

MongoDB is a popular, document-oriented, NoSQL database that is quick to install, easy to develop with, and trivial to scale. It is a great solution for anyone wishing to cluster an application’s storage across multiple servers, tracking arbitrarily complex JSON and binary data. As it is schemaless, it is also a quick path for applications needing to store data but without a clear idea of what form that data might take, or for services needing to store arbitrarily diverse records with no schema in common. Installing MongoDB on CentOS 7 is quick and easy, particularly as the company behind MongoDB maintains updated package repositories for most popular Linux distributions.

Getting Started

To complete this guide, you will need the following:
• 1 Node (Cloud Server or Dedicated Server) running a clean CentOS 7.
• Root access to the server.

It should also work on servers with other services installed, provided MongoDB’s port is not taken by anything else. When complete, the server will have a fresh MongoDB installation that can be connected to locally, or clustered with other servers to replicate and shard documents.

Tutorial

Start by updating your CentOS 7 installation. This will integrate any currently available security updates and bugfixes, and should be performed on a regular basis to keep your server running more efficiently.

yum -y update

MongoDB ships in its own official package repository. Let’s add that to Yum so we can automatically download the package and keep it updated later.

nano /etc/yum.repos.d/mongodb.repo

[mongodb]
name=MongoDB Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
gpgcheck=0
enabled=1

The repo list must now be updated before MongoDB can be installed.

yum -y update

Next we install MongoDB itself.

yum -y install mongodb-org mongodb-org-server

MongoDB must now be started and configured to run on boot. Note that the MongoDB daemon is called mongod, which is what you’ll actually be starting and configuring. We’ll also use the chkconfig command to check how this went.

systemctl start mongod.service
chkconfig mongod on

To determine if MongoDB is running, we’ll check its status.
systemctl status mongod.service
? mongod.service - SYSV: Mongo is a scalable, document-oriented database.
Loaded: loaded (/etc/rc.d/init.d/mongod)
Active: active (running) since Fri 2016-06-24 09:55:34 UTC; 2min 10s ago
Docs: man:systemd-sysv-generator(8)
Main PID: 32306 (mongod)
CGroup: /system.slice/mongod.service
??32306 /usr/bin/mongod -f /etc/mongod.conf

To further test our mongod instance, let’s check the statistics as reported by the database itself.

mongostat
insert query update delete getmore command flushes mapped vsize res faults locked db idx miss % qr|qw ar|aw netIn netOut conn time
*0 *0 *0 *0 0 1|0 0 80m 443m 31m 0 local:0.0% 0 0|0 0|0 62b 2k 1 09:59:25
*0 *0 *0 *0 0 1|0 0 80m 443m 31m 0 local:0.0% 0 0|0 0|0 62b 2k 1 09:59:26
*0 *0 *0 *0 0 1|0 0 80m 443m 31m 0 local:0.0% 0 0|0 0|0 62b 2k 1 09:59:27
*0 *0 *0 *0 0 1|0 0 80m 443m 31m 0 local:0.0% 0 0|0 0|0 62b 2k 1 09:59:28
*0 *0 *0 *0 0 1|0 0 80m 443m 31m 0 local:0.0% 0 0|0 0|0 62b 2k 1 09:59:29
*0 *0 *0 *0 0 1|0 0 80m 443m 31m 0 local:0.0% 0 0|0 0|0 62b 2k 1 09:59:30
*0 *0 *0 *0 0 1|0 0 80m 443m 31m 0 .:0.0% 0 0|0 0|0 62b 2k 1 09:59:31
*0 *0 *0 *0 0 1|0 0 80m 443m 31m 0 local:0.0% 0 0|0 0|0 62b 2k 1 09:59:32
*0 *0 *0 *0 0 1|0 1 80m 443m 31m 0 local:0.0% 0 0|0 0|0 62b 2k 1 09:59:33
*0 *0 *0 *0 0 1|0 0 80m 443m 31m 0 local:0.0% 0 0|0 0|0 62b 2k 1 09:59:34
insert query update delete getmore command flushes mapped vsize res faults locked db idx miss % qr|qw ar|aw netIn netOut conn time

Conclusion

MongoDB is now installed, running, configured to launch on boot, and ready to be connected to and clustered. Services can now connect to the mongod daemon by accessing localhost on port 27017. If this guide was helpful to you, kindly share it with others who may also be interested.