Installing MongoDB 3.4 on RHEL 7/CentOS 7

MongoDB is a free, open-source NoSQL database that stores data in a JSON documents format. One of the biggest differences between MongoDB and your run-of-mill RDBMS database is that the array fields in a MongoB collection can vary between documents and the structure of the data can be easily changed as requirements evolve without performance degradation. Since there is no set structure declared to the system, MongoDB databases are very flexible. Another key difference is that all data for a given record is stored in a single document rather than across multiple tables.

The official installation docs for MongoDB on Linux can be found here: https://docs.mongodb.com/master/tutorial/install-mongodb-on-red-hat/

As a best practice, I always take a backup before making any changes.

To save my Virtual Machine state before installing MongoDB, I took a snapshot before proceeding by running the following command

VBoxManage snapshot "CentOS7-NoSQL" take "Pre-MongoDB Installation" --live

Before downloading the packages and it's dependencies, you have to add the repository site for MongoDB 3.4.

As sudo:

  • Create the yum repo file
cd /etc/yum.repos.d
  • Create the MongoDB repo file:
sudo vi mongodb-org-3.4.repo

Paste the repo information listed below:

[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc

Save the file (:wq!)

  • Install the MongoDB packages and tools:
sudo yum install -y mongodb-org

If you get an error while installing mongodb-org server regarding write access on /etc/group, make the following changes to allow write access to /etc/group

As sudo run:

sudo semanage fcontext -a -t passwd_file_t '/etc/group'
sudo semanage fcontext -a -t passwd_file_t '/etc/gshadow'

Then execute:

sudo /sbin/restorecon -v /etc/group
sudo restorecon -v '/etc/gshadow'

Retry the package installation:

sudo yum install mongodb-org-server.x86_64 0:3.4.4-1.el7

Configure SELinux to allow MongoDB to start on Red Hat Linux-based systems (Red Hat Enterprise Linux or CentOS Linux):

First verify if SELinux is enforcing, disabled, or permissive:

sudo getenforce

If Enforcing, enable access to the relevant ports that the MongoDB deployment will use (e.g. 27017) by running:

sudo semanage port -a -t mongod_port_t -p tcp 2701

You can verify that MongoDB will start following a system reboot by issuing the following command as root:

systemctl status mongod.service

(It should say Active)

If permissive or disabled, no further actions are necessary.

Data Dictionaries and Permissions:

Create the data directory - This is the default location that MongoDB saves data files.

mkdir -p /data/db
chmod -R mongod: /data/db
chown 775 /data/db

*Note: MongoDB stores its data files in /var/lib/mongo and its log files in /var/log/mongodb by default, using the mongod user account. If you change the data path, you must make changes to the /etc/mongod.conf file and update your SELinux policy to grant mongod user access to the new path.

That's it, you've successfully installed MongoDB 3.4!

To start MongoDB:

service mongod start

To stop mongodb:

service mongod stop

To restart the mongodb service:

service mongodb restart

To verify the mongodb service is running:

Check the contents of the log file at /var/log/mongodb/mongod.log:

sudo cat /var/log/mongodb/mongod.log

And search for the following line:

[initandlisten] waiting for connections on port <port>

where <port> is the port configured in /etc/mongod.conf, 27017 by default.

 

 

* Cover image from mongodb.org