How to Install MongoDB on Ubuntu 22.04 LTS

In this article we are going to learn Import the MongoDB public key on Ubuntu 22.04 LTS, Configure MongoDB Repo on Ubuntu 22.04 LTS, How to Install MongoDB on Ubuntu 22.04 LTS, Login MongoDB 6.0 Database on Ubuntu 22.04 LTS,Enable MongoDB authentication.

Prerequisite:

  • Ubuntu Server with 22.04 LTS
  • Good Internet Connection

Step #1:Perform System Update

Before we can proceed with our installation, let’s update system and install the required packages:

sudo apt update
sudo apt install wget curl gnupg2 software-properties-common apt-transport-https ca-certificates lsb-release

Step #2:Import the MongoDB public key on Ubuntu 22.04 LTS

Run the following command to import the MongoDB public GPG Key:

curl -fsSL https://www.mongodb.org/static/pgp/server-6.0.asc|sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/mongodb-6.gpg

Step #3:Configure MongoDB Repo on Ubuntu 22.04 LTS

Using the following commands, add the repository to your system right away:

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

Step #4:Install MongoDB on Ubuntu 22.04 LTS

Use the following command to install MongoDB 6.0 on Ubuntu on 22.04 LTS.

sudo apt update
sudo apt install mongodb-org

After successful installation, start and enable MongoDB:

sudo systemctl enable --now mongod

Check MongoDB Status:

sudo systemctl status mongod

Check MongoDB installed version:

mongod --version

Step #5:Allow Remote Access in MongoDB

Open MongoDB configuration file /etc/mongod.conf and change bindIp by adding required

sudo nano /etc/mongod.conf
# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
To bind to all IPv4 and IPv6 address you’ll set:
 bindIp: 0.0.0.0

Save the file after making the required changes, then restart the service:

sudo systemctl restart mongod
sudo systemctl stop mongod
sudo systemctl daemon-reload
sudo systemctl start mongod

Step #6:Login MongoDB 6.0 Database on Ubuntu 22.04 LTS

You can use the following command to reach the MongoDB shell:

mongosh

Inside the mongo shell type this command to switch to the admin database: 

 > use admin

Now let’s create the administrator username and set a password for the username:

> db.createUser({user:"devopshint", pwd:"devopshint", roles:[{role:"root", db:"admin"}]})

Type this command in the shell to exit the shell:

>exit

Step #7:Enable MongoDB authentication

Open /lib/systemd/system/mongod.service with nano:

sudo nano /lib/systemd/system/mongod.service

Add this line into this file

ExecStart=/usr/bin/mongod --quiet --auth --config /etc/mongod.conf

Now connect to the MongoDB shell using this command:

mongosh -u demo -p --authenticationDatabase admin
> db.runCommand({connectionStatus : 1})

we have covered How to Install MongoDB on Ubuntu 22.04 LTS.

Conclusion:

In this article we have covered Import the MongoDB public key on Ubuntu 22.04 LTS, Configure MongoDB Repo on Ubuntu 22.04 LTS, How to Install MongoDB on Ubuntu 22.04 LTS, Login MongoDB 6.0 Database on Ubuntu 22.04 LTS,Enable MongoDB authentication.

Related Articles:

Reference:

MongoDB Installation official page

Shweta Mamidwar

I am Shweta Mamidwar working as a Intern in Product Company. Likes to share knowledge.

6 thoughts on “<strong>How to Install MongoDB on Ubuntu 22.04 LTS</strong>”

  1. Thank you for the wonderful article. I followed all the processes you listed here but I’m having issues with the last command. My own is returning an error: Authentication Failed. What can I do?

    Reply
  2. Looks good. Might not be a good idea to skip step 5 for local development. You wouldn’t want to expos your local Mongo instance to the whole world.

    Reply
  3. × mongod.service – MongoDB Database Server
    Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
    Active: failed (Result: exit-code) since Fri 2023-06-23 04:49:07 UTC; 36s ago
    Docs: https://docs.mongodb.org/manual
    Process: 24172 ExecStart=/usr/bin/mongod –config /etc/mongod.conf (code=exited, status=14)
    Main PID: 24172 (code=exited, status=14)
    CPU: 47ms

    Jun 23 04:49:07 systemd[1]: Started MongoDB Database Server.
    Jun 23 04:49:07 mongod[24172]: {“t”:{“$date”:”2023-06-23T04:49:07.547Z”},”s”:”I”, “c”:”CONTROL”, “id”:7484500, “ctx”:”-“,”msg”:”Environment variable MONGODB_CONFIG_OVERR>
    Jun 23 04:49:07systemd[1]: mongod.service: Main process exited, code=exited, status=14/n/a
    Jun 23 04:49:07 systemd[1]: mongod.service: Failed with result ‘exit-code’.
    lines 1-12/12 (END)

    I am unable to restart the service. can u give me a suggestion?

    Reply
  4. during 4th step i am getting error of core dump

    (base) vicky@vicky-VirtualBox:~$ sudo systemctl status mongod
    × mongod.service – MongoDB Database Server
    Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
    Active: failed (Result: core-dump) since Mon 2023-08-21 13:22:50 IST; 11s ago
    Docs: https://docs.mongodb.org/manual
    Process: 19617 ExecStart=/usr/bin/mongod –config /etc/mongod.conf (code=dumped, signal=ILL)
    Main PID: 19617 (code=dumped, signal=ILL)
    CPU: 65ms

    Aug 21 13:22:50 vicky-VirtualBox systemd[1]: Started MongoDB Database Server.
    Aug 21 13:22:50 vicky-VirtualBox systemd[1]: mongod.service: Main process exited, code=dumped, status=4/ILL
    Aug 21 13:22:50 vicky-VirtualBox systemd[1]: mongod.service: Failed with result ‘core-dump’.
    (base) vicky@vicky-VirtualBox:~$ mongod –version
    Illegal instruction (core dumped)

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap