How to install MongoDB on CentOS 7 linux

MongoDB is a NoSQL database engine with full flexible index support and rich queries and here we want to show how to install MongoDB on CentOS 7 linux.
It is an opensource database engine that stores data as Key-Value in Json format. Some big companies including Adobe, Facebook, Google, eBay, and Coinbase use MongoDB in their applications.

Here is our environment:

OS: CentOS 7 linux on VMware
Firewall: firewalld
SElinux: enforcing
IP address: 192.168.147.128
MongoDB version: 4.2

1- Add MongoDB repository

to install latest version of MongoDB, we add its repository and then install it using yum package manager:

# vim /etc/yum.repos.d/mongodb.repo

then put these content in above file:

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

2- Install MongoDB

Now we install MongoDB package from repository with yum:

# yum install mongodb-org

3- Start service

we enable MongoDB service automatically upon system starting and also run this service now:

# systemctl enable mongod.service
# systemctl start mongod.service
# systemctl status mongod.service -l

if you have any output like the following in “systemctl status mongodb.service -l”:

SELinux is preventing /usr/bin/mongod from read access on the file snmp.

run these commands and see output of mongodb.service status until errors disappear:

# grep mongod /var/log/audit/audit.log | audit2allow -M mypol
# semodule -i mypol.pp
# grep ftdc /var/log/audit/audit.log | audit2allow -M mypol
# semodule -i mypol.pp

MongoDB will be listening on “127.0.0.1:27017”.

4- Check version

to check MongoDB installed version, we use the following command:

# mongod --version

the output will be something like:

db version v4.2.1
git version: edf6d45851c0b9ee15548f0f847df141764a317e
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
distmod: rhel70
distarch: x86_64
target_arch: x86_64

then we connect to MongoDB shell and execute some test commands:

[root@tuxtips ~]# mongo
# use mydb;
# db.test.save( { a: 1 } )
# db.test.find()
{ "_id" : ObjectId("54fc2a4c71b56443ced99ba2"), "a" : 1 }

5- Create admin user

to do that we connect to shell and execute these commands:

[root@tuxtips ~]# mongo
# use admin;
# db.createUser(
{
user: "tuxtips_admin",
pwd: "tuxtipspassword",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)

if everything goes well the output will be:

Successfully added user: {
"user" : "tuxtips_admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}

to see list of users we can run:

# show users;

6- Configure authentication

Now for created admin user, we must create authentication to prevent other users from running commands without authorization.
To enable authentication edit the /lib/systemd/system/mongod.service file, under the [Service] section, locate and edit the Environment parameter as below:

Environment="OPTIONS= --auth -f /etc/mongod.conf"

because service file has been altered we must reload systemctl daemon and restart MongoDB service:

# systemctl daemon-reload
# systemctl restart mongod

to authenticate, we pass credentials like below:

# use admin;
# db.auth('tuxtips_admin', 'tuxtipspassword');

then we can run any command we want like:

# show users

finally to exit MongoDB shell we execute:

# exit