How to Install MongoDB Community Edition on CentOS/RHEL 7 and Fedora Linux Systems
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.
- Our CentOS/RHEL/Fedora must be 64-bit systems, run the following command:
# uname -r
- 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 >
[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 >
[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
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:
MongoDB shell version: 3.4.2
connecting to: test
> show dbs
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:
- Stay Connected to: Facebook | Twitter | Google+
- Support us via PayPal Donation
- Subscribe to our email newsletters.
- Tell other sysadmins / friends about Us - Share and Like our posts and services
We are thankful for your never ending support.