Installing MongoDB on Ubuntu 20.04

Select distribution:
Traducciones al Español
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Create a Linode account to try this guide with a $ credit.
This credit will be applied to any valid services used during your first  days.

MongoDB is a NoSQL database that provides an alternative to Relational DataBase Management System (RDBMS) applications such as MySQL and MariaDB. This guide introduces MongoDB and explains how to install the latest release on Ubuntu 20.04.

What is MongoDB?

MongoDB is a document-oriented database. It stores data in a more flexible manner than a traditional relational database. It aligns well with object-oriented models and is a good choice for unstructured or semi-structured data. MongoDB is considered easy to learn and use compared to SQL-based databases. It is designed as a distributed database for easy scalability and additional redundancy.

MongoDB is considered a NoSQL database because it does not use the Structured Query Language (SQL) to store and retrieve data. Administrators typically use the MongoDB Query API instead to modify documents and retrieve document data. They can also base queries on regular expressions or JavaScript functions. Additionally, MongoDB does not add an entry as a row inside a table. It stores data in documents using the Binary JSON (BSON) file format. BSON is a variation of JavaScript Object Notation (JSON) format that encodes a sequence of attribute-value pairs into binary data. This format reduces storage space and increases efficiency at the expense of readability.

The format of MongoDB documents is relatively unstructured, so each document can be different. The only real restriction is that a document must consist of a list of fields, each containing a key-value pair. The document schema does not have to be defined beforehand and users can adjust the format at any time. MongoDB can group multiple documents together into a collection. Because MongoDB is not a relational database, there are no implied connections between any documents or collections.

The MongoDB Community Edition, which is designed for individuals or small businesses, is free to use. The application can be used under the Server Side Public License (SSPL), and the source code is freely available. However, the SSPL differs from standard open source licenses and is somewhat more restrictive. Users are advised to thoroughly understand the license before developing any software around it. MongoDB is available for most Linux distributions along with Windows and macOS.

For a more in-depth introduction to MongoDB, including a comparison between MongoDB and SQL, see the Linode Introduction to MongoDB guide.

Before You Begin

  1. If you have not already done so, create a Linode account and Compute Instance. See our Getting Started with Linode and Creating a Compute Instance guides.

  2. Follow our Setting Up and Securing a Compute Instance guide to update your system. You may also wish to set the timezone, configure your hostname, create a limited user account, and harden SSH access.

Note
This guide is written for a non-root user. Commands that require elevated privileges are prefixed with sudo. If you are not familiar with the sudo command, see the Users and Groups guide.

How to Install MongoDB

The MongoDB installation process is fairly straightforward. First add the official MongoDB package and then use apt to install the application. To install MongoDB, follow these steps.

Note
The mongodb package provided with the standard Ubuntu repository is not the official package and is often not up to date. It might not work properly with other applications. The instructions in this section demonstrate how to install the official MongoDB package. To verify whether the unofficial mongodb package is installed, run the command which mongodb. If Ubuntu displays a path to the application, uninstall it using apt.
  1. Install the gnupg utility.

    sudo apt-get install gnupg
  2. Import the public MongoDB GPG signing key. Verify the key is imported successfully.

    wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
    OK
  3. Add details about the official MongoDB repository to the list of Ubuntu packages.

    echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
  4. Update the list of packages using apt.

    sudo apt-get update
  5. Install the latest release of MongoDB.

    sudo apt-get install -y mongodb-org
    Note

    To install a specific release of MongoDB, use the command following command, replacing release with the actual release to install.

    sudo apt-get install -y mongodb-org=release mongodb-org-database=release mongodb-org-server=release mongodb-org-mongos=release mongodb-org-tools=release
  6. Optional With the current configuration, the apt-get command always upgrades MongoDB whenever a new release becomes available. To prevent automatic upgrades, run the following commands.

    echo "mongodb-org hold" | sudo dpkg --set-selections
    echo "mongodb-org-database hold" | sudo dpkg --set-selections
    echo "mongodb-org-server hold" | sudo dpkg --set-selections
    echo "mongodb-mongosh hold" | sudo dpkg --set-selections
    echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
    echo "mongodb-org-tools hold" | sudo dpkg --set-selections

How to Run MongoDB

The MongoDB task is typically controlled using the systemctl tools. To start and enable MongoDB, follow these steps.

  1. Reload the systemctl daemon.

    sudo systemctl daemon-reload
  2. Start the mongod process using systemctl start.

    sudo systemctl start mongod
  3. Use systemctl status to ensure the MongoDB service is active.

    sudo systemctl status mongod
    mongod.service - MongoDB Database Server
    Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
    Active: active (running) since Mon 2023-01-16 12:08:37 UTC; 1min 0s ago
  4. To configure Ubuntu to launch MongoDB at system boot time, enter the following command.

    sudo systemctl enable mongod
    Created symlink /etc/systemd/system/multi-user.target.wants/mongod.service → /lib/systemd/system/mongod.service.
  5. Optional If necessary, stop and restart MongoDB using the following commands.

    sudo systemctl stop mongod
    sudo systemctl restart mongod
  6. To confirm MongoDB is working properly, enter mongosh to access the shell. Because authentication is not yet enabled, no password is required.

    Note
    Without additional parameters, this command connects to the MongoDB instance running on default port 27017 on the local server. If network access is enabled, or if MongoDB has been moved to a different port, append --host 127.0.0.1:27017 to the command.
    mongosh
    Connecting to:		mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.2
    Using MongoDB:		6.0.3
    Using Mongosh:		1.6.2

How to Configure MongoDB

The application settings for MongoDB are stored in the /etc/mongod.conf file. Most of these values can remain at the default values. However, it is important to enable user authentication for better security. To use MongoDB remotely, further changes must be made. For a full list of configuration file options, see the MongoDB Configuration File documentation.

How to Enable Authentication on MongoDB

To enable authentication, create an administrator and then configure the authentication setting in the main configuration file. Follow these steps to secure the database.

  1. Access the MongoDB shell.

    mongosh
  2. Switch to the admin database.

    use admin
    switched to db admin
  3. Use the db.createUser command to create an administrator. Provide a user name for the user and a password for pwd. For better security, use the command passwordPrompt() as the value for the pwd field. This tells MongoDB to prompt for the password. Grant two roles to the administrator, userAdminAnyDatabase and readWriteAnyDatabase. Enter the command in the MongoDB shell as follows, substituting the actual administrator name in place of userAdmin.

    db.createUser(
        {
            user: "userAdmin",
            pwd: passwordPrompt(), // or cleartext password
            roles: [
                { role: "userAdminAnyDatabase", db: "admin" },
                { role: "readWriteAnyDatabase", db: "admin" }
            ]
        }
    )
  4. After entering the command, MongoDB prompts for the administrative password. Enter the password when requested. If the password is accepted, MongoDB returns ok.

    Enter password
    ********{ ok: 1 }
  5. Use the db.adminCommand command to shut down the mongod instance.

    db.adminCommand( { shutdown: 1 } )
  6. Exit the shell.

    exit
  7. Edit the /etc/mongod.conf configuration file to add the configuration settings.

    sudo vi /etc/mongod.conf
  8. Uncomment the line security: and add the line authorization: enabled directly below it. Save and close the file.

    File: /etc/mongod.conf
    1
    2
    3
    4
    
    ...
    security:
        authorization: enabled
    ...
  9. Restart MongoDB.

    sudo systemctl restart mongod
  10. All subsequent attempts to access MongoDB must specify the user and the name of the authentication database. Substitute the name for the administrator account in place of userAdmin and enter the administrator password when prompted. Use the following command to enter the MongoDB shell.

    Note
    This command demonstrates how to authenticate at connection time. To connect first and authenticate afterwards, use the mongosh command without any options. Inside the shell, enter use admin to switch to the admin database. Then use the command db.auth("userAdmin", passwordPrompt()) to authenticate. Substitute the actual user name for userAdmin. Users can enter the MongoDB shell without authentication, but cannot access anything without the proper roles.
    mongosh --authenticationDatabase "admin" -u "userAdmin" -p

How to Enable Remote Access for MongoDB

Unless otherwise specified, MongoDB only provides local access. To enable remote access to the database, make the following changes to the configuration file.

  1. Edit the main MongoDB configuration file.

    sudo vi /etc/mongod.conf
  2. Under the net heading, change the value of bindIp to 0.0.0.0 to allow connections from all addresses. Enter a specific address to limit connectivity to that address.

    File: /etc/mongod.conf
    1
    2
    3
    4
    5
    
    ...
    net:
        port: 27017
        bindIp: 0.0.0.0
    ...
  3. Restart MongoDB.

    sudo systemctl restart mongod
  4. If the ufw firewall is enabled, allow connections to port 27017 through.

    sudo ufw allow 27017

How to Use MongoDB

MongoDB is quite different from other database systems in many ways and can be confusing to RDBMS users. Although this guide focuses on installation and configuration, here are a few additional tips. Before running any commands, use the mongosh command to enter the MongoDB shell. Add the authentication credentials if necessary. For more information on how to use MongoDB, review the MongoDB documentation. MongoDB also provides a short Getting Started tutorial which covers most of the basics.

To see all available databases, use the show dbs command.

show dbs
admin   132.00 KiB
config   60.00 KiB
local    72.00 KiB

To change context to a different database, apply the use command.

use admin
switched to db admin

There is no special command to create a new database. The use command either switches to an existing database or creates a new one, depending on whether the database already exists or not. To create a new database, enter use newdatabase, where newdatabase is the name of the database to add.

use testguidedb

A MongoDB collection has a similar function to a table in a relational database. To create a collection, use the db.collectionname.insertOne function, where collectionname is the name of the collection. Within the {} braces, enter a series of key-value pairs. Separate each pair, except the last one, with a comma. To insert several entries at once, use the insertMany command instead. Use the following format to add data to the testdata collection.

db.testdata.insertOne(
    {Value_one: "30",
    Value_two: "40",
    Value_three: "Test"
    }
)

To retrieve the data from a collection, use the db.find command. The following command returns all the pairs from the testdata collection.

db.testdata.find( {} )
{
    _id: ObjectId("63c56d3dd4cb6182bed3fac3"),
    Value_one: '30',
    Value_two: '40',
    Value_three: 'Test'
}

To list all collections inside a particular database, execute the show collections command.

show collections
testdata

Conclusion

MongoDB is a document-oriented database that permits great flexibility in how data is stored. Each database contains one or more collections, consisting of a free-form series of key-value pairs. To install MongoDB, add the official repository, then use apt to install it. Use systemctl to operate the MongoDB daemon and add authentication or remote access support to the configuration file. For more information, see the MongoDB website.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

This page was originally published on


Your Feedback Is Important

Let us know if this guide was helpful to you.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.
The Disqus commenting system for Linode Docs requires the acceptance of Functional Cookies, which allow us to analyze site usage so we can measure and improve performance. To view and create comments for this article, please update your Cookie Preferences on this website and refresh this web page. Please note: You must have JavaScript enabled in your browser.