How To Install MongoDB 4.4 / 4.2 on CentOS 7 / RHEL 7

0

ADVERTISEMENT

MongoDB is an open-source, document-oriented database designed with both scalability and developer agility in mind. Instead of storing your data in tables and rows as you would with a relational database, in MongoDB, you store JSON-like documents with dynamic schemas.

The goal of MongoDB is to bridge the gap between key-value stores (which are fast and scalable) and relational databases (which have rich functionality).

Here, we will see how to install MongoDB on CentOS 7 / RHEL 7.

THIS DOCUMENT IS ALSO AVAILABLE FOR

Add MongoDB Repository

MongoDB provides packages for CentOS / RHEL operating system from it’s dedicated repository.

Add the MongoDB repository on your system.

vi /etc/yum.repos.d/mongodb.repo

Add the below information to the above repo file.

MongoDB 4.4

[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/7Server/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc

MongoDB 4.2

[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

MongoDB 4.0

[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

Install MongoDB

Once you have added MongoDB repository, run the following command to install MongoDB.

yum -y install mongodb-org
mongob-org (Meta Package that installs below components)
mongodb-org-server Server Package
mongodb-org-mongos Shared Daemon
mongodb-org-shell Command Line Interface
mongodb-org-tools MongoDB Tools (Import, Export, Restore, Dump and other tools)

Post Installation

Admin User

MongoDB doesn’t come with any authentication mechanism to restrict user access. To improve security, create an admin user to manage databases.

Access Control

After creating an admin user, edit the MongoDB configuration file to enable authentication.

vi /etc/mongod.conf

Then, add below lines to the mongod.conf file.

security:
  authorization: enabled

Disable Huge Pages

MongoDB recommends the huge pages be disabled as it causes the performance issue. So, go ahead and disable transparent huge pages.

SELinux

We recommend you to disable SELinux on CentOS 7 / RHEL 7. If you still want to use SELinux, then you need to customize the SELinux policy for MongoDB.

Control MongoDB service

To start MongoDB service, run:

systemctl start mongod

To check the status MongoDB service, run:

systemctl status mongod

Output:

ADVERTISEMENT

 mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2020-08-03 15:05:50 EDT; 2s ago
     Docs: https://docs.mongodb.org/manual
  Process: 1806 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
  Process: 1804 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 1801 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 1800 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
 Main PID: 1809 (mongod)
   CGroup: /system.slice/mongod.service
           └─1809 /usr/bin/mongod -f /etc/mongod.conf

Aug 03 15:05:49 centos7.itzgeek.local systemd[1]: Starting MongoDB Database Server...
Aug 03 15:05:49 centos7.itzgeek.local mongod[1806]: about to fork child process, waiting until server is ready for connections.
Aug 03 15:05:49 centos7.itzgeek.local mongod[1806]: forked process: 1809
Aug 03 15:05:50 centos7.itzgeek.local systemd[1]: Started MongoDB Database Server.

Check the MongoDB version.

mongod --version

Output:

db version v4.4.0
Build Info: {
    "version": "4.4.0",
    "gitVersion": "563487e100c4215e2dce98d0af2a6a5a2d67c5cf",
    "openSSLVersion": "OpenSSL 1.0.1e-fips 11 Feb 2013",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "rhel70",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

Use netstat command to check whether the MongoDB is listening on port 27017.

netstat -antup | grep -i 27017

Output:

tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      1809/mongod

READ: netstat command not found on CentOS 7 / RHEL 7 – Quick Fix

Access MongoDB

Connect to MongoDB shell by entering the following command.

mongo

Output:

[root@centos7 ~]# mongo
MongoDB shell version v4.4.0
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("68e2f93f-f60c-45de-a9ef-4f5dea620a4a") }
MongoDB server version: 4.4.0
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
        https://community.mongodb.com
>

Conclusion

That’s All. I hope you have learned how to install MongoDB on CentOS 7 / RHEL 7. Please share your feedback in the comments section.

You might also like