How to Install and Configure MongoDB on Ubuntu 20.04/22.04

How to Install and Configure MongoDB on Ubuntu 20.04/22.04

How to install and configure mongodb on ubuntu 20.04/22.04. In this tutorial guide, you will learn how to install and configure MongoDB on Ubuntu 20.04/22.04. For more information on this topic, visit the MongoDB Manual .

In this tutorial will show you step by step on how to Installing the latest MongoDB on Ubuntu is fairly straightforward. And as well as how to enable the MongoDB repository, import the repository GPG key, and install the MongoDB server.

How to Install and Configure MongoDB on Ubuntu 20.04/22.04

Use the following steps to install, configure and access mongo db on ubuntu 20.04/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

In this step, you need to execute 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

In this step, open your terminal and type the following command 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

In this step, exeucte the following command on your terminal to install mongodb:

sudo apt install mongodb-org

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

sudo systemctl enable --now mongod

Step 4 – Configuring MongoDB

In this step, open again your terminal and execute the following command 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

In this step, you enabled the MongoDB authentication, you’ll need to create an administrative user that can access and manage the MongoDB instance. So execute 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

How to install and configure mongodb on ubuntu 20.04/22.04. In this tutorial guide, you have learned how to install and configure MongoDB on Ubuntu 20.04/22.04. For more information on this topic, visit the MongoDB Manual .

Recommended Ubuntu Tutorial

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published.