How to Install MongoDB 4.2 on CentOS 7

In this tutorial we learn how to install MongoDB 4.2 on CentOS 7. We also learn how to create user and import sample database and uninstall MongoDB 4.2.

Introduction

In this guide, we will learn how to install MongoDB 4.2 on CentOS 7. We will also learn to configure and secure our MongoDB 4.2 installation.

This tutorial can also be followed to install MongoDB 4.2 on RHEL 7 or Oracle Linux 7.

MongoDB is a document database with scalability and flexibility that you want with query and indexing that you need.

— from [What is MongoDB on MongoDB website][1]

Prerequisites

  • Fresh install of CentOS 7 with sudo access

Step 0 – Pre-installation

Import CentOS 7 RPM GPG Key

Import CentOS 7 RPM GPG Key using command below rpm or yum need this key to verify the package downloaded from repository is not tampered or corrupt. When successfully run, the command below will not provide any output.


sudo rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

Install Vim

Note: If you already have your own favorite text editor you can skip this part.

To install vim on CentOS 7 we can use the command below.


sudo yum install vim

Install wget

We will use wget later in this tutorial to download data sample from internet. Again, this is optional, you can use curl that usually already installed if you prefer.


sudo yum install wget

Step 1 – Import MongoDB RPM GPG Key

Similar to the steps above when we’re importing CentOS 7 RPM GPG Key. We import MongoDB GPG key so rpm and yum can verify the packages we downloaded from the repository are not corrupt or tampered.


sudo rpm --import https://www.mongodb.org/static/pgp/server-4.2.asc

Step 2 – Create MongoDB Yum Repository Configuration

Create new file /etc/yum.repos.d/mongodb-org-4.2.repo using your favorite editor with contents below.


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

If you’re using vim you can type

sudo vim /etc/yum.repos.d/mongodb-org-4.2.repo

Press i to change to INSERT mode.

Paste the repository configuration above to the file.

Press ESC to exit insert mode.

Now you’re on command mode. To save the file and exit vim you can type :wq!

Step 3 – Install MongoDB 4.2 on CentOS 7

To install the latest stable version of MongoDB 4.2 you can use the command below. At the time of this writing, the latest stable version of MongoDB 4.2 is 4.2.3


sudo yum install mongodb-org
mongodb 4.2 yum packages

To install a previous stable version of MongoDB 4.2 you need to specify a version for each package. For example, if you need to install MongoDB 4.2.2 you can use the command below.


sudo yum install \
    mongodb-org-4.2.2 \
    mongodb-org-server-4.2.2 \
    mongodb-org-shell-4.2.2 \
    mongodb-org-mongos-4.2.2 \
    mongodb-org-tools-4.2.2

Managing MongoDB Service in CentOS 7

Now MongoDB installed let’s check MongoDB service using the command below

sudo service mongod status
Check MongoDB service status in CentOS 7

We can also use systemctl command to check status of mongod service.

sudo systemctl status mongod
Check MongoDB 4.2 Service Status Using systemctl

We will get output similar to below which informs that mongod is not running.

To start MongoDB service we can use command below:

sudo service mongod start

or we can also use systemctl to start MongoDB service

sudo systemctl start mongod
Start MongoDB 4.2 on CentOS 7 using systemctl

Unlike MongoDB package on Ubuntu, MongoDB in CentOS is already set to enabled on boot.

To disable MongoDB service on boot on CentOS 7 we can use the command below.

sudo systemctl disable mongod

To enable MongoDB service on boot on CentOS 7 we can use the command below.

sudo systemctl enable mongod

Checking MongoDB Service on CentOS 7

Beside using service or systemctl command, we can use multiple tools to check status of MongoDB service.

To check where MongoDB service listening to we can use netstat


sudo netstat -naptu | grep 27017
Check MongoDB listening port using netstat

In the command above, we grep MongoDB default port 27017.

As alternative we can also grep mongod application name


sudo netstat -naptu | grep mongod
MongoDB 4.2 on CentOS 7 check listening port using netstat

We can also use ss to do similar check like netstat


ss -at  | grep 27017
MongoDB 4.2 on CentOS 7 check listening port using ss grep port

We can use ss and grep mongod process. Since we need to check all process name we need to use sudo this time


sudo ss -anp | grep mongod
Check MongoDB 4.2 on CentOS 7 check listening port using ss grep mongod process name

If we want to know MongoDB process details we can use ps command

ps aux | grep -m1 mongod
MongoDB 4.2 on CentOS 7 check process using ps

We use option -m1 to so we only show the first line of grep since grep will also our grep process that contains mongod word.

top command can be used to see more detailed and real time resource usage of MongoDB process. Use command below to use top but filter only for process run by mongodb user.


top -u mongod
MongoDB 4.2 on CentOS 7 check mongod process using top

To see thread details you can press CTRL+H.

Show MongoDB 4.2 Threads on CentOS 7

Creating root and admin users

MongoDB user management is different compared to RDBMS user management like MySQL or PostgreSQL.

In MongoDB, the user is managed per database. If you want to create an administrative user you need to create a user in admin database.

Connect to MongoDB using mongo client


mongo
Open MongoDB 4.2 Client on CentOS 7

Switch to admin database.


use admin

Use the command below to create a user root with the root role. Don’t forget to change the password. See the steps below to generate a random password.


db.createUser({user:"root", pwd:"changemeplease123123123", roles:[{role:"root", db:"admin"}]})
Create root user with root role in db admin

Generate random string for password on the command line using the command below


uuidgen | sha256sum | awk {'print $1'}

Besides using awk we can also use cut utility to only get the randomly generated password.


uuidgen | sha256sum | cut -d ' ' -f 1

To exit from mongo client you can type.


exit()

```

or you can also type `CTRL+D`

## Enabling Authentication

There are two ways to enable MongoDB authentication, by updating a systemd service file or updating mongod.conf file.

I recommend using the second method since MongoDB service file might be overwritten by yum when we upgrade the MongoDB package.

### Updating mongod.service file

Open `/lib/systemd/system/mongod.service` file.

Find line

```

ExecStart=/usr/bin/mongod --config /etc/mongod.conf

```

Replace the line with

```

ExecStart=/usr/bin/mongod --auth --config /etc/mongod.conf

```

Reload systemd daemons using the command below

```

sudo systemctl daemon-reload

```

Restart MongoDB service using the command below

```

sudo systemctl restart mongod

```

### Updating mongod.conf file

Open ``/etc/mongod.conf file

Find line

```

#security:

```

Replace it with

```

security:
  authorization: enabled

```

Restart MongoDB service to enable authentication

```

sudo service mongod restart

```

After enabling authentication we can connect using root user that we just created on the previous step.

```

mongo -uadmin admin -p

```

## Import Sample Data To MongoDB 4.2

This section will guide you on how to download and import some data sample that MongoDB provides

The sample data that we are using is from MongoDB that contains restaurant data.

Download the sample dataset using wget.

```

wget -c https://raw.githubusercontent.com/mongodb/docs-assets/primer-dataset/primer-dataset.json

```

Import the dataset using `mongoimport` command. We will import the data to database `howtodojo` and collection named `restaurants`

```

mongoimport -uroot \
            --authenticationDatabase admin \
            --db howtodojo \
            --collection restaurants \
            --drop \
            --file primer-dataset.json \
            -p ""

```

 

Now let's try to query to the database. Let's find all restaurant with the name Wendy'S

Log in to the database using root credential that we created Before

```

mongo -uroot -p

```

change database to `howtodojo`


> use howtodojo

```

To list collections inside a database in MongoDB we can use command below


> db.getCollectionNames()

```

to find restaurant with name Wendy'S we can use command below


db.restaurants.find({"name" : "Wendy'S"})

```

## Uninstall MongoDB 4.2

In this section, we'll learn how to uninstall MongoDB 4.2 from CentOS 7. Please be really careful when running the command in this section.

Before we uninstall MongoDB 4.2 we need to stop MongoDB service first.

```

sudo service mongodb stop

```

To uninstall MongoDB 4.2 we can use the command below

```

sudo yum remove mongodb-org*

```

 

The command above only removes MongoDB packages.

To remove MongoDB log directory use command below

```

sudo rm -r /var/log/mongodb

```

To remove the MongoDB data directory use command below

WARNING: The command below will remove your data and cannot be restored. be very very very careful when you're running the command below.

```

sudo rm -r /var/lib/mongodb

```

## MongoDB 4.2 References

You can find references related to MongoDB 4.2 below

  1. [MongoDB 4.2 What's New (pdf)][2]
  2. [MongoDB 4.2 Manual][3]
  3. [MongoDB 4.2 Release Notes][4]
  4. [MongoDB 4.2 FAQ][5]
  5. [Configuration file options][6]

## Conclusion

In this article, we learn how to install MongoDB 4.2 on CentOS 7.

We also learn how to manage MongoDB service, check MongoDB service status using multiple tools, create root user and also enable authentication in MongoDB 4.2.

At the end of this tutorial, we learn how to uninstall MongoDB 4.2 from CentOS 7.

Now you can start building your application using MongoDB as a database on CentOS 7.

 [1]: https://www.mongodb.com/what-is-mongodb
 [2]: https://webassets.mongodb.com/mongodb_whats_new_4.2.pdf
 [3]: https://docs.mongodb.com/v4.2/
 [4]: https://docs.mongodb.com/v4.2/release-notes/4.2/
 [5]: https://docs.mongodb.com/v4.2/faq/
 [6]: https://docs.mongodb.com/v4.2/reference/configuration-options/