Skip to content
Bots!
Bots!
  • About
    • Myself
    • আমার দোয়া
  • Bookmarks
    • Bookmarks
    • My OCI Bookmarks
    • Useful Proxmox Commands & Links
    • Learning Nano
    • Useful Sites
    • Useful Virtualbox Command
    • Useful MySQL Command
    • Useful Linux Command
    • BTT-CAS
  • Resources
    • Webinar on Cloud Adoption for Project Managers
  • Photos
  • Videos
  • Downloads
Bots!

Install MongoDB on CentOS 7

Rumi, July 17, 2021

Follow the steps below to install the latest stable version of MongoDB on your CentOS server :

Enabling MongoDB repository

To add the MongoDB repository to your system, open your text editor and create a new YUM repository configuration file named mongodb-org.repo inside the /etc/yum.repos.d/ directory:

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

If you want to install an older version of MongoDB, replace each instance of 4.0 with your preferred version.

Installing MongoDB

Now that the repository is enabled you can install the mongodb-org meta-package using the yum utility:

sudo yum install mongodb-org

During the installation yum will prompt you to import the MongoDB GPG key. Type y and hit Enter.

The following packages will be installed on your system as a part of the mongodb-org package:

mongodb-org-server - The mongod daemon, and corresponding init scripts and configurations.
mongodb-org-mongos - The mongos daemon.
mongodb-org-shell - The mongo shell, an interactive JavaScript interface to MongoDB, used to perform administrative tasks thought the command line.
mongodb-org-tools - Contains several MongoDB tools for importing and exporting data, statistics, as well as other utilities.

Starting MongoDB

Once the installation is completed, start the MongoDB daemon and enable it to start on boot by typing:

sudo systemctl start mongodsudo systemctl enable mongod

Verifying MongoDB Installation

To verify the installation we will connect to the MongoDB database server using the mongo tool and print the server version:

mongo

Once you are inside the MongoDB shell type the following command which will display the MongoDB version:

db.version()

The output will look like the following:

4.0.1

Configuring MongoDB

You can configure your MongoDB instance by editing the /etc/mongod.conf configuration file which is written in YAML .

The default configuration settings are sufficient in most cases. However, for production environments we recommend uncommenting the security section and enabling authorization as shown below:

nano /etc/mongod.conf
security:
authorization: enabled

The authorization option enables Role-Based Access Control (RBAC) that regulates users access to database resources and operations. If this option is disabled each user will have access to any database and will be able to execute any action.

After making changes to the MongoDB configuration file, restart the mongod service:

sudo systemctl restart mongod

To find more information about the configuration options available in MongoDB 4.0 visit the Configuration File Options documentation page.

Creating Administrative MongoDB User

If you enabled the MongoDB authentication, create one administrative MongoDB user that you will use to access and manage your MongoDB instance.

First access the mongo shell with:

mongo

Once you are inside the MongoDB shell type the following command to connect to the admin database:

use admin
switched to db admin

Create a new user named mongoAdmin with the userAdminAnyDatabase role:

db.createUser(
{
user: "mongoAdmin", 
pwd: "changeMe", 
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
Successfully added user: {
"user" : "mongoAdmin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}

You can name the administrative MongoDB user as you want. Exit the mongo shell with:

quit()

To test the changes, access the mongo shell using the administrative user you have previously created:

mongo -u mongoAdmin -p --authenticationDatabase admin
use admin
switched to db admin

Now, print the users with:

show users
{
"_id" : "admin.mongoAdmin",
"user" : "mongoAdmin",
"db" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}

You can also try to access the mongo shell without any arguments ( just type mongo) and see if you can list the users using the same commands as above.

Configurations (Linux) CentOSCentOS 7MongoDB

Post navigation

Previous post
Next post

Leave a Reply Cancel reply

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

Myself…

Hi, I am Hasan T. Emdad Rumi, an IT Project Manager & Consultant, Virtualization & Cloud Savvyfrom Dhaka, Bangladesh. I have prior experience in managing numerous local and international projects in the area of Telco VAS & NMC, National Data Center & PKI Naitonal Root and CA Infrastructure. Also engaged with several Offshore Software Development Team.

Worked with Orascom Telecom-Banglalink, Network Elites as VAS partner, BTRC, BTT (Turkey) , Mango Teleservices Limited and Access to Informaiton (A2I-UNDP)

Currently working at Oracle Corporation as Principal Technology Solution and Cloud Architect.

You can reach me [h.t.emdad at gmail.com] and I will be delighted to exchange my views.

Tags

Apache Bind Cacti CentOS CentOS 6 CentOS 7 Debain Debian Debian 10 Debian 11 Debian 12 DKIM Docker endian icinga iptables Jitsi LAMP Letsencrypt Linux Munin MySQL Nagios Nextcloud NFS nginx pfsense php Postfix powerdns Proxmox RDP squid SSH SSL Ubuntu Ubuntu 16 Ubuntu 18 Ubuntu 20 Varnish virtualbox vpn Webmin XCP-NG zimbra

Topics

Recent Posts

  • Install Jitsi on Ubuntu 22.04 / 22.10 April 30, 2025
  • Key Lessons in life April 26, 2025
  • Create Proxmox Backup Server (PBS) on Debian 12 April 19, 2025
  • Add Physical Drive in Proxmox VM Guest April 19, 2025
  • Mount a drive permanently with fstab in Linux April 16, 2025
  • Proxmox 1:1 NAT routing March 30, 2025
  • Installation steps of WSL – Windows Subsystem for Linux March 8, 2025
  • Enabling Nested Virtualization In Proxmox March 8, 2025
  • How to Modify/Change console/SSH login banner for Proxmox Virtual Environment (Proxmox VE / PVE) March 3, 2025
  • Install Proxmox Backup Server on Debian 12 February 12, 2025

Archives

Top Posts & Pages

  • Install Jitsi on Ubuntu 22.04 / 22.10
©2025 Bots! | WordPress Theme by SuperbThemes