How to Install and Configure MongoDB on Ubuntu 22.04

MongoDB is a source-available cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License which is deemed non-free by several distributions.

MongoDB is an open source NoSQL database management program. NoSQL is used as an alternative to traditional relational databases. NoSQL databases are quite useful for working with large sets of distributed data. MongoDB is a tool that can manage document-oriented information, store or retrieve information.

In this tutorial guide, i am going to show you how to install and configure MongoDB on Ubuntu 22.04.

How to Install and Configure MongoDB on Ubuntu 22.04

Follow the below given steps to install, configure and access mongo db on ubuntu 22.04:

  • Step 1 – Install the dependencies
  • Step 2 – Import the repository’s GPG key
  • Step 3 – Install MongoDB
  • Step 4 – Configuring MongoDB
  • Step 5 – Creating Administrative MongoDB User
  • Step 6 – Restart Server

Step 1 – Install the dependencies

Run the following command on your terminal to Install the dependencies necessary to add a new repository over HTTPS:

sudo apt update
sudo apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common

Step 2 – Import the repository’s GPG key

Run the following command on terminal to Import the repository’s GPG key and add the MongoDB repository:

wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
sudo add-apt-repository 'deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse'

Step 3 – Install MongoDB

Run the following command on your terminal to install mongodb:

sudo apt install mongodb-org

Then run the following command on terminal to Start the MongoDB daemon and enable it:

sudo systemctl enable --now mongod

Step 4 – Configuring MongoDB

Run the following command on terminal to configure mongodb in mongod.conf file:

sudo nano /etc/mongod.conf

After that, add the following line into mongod.conf file and save it:

security:
    authorization: enabled

After that, restart the mongod service for changes to take effect:

sudo systemctl restart mongod

Step 5 – Creating Administrative MongoDB User

To enabled the MongoDB authentication, you’ll need to create an administrative user that can access and manage the MongoDB instance. So run the following command on it:

mongo

Then type this following query:

use admin

The output will be:

switched to db admin

Now, execute the following command to create a new user named mongoAdmin, with password changeMe and userAdminAnyDatabase role:

db.createUser(
   {
     user: "mongoAdmin",
     pwd: "changeMe",
     roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
   }
 )

Once done, exit the mongo shell with:

quit() 

Step 6 – Restart Server

After successfully installed Mongodb and configure these on ubuntu 20.04.

Finally, You need to restart server by using the following command:

sudo systemctl restart mongod

Conclusion

In this tutorial guide, i have shown to you how to install and configure MongoDB on Ubuntu 20.04/22.04.

More Tutorials

Leave a Comment