How To Install MongoDB on Ubuntu 22.04

In this guide, we will explain how to install MongoDB on Ubuntu 22.04, providing a scalable NoSQL database solution for your applications and data management.

MongoDB is an open-source NoSQL database that uses a flexible, document-oriented data model. Unlike traditional relational databases that use tables and rows, MongoDB stores data in JSON-like documents with varied structures. This makes it highly adaptable and suitable for applications requiring scalable, high-performance, and flexible data storage solutions.

Prerequisites

Update && Upgrade the system

First, ensure your package list is up to date to avoid any issues with outdated packages

Installation MongoDB

Execute the given command to install the MongoDB package.

Add necessary packages

To install the most recent MongoDB package, you need to add the MongoDB package repository to your sources list file on Ubuntu.

After this next. add MongoDB APT repository to the /etc/apt/sources.list.d/ directory.

Next, include the MongoDB source list file in the /etc/apt/sources.list.d/ directory, which consists of a lone line stating

  • Add all repositories first, then update the packages

Now, proceed to install the mongodb-org meta-package, which includes MongoDB.

Verify the version of MongoDB Installed

Start and Enable MongoDB

  • After installation, the MongoDB service starts and enables

  • Enable it to start when the system rebooted

Manage MongoDB Service

To verify the database's status, make sure the server is actively listening on port 27017.

To check the status

Verify that the database service is active and running. Run the following command

Create MongoDB databases and User

Make sure the MongoDB instance is up and set up for remote access, then learn how to create a database and a user in MongoDB.

To access the MongoDB, run the following command.

By default, installation results in the creation of three databases. This includes admin, config, and local.

To list the existing databases, run these commands.

To make a database, execute the use command with the name of the database. For example, to create a database named test_db, run the command

To verify the database you are using, execute the db command. In this scenario, the output will be test_db.

To create a User

MongoDB offers various shell techniques to control your database. The db.createUser function enables the creation of a new user within a database.

This is how you can create a user name test_user with read-and-write roles for the test_db database.

You can list the users created using the db.getUsers() command.

Alternatively, run the command

Last updated