March 17, 2020 in Tutorial7 minutes

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
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-7Note: 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 vimWe 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 wgetSimilar 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.ascCreate 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.ascIf 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!
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
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.2Now MongoDB installed let’s check MongoDB service using the command below
sudo service mongod status
We can also use systemctl command to check status of mongod service.
sudo systemctl status mongod
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 startor we can also use systemctl to start MongoDB service
sudo systemctl start mongod
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 mongodTo enable MongoDB service on boot on CentOS 7 we can use the command below.
sudo systemctl enable mongodBeside 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
In the command above, we grep MongoDB default port 27017.
As alternative we can also grep mongod application name
sudo netstat -naptu | grep mongod
We can also use ss to do similar check like netstat
ss -at | grep 27017
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
If we want to know MongoDB process details we can use ps command
ps aux | grep -m1 mongod
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
To see thread details you can press CTRL+H.

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
Switch to admin database.
use adminUse 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"}]})
Generate random string for password on the command line using the command belowuuidgen | 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 1To exit from mongo client you can type.
exit()or you can also type CTRL+D
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.
Open /lib/systemd/system/mongod.service file.
Find line
ExecStart=/usr/bin/mongod --config /etc/mongod.confReplace the line with
ExecStart=/usr/bin/mongod --auth --config /etc/mongod.confReload systemd daemons using the command below
sudo systemctl daemon-reloadRestart MongoDB service using the command below
sudo systemctl restart mongodOpen ``/etc/mongod.conf file
Find line
#security:Replace it with
security:
authorization: enabledRestart MongoDB service to enable authentication
sudo service mongod restartAfter enabling authentication we can connect using root user that we just created on the previous step.
mongo -uadmin admin -pThis 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.jsonImport 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 -pchange database to howtodojo
> use howtodojoTo 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" });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 stopTo uninstall MongoDB 4.2 we can use the command below
sudo yum remove mongodb-org*<img src="/wp-content/uploads/2020/03/install-mongodb-4.2-centos-7-15-750x400.png" alt=““Remove MongoDB”"> The command above only removes MongoDB packages.
To remove MongoDB log directory use command below
sudo rm -r /var/log/mongodbTo 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/mongodbYou can find references related to MongoDB 4.2 below
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.