How to Install MongoDB 4.0 Community Edition on CentOS/RHEL 7 and Fedora Linux Systems

undefined

In this post, we will show you how to install MongoDB 4.0 community edition on Linux systems (RPM Family “Redhat /CentOS /Scientific Linux 7 and latest releases of Fedora ”). This tutorial will help you to install the latest stable release “MongoDB 4.0, exists at the time of writing this post” on CentOS, RHEL and Fedora Systems.

MongoDB is a NoSQL database intended for storing large amounts of data in document-oriented storage with dynamic schemas. NoSQL refers to a database with a data model other than the tabular format used in relational databases such as MySQL, PostgreSQL, and Microsoft SQL. MongoDB features include: full index support, replication, high availability, and auto-sharding. So, let’s start our installation steps.

Prerequisites

Before begin our installation steps, we need to check the following points.
  • Our CentOS/RHEL/Fedora must be 64-bit systems,  run the following command:
# uname -r
3.18.44-20.el7.x86_64
  • We need to run the following steps as the root user.

Step 1: Add MongoDB Yum Repository

The first step is adding the MongoDB yum repository configuration file /etc/yum.repos.d/mongodb-org-4.0.repo to our existing CentOS/RHEL yum repositories to fetch the latest stable release of MongoDB “which is 4.0 at the time of writing this article”. Depending on your OS, copy and paste the following lines into your terminal “of course without #”:

  • For CentOS/RHEL 7
# cat << EOL > /etc/yum.repos.d/mongodb-org-4.0.repo
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
EOL
  • For Fedora Systems “19, 20, 21, 22, 23, 24, 25”
# cat << EOL > /etc/yum.repos.d/mongodb-org-4.0.repo
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
EOL

Step 2 : Install MongoDB Server

Now, we’ll use the yum package manager to install mongodb-org package “A metapackage that will automatically install the four component packages mongodb-org-server, mongodb-org-mongos, mongodb-org-shell, and mongodb-org-tools”. Run the following command will install latest stable version available.

# yum install -y mongodb-org

Step 3 : Enable, Start, and Restart MongoDB Server

As we successfully installed all the four packages included in the mongodb-org package, we need to enable and start our MongoDB server.

To enable the server to start at reboot and boot time, run the following command:

# systemctl enable mongod

To start our MongoDB server, run the following command:

# systemctl start mongod

To restart MongoDB server “in case of configuration changes”, run the following command:

# systemctl restart mongod

Step 4: Checking the MongoDB Version and running service

To check installed MongoDB version, run the following command:

# mongo --version
MongoDB shell version v4.0.9
git version: fc525e2d9b0e4bceff5c2201457e564362909765
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

As you see, we are using MongoDB version v4.0.9 for CentOS/RHEL 7.

Now, we’ll check that the service is up and running by running netstat command, run the following command:

#  netstat  -nltp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      223/httpd           
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      223/httpd           
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      21126/mongod        
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      -     

As you see, our MongoDB server is up and listening on the localhost IP “127.0.0.1”and  port “27017”. The listening IP and port is the default shipped configuration.

Hints:

1. The default MongoDB configuration will accept connection only from the machine MongoDB packages installed on.
2. To change the listening IP, edit the configuration file "/etc/mongod.conf", and comment/remove the line start with "bindIp: 127.0.0.1", then restart you MongoDB server.

A final check we run is connecting to the MongoDB using command line and execute some test commands for checking proper working:

# mongo
MongoDB shell version v4.0.9
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
2019-04-18T08:33:04.198+0000 I NETWORK  [listener] connection accepted from 127.0.0.1:58236 #1 (1 connection now open)
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
> exit

As you see, we used mongo command to connect to the server. Also we listed all the databases we have on the server by running “show dbs” command.

Extra Step: Uninstall MongoDB Community Edition

If you want to completely remove MongoDB from a system, you must remove the MongoDB applications themselves, the configuration files, and any directories containing data and logs.

Hints:

1. This process will completely remove MongoDB, its configuration, and all databases.
2. This process is not reversible, so ensure that all of your configuration and data is backed up before proceeding.

Run the following steps to completely uninstall MongoDB server.

  • Stop the MongoDB Server.
    You need to stop the mongod process by issuing the following command:
# systemctl stop mongod
  •  Remove the installed Packages.
    Remove any MongoDB packages that you had previously installed.
# yum erase $(rpm -qa | grep mongodb-org)
  • Remove Data and Log Directories.
    Remove MongoDB databases and log files.
# rm -rf /var/log/mongodb # rm -rf /var/lib/mongo

Finally, I hope this article is helpful for you.

If You Appreciate What We Do Here On Mimastech, You Should Consider:

  1. Stay Connected to: Facebook | Twitter | Google+
  2. Support us via PayPal Donation
  3. Subscribe to our email newsletters.
  4. Tell other sysadmins / friends about Us - Share and Like our posts and services

We are thankful for your never ending support.

Leave a Reply

Your email address will not be published. Required fields are marked *