Install MongoDB Community on Amazon Linux

MongoDB Atlas and AWS

MongoDB Atlas is ahosted MongoDB service on AWS, for launching, running, andmaintaining MongoDB clusters.

Overview

Use this tutorial to install MongoDB 4.2 Community Edition onAmazon Linux from .rpm packages.

This installation guide only supports 64-bit systems. SeeSupported Platforms for more information.

Production Notes

Before deploying MongoDB in a production environment, consider theProduction Notes document.

MongoDB Version

This tutorial installs MongoDB 4.2 Community Edition . Forother versions of MongoDB, refer to the corresponding version of themanual.

Platform Support

MongoDB only provides packages for the following Amazon Linux releases:

  • Amazon Linux 2013.03 or later
  • Amazon Linux 2

See Supported Platforms for moreinformation.

Install MongoDB Community Edition using .rpm Packages

Note

To install a different version of MongoDB, please refer to thatversion’s documentation. To install the previous version, seethe tutorial for version 4.0.

This installation guide only supports 64-bit systems. SeeSupported Platforms for more information.

  • Amazon Linux 2
  • Amazon Linux

Configure the package management system (yum).

Create a /etc/yum.repos.d/mongodb-org-4.2.repo file so thatyou can install MongoDB directly using yum:

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

You can also download the .rpm files directly from theMongoDB repository. Downloads are organized by Amazon Linuxversion (e.g. 2), then MongoDBrelease version(e.g. 4.2), then architecture (e.g. x86_64).Odd-numbered MongoDB release versions, such as 4.1, are developmentversions and are unsuitable for production deployment.

Install the MongoDB packages.

To install the latest stable version of MongoDB, issue the followingcommand:

  1. sudo yum install -y mongodb-org

Alternatively, to install a specific release of MongoDB, specify eachcomponent package individually and append the version number to thepackage name, as in the following example:

  1. sudo yum install -y mongodb-org-4.2.1 mongodb-org-server-4.2.1 mongodb-org-shell-4.2.1 mongodb-org-mongos-4.2.1 mongodb-org-tools-4.2.1

You can specify any available version of MongoDB. However yumupgrades the packages when a newer version becomes available. Toprevent unintended upgrades, pin the package. To pin a package, addthe following exclude directive to your /etc/yum.conf file:

  1. exclude=mongodb-org,mongodb-org-server,mongodb-org-shell,mongodb-org-mongos,mongodb-org-tools

Configure the package management system (yum).

Create a /etc/yum.repos.d/mongodb-org-4.2.repo file so thatyou can install MongoDB directly using yum:

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

You can also download the .rpm files directly from theMongoDB repository. Downloads are organized by Amazon Linuxversion (e.g. 2013.03), then MongoDBrelease version(e.g. 4.2), then architecture (e.g. x86_64).Odd-numbered MongoDB release versions, such as 4.1, are developmentversions and are unsuitable for production deployment.

Install the MongoDB packages.

To install the latest stable version of MongoDB, issue the followingcommand:

  1. sudo yum install -y mongodb-org

Alternatively, to install a specific release of MongoDB, specify eachcomponent package individually and append the version number to thepackage name, as in the following example:

  1. sudo yum install -y mongodb-org-4.2.1 mongodb-org-server-4.2.1 mongodb-org-shell-4.2.1 mongodb-org-mongos-4.2.1 mongodb-org-tools-4.2.1

You can specify any available version of MongoDB. However yumupgrades the packages when a newer version becomes available. Toprevent unintended upgrades, pin the package. To pin a package, addthe following exclude directive to your /etc/yum.conf file:

  1. exclude=mongodb-org,mongodb-org-server,mongodb-org-shell,mongodb-org-mongos,mongodb-org-tools

Run MongoDB Community Edition

  • Production Notes
  • Before deploying MongoDB in a production environment, consider theProduction Notes document.
  • ulimit Considerations
  • Most Unix-like operating systems limit the system resources that asession may use. These limits may negatively impact MongoDB operation.See UNIX ulimit Settings for more information.
  • Directories
  • By default, MongoDB instance stores:

    • its data files in /var/lib/mongo
    • its log files in /var/log/mongodbIf you installed via the package manager, the directories are createdduring the installation.

If you installed manually by downloading the tarballs, you can createthe directories using mkdir -p <directory> or sudo mkdir -p<directory> depending on the user that will run MongoDB. (See yourlinux man pages for information on mkdir and sudo.)

By default, MongoDB runs using the mongod user account. If youchange the user that runs the MongoDB process, you must also modifythe permission to the /var/lib/mongo and /var/log/mongodbdirectories to give this user access to these directories.

To specify a different log file directory and data file directory, editthe systemLog.path and storage.dbPath settings inthe /etc/mongod.conf. Ensure that the user running MongoDB hasaccess to these directories.

Start MongoDB.

You can start the mongod process by issuing the followingcommand:

  1. sudo service mongod start

Verify that MongoDB has started successfully

You can verify that the mongod process has startedsuccessfully by checking the contents of the log file at/var/log/mongodb/mongod.logfor a line reading

  1. [initandlisten] waiting for connections on port <port>

where <port> is the port configured in /etc/mongod.conf, 27017 by default.

You can optionally ensure that MongoDB will start following a systemreboot by issuing the following command:

  1. sudo chkconfig mongod on

Stop MongoDB.

As needed, you can stop the mongod process by issuing thefollowing command:

  1. sudo service mongod stop

Restart MongoDB.

You can restart the mongod process by issuing the followingcommand:

  1. sudo service mongod restart

You can follow the state of the process for errors or important messagesby watching the output in the /var/log/mongodb/mongod.log file.

Begin using MongoDB.

Start a mongo shell on the same host machine as themongod. You can run the mongo shellwithout any command-line options to connect to amongod that is running on your localhost with defaultport 27017:

  1. mongo

For more information on connecting using the mongoshell, such as to connect to a mongod instance runningon a different host and/or port, see The mongo Shell.

To help you start using MongoDB, MongoDB provides GettingStarted Guides in various driver editions. SeeGetting Started for the available editions.

Uninstall MongoDB Community Edition

To completely remove MongoDB from a system, you must remove the MongoDBapplications themselves, the configuration files, and any directories containingdata and logs. The following section guides you through the necessary steps.

Warning

This process will completely remove MongoDB, its configuration, and _all_databases. This process is not reversible, so ensure that all of yourconfiguration and data is backed up before proceeding.

Stop MongoDB.

Stop the mongod process by issuing the following command:

  1. sudo service mongod stop

Remove Packages.

Remove any MongoDB packages that you had previously installed.

  1. sudo yum erase $(sudo rpm -qa | grep mongodb-org)

Remove Data Directories.

Remove MongoDB databases and log files.

  1. sudo rm -r /var/log/mongodb
  2. sudo rm -r /var/lib/mongo

Additional Information

Packages

MongoDB provides officially supported packages in their own repository:

Package NameDescription
mongodb-orgA metapackage that will automatically installthe four component packages listed below.
mongodb-org-serverContains the mongod daemon, associated initscript, and a configuration file (/etc/mongod.conf). Youcan use the initialization script to start mongodwith the configuration file. For details, see Run MongoDBCommunity Edition.
mongodb-org-mongosContains the mongos daemon.
mongodb-org-shellContains the mongo shell.
mongodb-org-toolsContains the following MongoDB tools: mongoimportbsondump, mongodump, mongoexport,mongofiles,mongorestore, mongostat,and mongotop.