HomeDATABASEHow To Install MongoDB 6.0 on Ubuntu 22.04|20.04|18.04

How To Install MongoDB 6.0 on Ubuntu 22.04|20.04|18.04

Welcome to this guide on how to install MongoDB 6.0 on Ubuntu 22.04|20.04|18.04. Databases are so important in any environment. They are mainly used to store data. There are two major database types. These are:

  • Relational databases/RDBMS/SQL databases: data is stored in tables and rows, and the tables are then linked using keys. Popular examples are MySQL, MariaDB, SQLite, PostgreSQL e.t.c
  • Non-relational databases/NoSQL databases: data is stored using an optimized model for specific requirements. They include MongoDB, Redis, Apache HBase, Apache Cassandra etc.

MongoDB is a popular general-purpose non-relational database. It is highly preferred by developers because it makes it easier to develop and scale their applications. Data in MongoDB is stored in BSON under the hood and represented as JSON. This is what makes it different from relational databases.

MongoDB 6.0 is a major release (supported for both MongoDB Atlas and on-premises deployments). It includes all the changes in previous releases 5.1, 5.2, and 5.3. The notable features in MongoDB 6.0 are:

  • Better support for event-driven architectures which enriches change streams, adding abilities that take change streams to the next level.
  • Enriched queries with deeper insights to enable users to process multiple documents and return computed results. This can be useful when building complex data processing pipelines to extract the insights you need.
  • More resilient operations with the replica set design set to withstand and overcome outages
  • Improved data security and operational efficiency
  • Seamless data sync. The Cluster-to-Cluster Sync provides an easy way to migrate data to the cloud, with continuous, unidirectional data synchronization between two MongoDB clusters.
  • More operators, allow you to push more work to the database while spending less time writing code.

In this guide, we will systematically walk through how to install MongoDB 6.0 on Ubuntu 22.04|20.04|18.04

System Requirements

MongoDB 6.0 runs on a system with the below architectures:

  • For x86_64 microarchitecture:
    • Intel x86_64 of a Sandy Bridge or later Core processor or a Tiger Lake or later Celeron or Pentium processor
    • AMD x86_64 of a Bulldozer or later processor.
  • ARM64 microarchitecture
    • ARMv8.2-A or later microarchitecture

Check the architecture of your system with the command:

$ cat /proc/cpuinfo
processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 94
model name	: Intel(R) Xeon(R) CPU E3-1275 v5 @ 3.60GHz
stepping	: 3
microcode	: 0xf0
cpu MHz		: 3600.056
cache size	: 16384 KB
physical id	: 0
siblings	: 1
core id		: 0
cpu cores	: 1
.....

Step 1 – Add MongoDB 6.0 Repository on Ubuntu 22.04|20.04|18.04

To be able to install MongoDB 6.0 we need to add the repository on our Ubuntu 22.04|20.04|18.04. Begin by updating the APT package index:

sudo apt update

Install the required packages:

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

Next, import the GPG keys:

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

Now add the repository to the system:

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 2 – Install MongoDB 6.0 on Ubuntu 22.04|20.04|18.04

Once the repository has been added, you can proceed to install MongoDB 6.0 on Ubuntu 22.04|20.04|18.04 using the command:

sudo apt update && sudo apt install mongodb-org

Sample Output:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  mongodb-database-tools mongodb-mongosh mongodb-org-database
  mongodb-org-database-tools-extra mongodb-org-mongos mongodb-org-server mongodb-org-shell
  mongodb-org-tools
The following NEW packages will be installed:
  mongodb-database-tools mongodb-mongosh mongodb-org mongodb-org-database
  mongodb-org-database-tools-extra mongodb-org-mongos mongodb-org-server mongodb-org-shell
  mongodb-org-tools
0 upgraded, 9 newly installed, 0 to remove and 229 not upgraded.
Need to get 146 MB of archives.
After this operation, 422 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

Once the installation is complete, start and enable the service:

sudo systemctl start mongod
sudo systemctl enable mongod

Verify if the service is running as desired.

$ systemctl status mongod
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2023-04-07 19:45:24 EAT; 7s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 9311 (mongod)
     Memory: 64.4M
        CPU: 567ms
     CGroup: /system.slice/mongod.service
             └─9311 /usr/bin/mongod --config /etc/mongod.conf

Apr 07 19:45:24 tutornix systemd[1]: Started MongoDB Database Server.

MongoDB might fail to start if your system does not meet the requirements otherwise proceed to verify the installed MongoDB version:

$ mongod --version
db version v6.0.5
Build Info: {
    "version": "6.0.5",
    "gitVersion": "c9a99c120371d4d4c52cbb15dac34a36ce8d3b1d",
    "openSSLVersion": "OpenSSL 3.0.2 15 Mar 2022",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "ubuntu2204",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

Step 3 – Configure MongoDB 6.0 on Ubuntu 22.04|20.04|18.04

MongoDB stores its configuration file at /etc/mongod.conf. This file contains all the configurations such as database path, logs directory, etc.

In this guide, I will demonstrate some of the basic configurations for the MongoDB 6.0 server. Open the file for editing:

sudo vim /etc/mongod.conf

3.1 – Enable Password Authentication on MongoDB 6.0

After installation, MongoDB is not secure as password authentication is not enabled by default. To enable it, find and uncomment the below line:

security:
  authorization: enabled

3.2 – Enable Remote Access on MongoDB 6.0

You can also modify the listen IP address and port for MongoDB in this file. To be able to access MongoDB remotely, allow it to bind to all interfaces by making the below changes:

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.

Once the desired changes have been made, save the file and restart the service:

sudo systemctl restart mongod

If you have a firewall enabled, allow the port through it:

sudo ufw allow 27017

Step 4 – Use MongoDB 6.0 on Ubuntu 22.04|20.04|18.04

To use MongoDB, log into the shell using the command:

$ mongosh
Current Mongosh Log ID:	6430490e3e1c8a8d9050c06e
Connecting to:		mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.0
Using MongoDB:		6.0.5
Using Mongosh:		1.8.0

For mongosh info see: https://docs.mongodb.com/mongodb-shell/


To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.

test> 

Create a user and a role. For this demo, we will create a user and assign them the admin role as shown:

use admin
db.createUser(
{
user: "monguser",
pwd: passwordPrompt(), // or cleartext password
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
}
)

You will be required to provide a password for the user before you proceed to exit the shell:

admin> exit
bye

To test if everything is okay, we will try to log in with the created user:

$ mongosh -u monguser
Enter password: *********
Current Mongosh Log ID:	64304980fe0055a73d77e29d
Connecting to:		mongodb://<credentials>@127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.0
Using MongoDB:		6.0.5
Using Mongosh:		1.8.0

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

test> 

To view the available databases use:

> show dbs

Creating a database in MongoDB is done by switching to a non-existing database. For example:

> use tutordb
switched to db tutordb

Create a test collection in the database:

db.userdetails.insertOne(
   {F_Name: "Tutornix",
    L_NAME: "Home",
    ID_NO: "124345",
     AGE: "70",
     TEL: "25465445642"
   }
)

Check the created collection:

> show collections
userdetails

To create an overall admin(has the ability to access and use any database), use the command with the below syntax:

use admin
db.createUser(
  {
    user: 'admin',
    pwd: 'AdminPassW0rd',
    roles: [ { role: 'userAdminAnyDatabase', db: 'admin' } ]
  }
);

You can also create an admin for a specific database:

use testdatabase
db.createUser(
  {
    user: 'testadmin',
    pwd: 'TestPassW0rd',
    roles: [ { role: 'userAdmin', db: 'testdatabase' } ]
  }
);

List users created on MongoDB:

use admin
db.system.users.find()

Sample Output:

How To Install MongoDB 6.0 on Ubuntu 22.04|20.04|18.04

Final Thoughts

That is it for today! We have triumphantly walked through how to install MongoDB 6.0 on Ubuntu 22.04|20.04|18.04. Now proceed and use the database as desired.

Interested in more?

How To Install MariaDB 11.0 on Ubuntu 22.04|20.04|18.04

How To Install MariaDB 10.6 on Ubuntu 22.04|20.04|18.04

Klinsmann Öteyo
Klinsmann Öteyo
Self proclaimed Geek
- Advertisment -

Recent posts

LEAVE A REPLY

Please enter your comment!
Please enter your name here