Steps to Install MongoDB on Amazon Linux

In this tutorial, we will explain Steps to Install MongoDB on Amazon Linux. Before starting with the tutorial lets see what are the dependency to install MongoDB on Amazon Linux.

Steps to install MongoDB on Amazon Linux

Prerequisites to Install MongoDB on Amazon Linux EC2

  • You must have an EC2 instance running with Amazon Linux
  • Need root/sudo access to this EC2 instance.

These below steps support for EC2 Amazon Linux:

  • Amazon Linux 2
  • Amazon Linux (2013.03 or later)
MongoDB only supports the 64-bit versions of these platforms for other support platform checkout this link

Let’s see what are the steps to Install MongoDB on Amazon Linux .

Step 1: Create YUM repository for installing MongoDB

– on Amazon Linux (2013.03 or later)

Create a yum repository file for MongoDB to which will install with yum command. /etc/yum.repos.d/mongodb-org-4.2.repo file.

MongoDB Yum Repository for Amazon Linux (2013.03 or later)

[ec2-user@MongoDB-Server ~]$ sudo vi /etc/yum.repos.d/mongodb-org-4.2.repo
#Copy paste below content and save it
[mongodb-org-4.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2013.03/mongodb-org/4.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc

MongoDB Yum Repository for Amazon Linux 2

[mongodb-org-4.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2/mongodb-org/4.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc

Another method to install MongoDB is via .rpm files, you can directly download the MongoDB repository from the official download page (https://repo.mongodb.org/yum/redhat/).

Step 2: YUM Command to install MongoDB for Amazon Linux

Command:

$ sudo yum install -y mongodb-org

Demo Output:

[ec2-user@MongoDB-Server ~]$ sudo yum install -y mongodb-org
Loaded plugins: priorities, update-motd, upgrade-helper
amzn-main                                                                                                        | 2.1 kB  00:00:00
amzn-updates                                                                                                     | 2.5 kB  00:00:00
mongodb-org-4.2                                                                                                  | 2.5 kB  00:00:00
(1/6): amzn-updates/latest/group_gz                                                                              | 4.4 kB  00:00:00
(2/6): amzn-main/latest/group_gz                                                                                 | 4.4 kB  00:00:00
(3/6): amzn-updates/latest/updateinfo                                                                            | 629 kB  00:00:00
(4/6): mongodb-org-4.2/primary_db                                                                                |  22 kB  00:00:00
(5/6): amzn-main/latest/primary_db                                                                               | 4.0 MB  00:00:00
(6/6): amzn-updates/latest/primary_db                                                                            | 2.9 MB  00:00:01
Resolving Dependencies
--> Running transaction check
---> Package mongodb-org.x86_64 0:4.2.3-1.amzn1 will be installed
--> Processing Dependency: mongodb-org-tools = 4.2.3 for package: mongodb-org-4.2.3-1.amzn1.x86_64
--> Processing Dependency: mongodb-org-shell = 4.2.3 for package: mongodb-org-4.2.3-1.amzn1.x86_64
--> Processing Dependency: mongodb-org-server = 4.2.3 for package: mongodb-org-4.2.3-1.amzn1.x86_64
--> Processing Dependency: mongodb-org-mongos = 4.2.3 for package: mongodb-org-4.2.3-1.amzn1.x86_64
--> Running transaction check
---> Package mongodb-org-mongos.x86_64 0:4.2.3-1.amzn1 will be installed
---> Package mongodb-org-server.x86_64 0:4.2.3-1.amzn1 will be installed
---> Package mongodb-org-shell.x86_64 0:4.2.3-1.amzn1 will be installed
---> Package mongodb-org-tools.x86_64 0:4.2.3-1.amzn1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================================
 Package                              Arch                     Version                          Repository                         Size
========================================================================================================================================
Installing:
 mongodb-org                          x86_64                   4.2.3-1.amzn1                    mongodb-org-4.2                   5.8 k
Installing for dependencies:
 mongodb-org-mongos                   x86_64                   4.2.3-1.amzn1                    mongodb-org-4.2                    14 M
 mongodb-org-server                   x86_64                   4.2.3-1.amzn1                    mongodb-org-4.2                    25 M
 mongodb-org-shell                    x86_64                   4.2.3-1.amzn1                    mongodb-org-4.2                    17 M
 mongodb-org-tools                    x86_64                   4.2.3-1.amzn1                    mongodb-org-4.2                    62 M

Transaction Summary
========================================================================================================================================
Install  1 Package (+4 Dependent packages)

Total download size: 118 M
Installed size: 281 M
Downloading packages:
warning: /var/cache/yum/x86_64/latest/mongodb-org-4.2/packages/mongodb-org-4.2.3-1.amzn1.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID 058f8b6b: NOKEY
Public key for mongodb-org-4.2.3-1.amzn1.x86_64.rpm is not installed
(1/5): mongodb-org-4.2.3-1.amzn1.x86_64.rpm                                                                      | 5.8 kB  00:00:00
(2/5): mongodb-org-mongos-4.2.3-1.amzn1.x86_64.rpm                                                               |  14 MB  00:00:00
(3/5): mongodb-org-server-4.2.3-1.amzn1.x86_64.rpm                                                               |  25 MB  00:00:00
(4/5): mongodb-org-tools-4.2.3-1.amzn1.x86_64.rpm                                                                |  62 MB  00:00:01
(5/5): mongodb-org-shell-4.2.3-1.amzn1.x86_64.rpm                                                                |  17 MB  00:00:02
----------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                    34 MB/s | 118 MB  00:00:03
Retrieving key from https://www.mongodb.org/static/pgp/server-4.2.asc
Importing GPG key 0x058F8B6B:
 Userid     : "MongoDB 4.2 Release Signing Key <packaging@mongodb.com>"
 Fingerprint: e162 f504 a20c df15 827f 718d 4b7c 549a 058f 8b6b
 From       : https://www.mongodb.org/static/pgp/server-4.2.asc
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mongodb-org-server-4.2.3-1.amzn1.x86_64                                                                              1/5
  Installing : mongodb-org-mongos-4.2.3-1.amzn1.x86_64                                                                              2/5
  Installing : mongodb-org-tools-4.2.3-1.amzn1.x86_64                                                                               3/5
  Installing : mongodb-org-shell-4.2.3-1.amzn1.x86_64                                                                               4/5
  Installing : mongodb-org-4.2.3-1.amzn1.x86_64                                                                                     5/5
  Verifying  : mongodb-org-shell-4.2.3-1.amzn1.x86_64                                                                               1/5
  Verifying  : mongodb-org-tools-4.2.3-1.amzn1.x86_64                                                                               2/5
  Verifying  : mongodb-org-mongos-4.2.3-1.amzn1.x86_64                                                                              3/5
  Verifying  : mongodb-org-server-4.2.3-1.amzn1.x86_64                                                                              4/5
  Verifying  : mongodb-org-4.2.3-1.amzn1.x86_64                                                                                     5/5

Installed:
  mongodb-org.x86_64 0:4.2.3-1.amzn1

Dependency Installed:
  mongodb-org-mongos.x86_64 0:4.2.3-1.amzn1    mongodb-org-server.x86_64 0:4.2.3-1.amzn1    mongodb-org-shell.x86_64 0:4.2.3-1.amzn1
  mongodb-org-tools.x86_64 0:4.2.3-1.amzn1

Complete!
[ec2-user@MongoDB-Server ~]$

You can also download MongoDB from below link:

MongoDB Download Page

Default directories for MongoDB

Data files in /var/lib/mongo

Log files in /var/log/mongodb

Step 3: Recommended Linux ulimit Settings:

limit fsize unlimited unlimited    # (file size)
limit cpu unlimited unlimited      # (cpu time)
limit as unlimited unlimited       # (virtual memory size)
limit memlock unlimited unlimited  # (locked-in-memory size)
limit nofile 64000 64000           # (open files)
limit nproc 64000 64000            # (processes/threads)

You can check the ulimit set on your Linux system with the below command.

$ ulimit -a

How to change ulimit values in Linux

Whenever you update the ulimit settings restart the service, to ensure that the changes take effect on system for the MongoDB service “sudo service mongod start

Step 4: Command to start MongoDB service

$ sudo service mongod start

Demo Output:

[ec2-user@MongoDB-Server ~]$ sudo service mongod start
Starting mongod:                                           [  OK  ]
[ec2-user@MongoDB-Server ~]$

Step 5: Command to auto-start MongoDB service on boot:

$ sudo chkconfig mongod on

Demo Output

[ec2-user@MongoDB-Server ~]$ sudo chkconfig | grep mongod 
mongod          0:off   1:off   2:off   3:off   4:off   5:off   6:off
[ec2-user@MongoDB-Server ~]$
[ec2-user@MongoDB-Server ~]$ sudo chkconfig mongod on
[ec2-user@MongoDB-Server ~]$
[ec2-user@MongoDB-Server ~]$ sudo chkconfig | grep mongod
mongod          0:off   1:off   2:on    3:on    4:on    5:on    6:off
[ec2-user@MongoDB-Server ~]$

Command to Start | Stop | Restart MongoDB service

$ sudo service mongod start

$ sudo service mongod stop

$ sudo service mongod restart

Steps 6: Start MongoDB Shell

$ mongo

This is the end of the tutorial we explained the steps to Install MongoDB on Amazon Linux.


Thanks for reading this article, checkout below article you’ll like it.

How to create YUM Repository from ISO or Online

Steps to Install MySQL8 on CentOS or Amazon Linux

Steps to Install Docker on Ubuntu

Steps to Install PIP in Amazon Linux or RHEL or CentOS