How to Install MongoDB in Ubuntu 20.04/22.04

How to Install MongoDB in Ubuntu 20.04/22.04

To install and configure mongodb ubuntu. In this tutorial guide, you will learn how to install and configure MongoDB 4.4, 5.0 in Ubuntu 20.04/22.04 using terminal

How to Install and Configure MongoDB 4.4, 5.0 on Ubuntu 20.0/22.04 Terminal

Steps to install, configure, and access mongodb in ubuntu 20.04/22.04 using terminal or command line:

  • Step 1 – Install the dependencies
  • Step 2 – Import repository’s GPG key for MongoDB 4.4, 5.0
  • Step 3 – Install MongoDB 4.4, 5.0
  • 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 or command line 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 repository’s GPG key for MongoDB 4.4, 5.0, 6.0

Import the MongoDB 4.4 GPG key to ensure the authenticity of the MongoDB packages:

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'

Import the MongoDB 5.0 GPG key to ensure the authenticity of the MongoDB 5.0 packages:

wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb.list

Step 3 – Install MongoDB 4.4, 5.0

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. Required fields are marked *