How to Install MonogDB on CentOS 7

How To Install MongoDB Community Edition on CentOS 7

MongoDB is classified as a NoSQL database which is different than traditional table-based SQL databases like Oracle, MariaDB, MySQL and PostgreSQL. MongoDB is a free and open-source document database. Data is stored as JSON-like documents where fields can vary from document to document. It does not require a predefined schema and data structure can be changed over time. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License (SSPL). In this article, we will discuss how to install and configure MongoDB Community Edition on CentOS 7, the source is using official MongoDB repositories.

Installing MongoDB Community Edition

There are any MongoDB Community Edition distribution for any platform, For Linux, OS X, and Windows. For Linux distribution itself, it has been provided MongoDB for various Linux systems, namey : Red Hat, SUSE, Amazon, Ubuntu and Debian. While this article is composed, the latest version of MongoDB available from the official MongoDB repositories is version 4.2.

There are several steps to be followed to install MongoDB on CentOS 7 environment.

  1. Prerequisites
  2. Enabling MongoDB repository
  3. Installing MongoDB
  4. Starting MongoDB
  5. Verifying MongoDB Installation

1. Prerequisite

Before installing MongoDB on our CentOS7, we have to prepare the environment first. It requires a user with sudo privileges, sufficient space and the last update of CentOS repository. In our enviroment we have all the necessary requirements. To ensure our CentOS 7 is running on the last update, we have to submit command line as below :

[ramans@localhost ~]$ sudo yum update

2. Enabling MongoDB repository

To get the latest and most authentic MongoDB source, we must get it from the original source, we will go to the MongoDB repository from its own source at https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/#configure-the-package-management-system-yum.

MongoDB repository has to be added to our CentOS 7 repos, create the file /etc/yum.repos.d/mongodb-org-4.2.repo with following entries :

[ramans@localhost yum.repos.d]$ sudo vi /etc/yum.repos.d/mongodb-org-4.2.repo
[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.asc
Installing MongDB on CentOS 7 | enabling repostiories
Enabling MongoDB repository

3. Installing MongoDB

Enabling MongoDB repositories means that we have made our CentOS 7 could access MongoDB repositories. The next step is stalling MongoDB based on this repostitories. Installing MongoDB by submiting command line below :

[ramans@localhost yum.repos.d]$ sudo yum install -y mongodb-org
Installing MongDB on CentOS 7
Installing MongDB on CentOS 7

4. Starting MongoDB

After MongoDB installation is completed, then start the MongoDB daemon and enable it to start on boot. This task can be done by executing command line below :

[ramans@localhost yum.repos.d]$ sudo systemctl start mongod
[ramans@localhost yum.repos.d]$ sudo systemctl enable mongod

Theris no error detected after MongoDB daemon started.

5. Verifying MongoDB Installation

Verifying installation result will be obtained by connect to MongDB and do query related to its properties. Use the command line below to connect to MongoDB :

[ramans@localhost yum.repos.d]$ mongo

Output will be as follwing below :

MongoDB shell version v4.2.2
 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
 Implicit session: session { "id" : UUID("4d14fef1-f478-4464-be87-fe3eb049dda7") }
 MongoDB server version: 4.2.2
 Welcome to the MongoDB shell.
 For interactive help, type "help".
 For more comprehensive documentation, see
     http://docs.mongodb.org/
 Questions? Try the support group
     http://groups.google.com/group/mongodb-user
 Server has startup warnings: 
 2020-01-02T20:50:43.986+0700 I  CONTROL  [initandlisten] 
 2020-01-02T20:50:43.986+0700 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
 2020-01-02T20:50:43.986+0700 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
 2020-01-02T20:50:43.986+0700 I  CONTROL  [initandlisten] 
 2020-01-02T20:50:43.987+0700 I  CONTROL  [initandlisten] 
 2020-01-02T20:50:43.987+0700 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
 2020-01-02T20:50:43.987+0700 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
 2020-01-02T20:50:43.987+0700 I  CONTROL  [initandlisten] 
 2020-01-02T20:50:43.987+0700 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
 2020-01-02T20:50:43.987+0700 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
 2020-01-02T20:50:43.987+0700 I  CONTROL  [initandlisten] 
 Enable MongoDB's free cloud-based monitoring service, which will then receive and display
 metrics about your deployment (disk utilization, CPU, operation statistics, etc).
 The monitoring data will be available on a MongoDB website with a unique URL accessible to you
 and anyone you share the URL with. MongoDB may use this information to make product
 improvements and to suggest MongoDB products and deployment options to you.
 To enable free monitoring, run the following command: db.enableFreeMonitoring()
 To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
 >

Querying the MongoDB version by submitting command db.version inside MongoDB console.

   > db.version()
   4.2.2
MongoDB version 4.2.2

MongoDB Configuration Management

MongoDB uses /etc/mongod.conf file for configuring all aspects related to the database operation. At this file, we can configure : system log, storage, process management, network interfaces, security, operation profiling, replication, shariding, snmp, auditlog and enterprise version.

Using MongoDB Example

Here’s the example of how to connect and query the MongoDB database. In this scenario, we are using Adminmongo user to connect and then switch to admin user and list all use exist on MongoDB.

[ramans@localhost yum.repos.d]$ mongo -u Adminmongo -p --authenticationDatabase admin
 MongoDB shell version v4.2.2
 Enter password: 
 connecting to: mongodb://127.0.0.1:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
 Implicit session: session { "id" : UUID("3a3432b5-5816-4db5-a087-2d4c843fcf67") }
 MongoDB server version: 4.2.2
 Server has startup warnings: 
 2020-01-02T20:50:43.986+0700 I  CONTROL  [initandlisten] 
 2020-01-02T20:50:43.986+0700 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
 2020-01-02T20:50:43.986+0700 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
 2020-01-02T20:50:43.986+0700 I  CONTROL  [initandlisten] 
 2020-01-02T20:50:43.987+0700 I  CONTROL  [initandlisten] 
 2020-01-02T20:50:43.987+0700 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
 2020-01-02T20:50:43.987+0700 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
 2020-01-02T20:50:43.987+0700 I  CONTROL  [initandlisten] 
 2020-01-02T20:50:43.987+0700 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
 2020-01-02T20:50:43.987+0700 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
 2020-01-02T20:50:43.987+0700 I  CONTROL  [initandlisten] 
 Enable MongoDB's free cloud-based monitoring service, which will then receive and display
 metrics about your deployment (disk utilization, CPU, operation statistics, etc).
 The monitoring data will be available on a MongoDB website with a unique URL accessible to you
 and anyone you share the URL with. MongoDB may use this information to make product
 improvements and to suggest MongoDB products and deployment options to you.
 To enable free monitoring, run the following command: db.enableFreeMonitoring()
 To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
   use admin
   switched to db admin
   show users
   {
       "_id" : "admin.Adminmongo",
       "userId" : UUID("02018b78-0453-4e9b-adf3-0920af9a9d32"),
       "user" : "Adminmongo",
       "db" : "admin",
       "roles" : [
           {
               "role" : "userAdminAnyDatabase",
               "db" : "admin"
           }
       ],
       "mechanisms" : [
           "SCRAM-SHA-1",
           "SCRAM-SHA-256"
       ]
   } 

Note : MongoDB logo is owned by MongoDB Inc.

Share this article via :

Leave a Reply

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