How to Install MongoDB on Ubuntu 22.04

How to Install MongoDB on Ubuntu 22.04

Posted on

Learn how to set up MongoDB on Ubuntu 22.04. MongoDb or Mongo is a hottest open supply NoSQL database. It doesn’t depend upon a conventional desk primarily based relational database construction. It makes use of JSON primarily based dynamic schemas that are editable anytime.

On this information you will learn to set up and setup MongoDB in your Ubuntu 22.04 server. Additionally, you will study to configure distant connection to your Mongo database.

This setup is examined on Google Cloud, so it ought to work positive on different VPS, cloud servers operating Ubuntu 22.04.

Stipulations

  • A Ubuntu 22.04 server with sudo entry.

Preliminary Server Setup

Begin by updating the server packages to the most recent obtainable.

sudo apt replace
sudo apt dist-upgrade -y

Set up Required Packages

You might want to put in gnupg for importing the important thing. Principally it’s not obligatory as a result of it could be put in by default. In case if you happen to don’t have it you may set up utilizing the next command.

sudo apt set up gnupg

Set up Libssl1

You might want to set up libssl1 to put in MongoDb on Ubuntu 22.04 in any other case you’ll obtain an error just like the one under.

The next packages have unmet dependencies:
 mongodb-org-mongos : Relies upon: libssl1.1 (>= 1.1.1) however it's not installable
 mongodb-org-server : Relies upon: libssl1.1 (>= 1.1.1) however it's not installable
 mongodb-org-shell : Relies upon: libssl1.1 (>= 1.1.1) however it's not installable

To put in libssl1 observe the under steps.

echo "deb http://safety.ubuntu.com/ubuntu impish-security primary" | sudo tee /and so forth/apt/sources.checklist.d/impish-security.checklist
sudo apt replace
sudo apt set up libssl1.1

Now you may have all of your pre-required packages put in and you’ll proceed to put in MongoDB.

Set up MongoDB

Right here we’ll set up MongoDB Group Version with LTS utilizing the apt package deal supervisor.

The present newest model of MongoDB on the time of this text is 5.0.8.

Import the general public key utilizing the next command.

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

Add the MongoDB repository to the sources checklist. We are going to use the Focal Fosa repo as a result of by the point of writing this text, the MongoDB Group Version doesn’t have a separate repository for Jammy Jellyfish.

The Focal Fosa repository is in energetic growth and suitable with Jammy Jellyfish.

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

Replace the packages and set up MongoDB.

sudo apt replace
sudo apt set up -y mongodb-org

As soon as the set up is accomplished allow MongoDB to start out at system startup.

sudo systemctl allow mongod

Begin MongoDB server.

sudo service mongod begin

You may view the standing utilizing the next command.

sudo service mongod standing
Output
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
     Energetic: energetic (operating) since Mon 2022-05-30 08:49:02 UTC; 7s in the past
       Docs: https://docs.mongodb.org/handbook
   Foremost PID: 3739 (mongod)
     Reminiscence: 99.3M
        CPU: 444ms
     CGroup: /system.slice/mongod.service
             └─3739 /usr/bin/mongod --config /and so forth/mongod.conf

Configure MongoDB

Now we are able to safe MongoDB, configure MongoDB to just accept distant connections and likewise create a brand new database.

Safe MongoDB

Edit MongoDB config file.

sudo nano /and so forth/mongod.conf

Scroll right down to the safety part #safety and uncomment it and allow authorization. The ultimate edit ought to look as under.

safety:
  authorization: enabled

Allow Distant Connections

To allow distant connections you’ll want to edit the identical file and add your inside or personal IP to the community interfaces. Your configuration ought to seem like the one under.

internet:
  port: 27017
  bindIp: 127.0.0.1,10.128.15.214

Change 10.128.15.214 along with your IP tackle.

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

Restart MongoDB for the modifications to take impact.

sudo systemctl restart mongod

Verify if MongoDB is permitting distant connections utilizing the next command.

sudo lsof -i | grep mongo

You must obtain an output just like the one under.

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

Create MongoDB Admin Person

Hook up with MongoDB shell utilizing mongosh command.

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

Change to admin database.

use admin

Create admin person with all privileges and setup password.

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

Enter password when prompted.

Enter exit to exit the shell.

Now you should utilize the next connection string to connect with MongoDB.

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

Put together your self for a job working as an Information Technology Professional with Linux working system

MongoDB Service Instructions

You may begin, cease, allow, disable, restart utilizing the next instructions.

Begin MongoDB

sudo systemctl begin mongodb

Cease MongoDB

sudo systemctl cease mongodb

Allow MongoDB

sudo systemctl allow mongodb

Disable MongoDB

sudo systemctl disable mongodb

Restart MongoDB

sudo systemctl restart mongodb

Conclusion

Now you may have discovered the best way to set up and setup MongoDB on Ubuntu 22.04 and likewise configure distant connections.

Thanks in your time. In the event you face any drawback or any suggestions, please depart a remark under.

offshore vps