Greetings and welcome to this detailed guide on how to install MongoDB 7.0 on Ubuntu 22.04|20.04. But before we plunge into the nub of this matter, we need to understand a few concepts.

Databases are divided into two broad categories. These are Relational and Non-relational databases. Relational databases observe the SQL syntax. Here, data is stored in structured tables with predefined schemas. Non-relational databases store data in various formats such as documents, column-family stores, key-value pairs, or graphs.

MongoDB is one of the most popularly used Non-relational databases. This cross-platform object-oriented database is developed and maintained by MongoDB Inc. MongoDB supports both 32 and 64-bit systems and is preferred where scalability and flexibility are highly required.

🏆 BESTSELLER

The Ultimate Ubuntu Desktop Handbook

Master Ubuntu like a pro - from beautiful desktop customization to powerful terminal automation. Perfect for developers, system admins, and power users who want total control of their workspace.

Only $15 $30
Get Instant Access →

Features of MongoDB 7.0

The latest major release of MongoDB, version 7.0 has major improvements in 4 key areas. These are Migrations, performance, developer experience and security.

The improvements done in the areas are:

  • Security: There are a lot of improvements in security in MongoDB 7.0. The availability of Queryable Encryption provides users with the ability to encrypt sensitive workloads throughout its lifecycle and also be able to query the encrypted data.
  • Performance improvements: The performance improvements in MongoDB 7.0 include an advanced query execution strategy, a Sharded cluster which have faster chunk migrations and a new high throughput parameter. The Balancer chunk auto-merge is that ensures a linear growth in the number of chunks is no longer required when scaling the sharded cluster.
  • Migration operations: To make migrations easier and simpler, MongoDB 7.0 has streamlined Cluster-to-Cluster Sync (mongosync). This makes it simple to sync clusters with, unlike topologies.
  • Updates to the Query API: To solve the errors experienced by developers in change streams with large documents, MongoDB 7.0 has introduced bitwise operators, percentile operators, and user role variables in the Aggregation Framework. In addition to that, there are ad-hoc updates and deletes for time series collections included.

Follow the below steps to install MongoDB 7.0 on Ubuntu 22.04|20.04.

1. Add MongoDB 7.0 Repository

Before we start on anything, update your APT package index:

sudo apt update -y

Install all the required packages:

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

Next, import the GPG key for the MongoDB 7.0 repository:

curl -fsSL https://pgp.mongodb.com/server-7.0.asc |sudo gpg  --dearmor -o /etc/apt/trusted.gpg.d/mongodb-server-7.0.gpg

Now add the MongoDB 7.0 Repo on Ubuntu 22.04|20.04:

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

2. Install MongoDB 7.0 Packages

Once the repository has been added, update your package index and install MongoDB 7.0 using the command:

sudo apt update && sudo apt install mongodb-org

Dependency Tree:

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 501 not upgraded.
Need to get 157 MB of archives.
After this operation, 511 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

Start and enable the service:

sudo systemctl enable --now mongod

Verify if the service is up:

$ systemctl status mongod
 mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2023-08-27 12:36:44 EAT; 8s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 9811 (mongod)
     Memory: 72.7M
        CPU: 641ms
     CGroup: /system.slice/mongod.service
             └─9811 /usr/bin/mongod --config /etc/mongod.conf

Ago 27 12:36:44 computingforgeeks systemd[1]: Started MongoDB Database Server.
....

Verify the installed MongoDB version:

$ mongod --version
db version v7.0.0
Build Info: {
    "version": "7.0.0",
    "gitVersion": "37d84072b5c5b9fd723db5fa133fb202ad2317f1",
    "openSSLVersion": "OpenSSL 3.0.2 15 Mar 2022",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "ubuntu2204",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

3. Configure MongoDB 7.0

MongoDB stores its configuration file at /etc/mongod.conf. In the file, you can make all the desired configurations for MongoDB including the database path, logs directory, etc.

Here are some of the settings we can make for MongoDB 7.0. First, open the file for editing:

sudo vim /etc/mongod.conf

a. Enable Password Authentication

To enable password authentication on MongoDB 7.0, uncomment the below lines in the config:

security:
  authorization: enabled

b. Enable Remote Access

To allow the database to be accessed remotely, we need to set it to bind to all interfaces and not localhost only. To achieve that, make the below modification:

# 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.

After the settings have been made, save the config and restart the service:

sudo systemctl restart mongod

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

sudo ufw allow 27017

4. Changing MongoDB 7.0 default Path

There are some scenarios when you need to change the data path for MongoDB. The default path for MongoDB is /var/lib/mongodb. Here, we will create and use a different path.

Begin by stopping the service:

sudo systemctl stop mongod.service

Create the new data path and set the required permissions:

sudo mkdir -p /monogdb/data
sudo chown -R mongodb:mongodb /monogdb/data

You can now copy the contents of the old path using Rsync as shown:

sudo rsync -av /var/lib/mongodb/  /monogdb/data

Move the old path to a backup file:

sudo mv /var/lib/mongodb /var/lib/mongodb.bak

Now modify your config file to accommodate the new data path:

sudo vim /etc/mongod.conf

Modify the below lines:

# Where and how to store data.
storage:
  dbPath: /monogdb/data

Save the changes and start the service:

sudo systemctl daemon-reload
sudo systemctl start mongod

5. Using MongoDB 7.0 Database

To connect to the MongoDB shell, use:

mongosh

Sample Output:

Current Mongosh Log ID:	64eb1de75716266ec1174a00
Connecting to:		mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.10.6
Using MongoDB:		7.0.0
Using Mongosh:		1.10.6

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> 

a. Create a User on MongoDB

We will test by creating a sample user with the name testuser. We will also assign admin roles to the user:

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

Set the desired password for the user then exit with the command:

> exit
bye

We will test if all is okay by connecting using the user:

$ mongosh -u testuser -p --authenticationDatabase admin
Enter password: *********
Current Mongosh Log ID:	64eb1ed66fed4f50be41bc36
Connecting to:		mongodb://<credentials>@127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&authSource=admin&appName=mongosh+1.10.6
Using MongoDB:		7.0.0
Using Mongosh:		1.10.6

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

test> 

b. Create databases & Collections

To create a database on MongoDB, just switch to a non-existing database, say testdb as shown:

> use testdb
switched to db testdb

You can now create collections in the database:

db.employeedetails.insertOne(
   {F_Name: "John",
    L_NAME: "Doe",
    ID_NO: "23245",
     AGE: "25",
     TEL: "63365467666"
   }
)

View the created collection

> show collections
employeedetails

Verdict

Today we have learned how you can install MongoDB 7.0 on Ubuntu 22.04|20.04. We have also learned how to make configurations, connect and use MongoDB 7.0. I hope this was important to you.

See more:

2 COMMENTS

  1. I’m very thankful to you my brother for providing me update resource. It worked fine for Ubuntu 20.04, Love form India.

LEAVE A REPLY

Please enter your comment!
Please enter your name here