We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

How to Install MongoDB on Ubuntu 22.04

How to install MongoDB on Ubuntu 22.04. MongoDb or Mongo is a most popular open source NoSQL database. It does not rely upon a traditional table based relational database structure. It uses JSON based dynamic schemas which are editable anytime.

In this guide you are going to learn how to install and setup MongoDB on your Ubuntu 22.04 server. You will also learn to configure remote connection to your Mongo database.

This setup is tested on Google Cloud, so it should work fine on other VPS, cloud servers running Ubuntu 22.04.

Prerequisites

  • A Ubuntu 22.04 server with sudo access.

Initial Server Setup

Start by updating the server packages to the latest available.

sudo apt update
sudo apt dist-upgrade -y

Install Required Packages

You may need to install gnupg for importing the key. Mostly it is not necessary because it may be installed by default. In case if you don’t have it you can install using the following command.

sudo apt install gnupg

Install Libssl1

You need to install libssl1 to install MongoDb on Ubuntu 22.04 otherwise you will receive an error similar to the one below.

The following packages have unmet dependencies:
 mongodb-org-mongos : Depends: libssl1.1 (>= 1.1.1) but it is not installable
 mongodb-org-server : Depends: libssl1.1 (>= 1.1.1) but it is not installable
 mongodb-org-shell : Depends: libssl1.1 (>= 1.1.1) but it is not installable

To install libssl1 follow the below steps.

echo "deb http://security.ubuntu.com/ubuntu impish-security main" | sudo tee /etc/apt/sources.list.d/impish-security.list
sudo apt update
sudo apt install libssl1.1

Now you have all your pre-required packages installed and you can proceed to install MongoDB.

Install MongoDB

Here we will install MongoDB Community Edition with LTS using the apt package manager.

The current latest version of MongoDB at the time of this article is 5.0.8.

Import the public key using the following command.

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

Add the MongoDB repository to the sources list. We will use the Focal Fosa repo because by the time of writing this article, the MongoDB Community Edition does not have a separate repository for Jammy Jellyfish.

The Focal Fosa repository is in active development and compatible with Jammy Jellyfish.

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

Update the packages and install MongoDB.

sudo apt update
sudo apt install -y mongodb-org

Once the installation is completed enable MongoDB to start at system startup.

sudo systemctl enable mongod

Start MongoDB server.

sudo service mongod start

You can view the status using the following command.

sudo service mongod status
Output
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2022-05-30 08:49:02 UTC; 7s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 3739 (mongod)
     Memory: 99.3M
        CPU: 444ms
     CGroup: /system.slice/mongod.service
             └─3739 /usr/bin/mongod --config /etc/mongod.conf

Configure MongoDB

Now we can secure MongoDB, configure MongoDB to accept remote connections and also create a new database.

Secure MongoDB

Edit MongoDB config file.

sudo nano /etc/mongod.conf

Scroll down to the security section #security and uncomment it and enable authorization. The final edit should look as below.

security:
  authorization: enabled

Enable Remote Connections

To enable remote connections you need to edit the same file and add your internal or private IP to the network interfaces. Your configuration should look like the one below.

net:
  port: 27017
  bindIp: 127.0.0.1,10.128.15.214

Replace 10.128.15.214 with your IP address.

Open firewall if you have configured any, for the port 27017.

Restart MongoDB for the changes to take effect.

sudo systemctl restart mongod

Confirm if MongoDB is allowing remote connections using the following command.

sudo lsof -i | grep mongo

You should receive an output similar to the one below.

mongod    3866         mongodb   12u  IPv4  33773      0t0  TCP instance-1.c.project_id.internal:27017 (LISTEN)
mongod    3866         mongodb   13u  IPv4  33774      0t0  TCP localhost:27017 (LISTEN)

Create MongoDB Admin User

Connect to MongoDB shell using mongosh command.

mongosh
Current Mongosh Log ID: 62948cd79def2ce0319469d5
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.4.2
Using MongoDB:          5.0.8
Using Mongosh:          1.4.2

Change to admin database.

use admin

Create admin user with all privileges and setup password.

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

Enter password when prompted.

Enter exit to exit the shell.

Now you can use the following connection string to connect to MongoDB.

mongodb://admin:password@External-IP:27017/database

Prepare yourself for a role working as an Information Technology Professional with Linux operating system

MongoDB Service Commands

You can start, stop, enable, disable, restart using the following commands.

Start MongoDB

sudo systemctl start mongodb

Stop MongoDB

sudo systemctl stop mongodb

Enable MongoDB

sudo systemctl enable mongodb

Disable MongoDB

sudo systemctl disable mongodb

Restart MongoDB

sudo systemctl restart mongodb

Conclusion

Now you have learned how to install and setup MongoDB on Ubuntu 22.04 and also configure remote connections.

Thanks for your time. If you face any problem or any feedback, please leave a comment below.

Cart

Your Cart is Empty

Back To Shop