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

undefined

In this post, we will show you how to install MongoDB 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 3.4, 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-3.4.repo to our existing CentOS/RHEL yum repositories to fetch the latest stable release of MongoDB “which is 3.4 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-3.4.repo
[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
EOL

 

  • For Fedora Systems “19, 20, 21, 22, 23, 24, 25”
# cat << EOL > /etc/yum.repos.d/mongodb-org-3.4.repo
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.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 v3.4.2
git version: 3f76e40c105fc223b3e5aac3e20dcd026b83b38b
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 v3.4.2 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 127.0.0.1:27017   0.0.0.0:*         LISTEN    24356/mongod 
tcp        0      0 0.0.0.0:3306      0.0.0.0:*         LISTEN    4304/mysqld 
tcp        0      0 0.0.0.0:111       0.0.0.0:*         LISTEN    1222/rpcbind 
tcp        0      0 0.0.0.0:22        0.0.0.0:*         LISTEN    340/sshd 
.....

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: 3.4.2
connecting to: test

> show dbs
Semari 0.000GB
local 0.000GB
test 0.000GB

> exit

As you see, we used mongo command to connect to the server, by default we connected to the test database. 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.

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

  1. Mark says:

    The repo file here will not work on Fedora, the “$releasever” would resolve as something like “24” on fedora 24 and there is no directory in the web server at the location pointed to by baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/

    • You’ve a point it’ll not work as you said. As Fedora and the latest releases of Fedora are Redhat 7, so we need to only replace this line “baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/” with this one “baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/3.4/x86_64/” in our yum repository configuration file. After this modification, the MongoDB repository will be available on your fedora. I’ll update this post to include a hint for Fedora Linux.

Leave a Reply to Mohammed Semari Cancel reply

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