How To Install and Configure MongoDB on CentOS 7


In this article, we will learn how to install and configure the MongoDB on CentOS 7, MongoDB which is an open-source and free database (is a NoSQL database), means it is a document-oriented database, it stores the document which is similar structurally to JSON (in MongoDB it is called as BSON) with high availability, performance and auto scaling. Unlike the RDBMS, it doesn’t need any predefined database schema for adding the data to the database tables. We can alter the schema any point of time without disturbing the existing schema.

Prerequisites

  • Centos 7 installed on the Linux Machine.
  • A user with root user privileges.

Adding the MongoDB Repository

By default, the MongoDB repository is not in the CentOS 7 repository, we needed to add the MongoDB repository to the local machine.

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

Once we add the MongoDB repository to the local machine, we will the system so that the yum will pull check the repository information.

# yum update
Output:
Loaded plugins: fastestmirror
base                                           | 3.6 kB 00:00
extras                                         | 3.4 kB 00:00
mongodb-org-3.2                               | 2.5 kB 00:00
updates                                        | 3.4 kB 00:00
mongodb-org-3.2/7/primary_db | 54 kB 00:01
Determining fastest mirrors
* base: mirror.dhakacom.com
* extras: mirror.dhakacom.com
* updates: mirror.dhakacom.com
No packages marked for update

Installing the MongoDB

As the repository details are updated on the local machine, we will now install the MongoDB using the yum command.

Below is the command to install the MongoDB.

# yum install mongodb-org
Output:
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.dhakacom.com
* extras: mirror.dhakacom.com
* updates: mirror.dhakacom.com
Resolving Dependencies
--> Running transaction check
---> Package mongodb-org.x86_64 0:3.2.11-1.el7 will be installed
…
…
…
---> Package mongodb-org-mongos.x86_64 0:3.2.11-1.el7 will be installed
---> Package mongodb-org-server.x86_64 0:3.2.11-1.el7 will be installed
---> Package mongodb-org-shell.x86_64 0:3.2.11-1.el7 will be installed
---> Package mongodb-org-tools.x86_64 0:3.2.11-1.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package                Arch       Version       Repository       Size
================================================================================
Installing:
mongodb-org            x86_64    3.2.11-1.el7   mongodb-org-3.2 5.8 k
Installing for dependencies:
mongodb-org-mongos    x86_64     3.2.11-1.el7   mongodb-org-3.2 5.6 M
mongodb-org-server    x86_64     3.2.11-1.el7   mongodb-org-3.2 12 M
mongodb-org-shell     x86_64     3.2.11-1.el7   mongodb-org-3.2 6.7 M
mongodb-org-tools     x86_64     3.2.11-1.el7   mongodb-org-3.2 41 M
Transaction Summary
================================================================================
Install 1 Package (+4 Dependent packages)
Total download size: 66 M
Installed size: 201 M
Is this ok [y/d/N]:y
Downloading packages:
warning: /var/cache/yum/x86_64/7/mongodb-org-3.2/packages/mongodb-org-3.2.11-1.el7.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID ea312927: NOKEY
Public key for mongodb-org-3.2.11-1.el7.x86_64.rpm is not installed
(1/5): mongodb-org-3.2.11-1.el7.x86_64.rpm | 5.8 kB 00:01
(2/5): mongodb-org-mongos-3.2.11-1.el7.x86_64.rpm | 5.6 MB 00:01
(3/5): mongodb-org-shell-3.2.11-1.el7.x86_64.rpm | 6.7 MB 00:00
(4/5): mongodb-org-server-3.2.11-1.el7.x86_64.rpm | 12 MB 00:06
(5/5): mongodb-org-tools-3.2.11-1.el7.x86_64.rpm | 41 MB 00:06
--------------------------------------------------------------------------------
Total 7.3 MB/s | 66 MB 00:08
Retrieving key from https://www.mongodb.org/static/pgp/server-3.2.asc
Importing GPG key 0xEA312927:
Userid : "MongoDB 3.2 Release Signing Key <packaging@mongodb.com>"
Fingerprint: 42f3 e95a 2c4f 0827 9c49 60ad d68f a50f ea31 2927
From : https://www.mongodb.org/static/pgp/server-3.2.asc
Is this ok [y/N]:y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
   Installing : mongodb-org-server-3.2.11-1.el7.x86_64 1/5
   Installing : mongodb-org-mongos-3.2.11-1.el7.x86_64 2/5
   Installing : mongodb-org-tools-3.2.11-1.el7.x86_64 3/5
   Installing : mongodb-org-shell-3.2.11-1.el7.x86_64 4/5
   Installing : mongodb-org-3.2.11-1.el7.x86_64 5/5
   Verifying : mongodb-org-shell-3.2.11-1.el7.x86_64 1/5
   Verifying : mongodb-org-tools-3.2.11-1.el7.x86_64 2/5
   Verifying : mongodb-org-mongos-3.2.11-1.el7.x86_64 3/5
   Verifying : mongodb-org-server-3.2.11-1.el7.x86_64 4/5
   Verifying : mongodb-org-3.2.11-1.el7.x86_64 5/5
Installed:
   mongodb-org.x86_64 0:3.2.11-1.el7
Dependency Installed:
   mongodb-org-mongos.x86_64 0:3.2.11-1.el7
   mongodb-org-server.x86_64 0:3.2.11-1.el7
   mongodb-org-shell.x86_64 0:3.2.11-1.el7
   mongodb-org-tools.x86_64 0:3.2.11-1.el7

Complete!

Once MongoDD is installed, we will now start the MongoDB services.

Below is the command to start the MongoDB services –

# systemctl start mongod

We run the below command to check the status of the MongoDB services.

# systemctl status mongod

Output:
mongod.service - SYSV: Mongo is a scalable, document-oriented database.
Loaded: loaded (/etc/rc.d/init.d/mongod)
Active: active (running) since Fri 2016-11-25 14:09:25 IST; 12s ago
Docs: man:systemd-sysv-generator(8)
Process: 9901 ExecStart=/etc/rc.d/init.d/mongod start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/mongod.service
└─9912 /usr/bin/mongod -f /etc/mongod.conf
Nov 25 14:09:25 localhost.localdomain systemd[1]: Starting SYSV: Mongo is a s...
Nov 25 14:09:25 localhost.localdomain runuser[9908]: pam_unix(runuser:session...
Nov 25 14:09:25 localhost.localdomain runuser[9908]: pam_unix(runuser:session...
Nov 25 14:09:25 localhost.localdomain mongod[9901]: Starting mongod: [ OK ]
Nov 25 14:09:25 localhost.localdomain systemd[1]: Started SYSV: Mongo is a sc...Hint: Some lines were ellipsized, use -l to show in full.

Reload the MongoDB configuration

# systemctl reload mongod

Stop the MongoDB services

# systemctl stop mongod

Configuring the MongoDB No of Processes

By default, when we run the Mongo the no of processes is too low to 4096, so MongoDB will show the below error –

# mongo
Output:
MongoDB shell version: 3.2.11
connecting to: test
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:
2016-11-25T14:09:25.553+0530 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 4096 processes, 64000 files. Number of processes should be at least 32000 : 0.5 times number of files.
2016-11-25T14:09:25.553+0530 I CONTROL [initandlisten]

To fix the no of process we needed to edit the file “20-nproc.conf” which is at “/etc/security/limits.d/”

# vi /etc/security/limits.d/20-nproc.conf
Output:
# Default limit for number of user's processes to prevent
# accidental fork bombs.
# See rhbz #432903 for reasoning.
* soft nproc 4096
root soft nproc unlimited

We needed to change the 4096 to 32000

* soft nproc 32000

Once we change the value to 32000 we needed to restart the MongoDB services, below is the command to restart the MongoDB services.

# systemctl restart mongod

Creating the MongoDB Administrator User

Here we will create one user with username admin and with password “password123”, once we created the user we will check for the user list in the database.

# mongo
Output:
MongoDB shell version: 3.2.11
connecting to: test
> use admin
switched to db admin
> db.createUser(
...    {
...       user: "admin",
...       pwd: "password123",
...       roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
...    }
... )
Successfully added user: {
   "user" : "admin",
   "roles" : [
      {
         "role" : "userAdminAnyDatabase",
         "db" : "admin"
      }
   ]
}
> show users;
{
   "_id" : "admin.admin",
   "user" : "admin",
   "db" : "admin",
   "roles" : [
      {
         "role" : "userAdminAnyDatabase",
         "db" : "admin"
      }
   ]
}
>

In this article, we have learnt – How to add MongoDB repository to the local machine and install the MongoDB and we also learnt about how to change or increase the no. of process from 4096 to 32000 in the MongoDB configuration and how to create a admin user.

Updated on: 20-Jan-2020

326 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements