Jun 29, 2023 9 min read

How to Install MongoDB 6.0 on Ubuntu 22.04

Install MongoDB 6.0 on Ubuntu 22.04 with our step-by-step tutorial. It is a database that stores data in a flexible, document-oriented format.

Install MongoDB 6.0 on Ubuntu 22.04
Table of Contents

Choose a different version or distribution

Introduction

Before we begin talking about how to install MongoDB 6.0 on Ubuntu 22.04, let's briefly understand – What is MongoDB?

MongoDB is a popular, open-source database that stores data in a flexible, document-oriented format. It provides a scalable and high-performance solution for managing vast amounts of data across diverse applications. With its dynamic schema, MongoDB allows for easy and seamless integration of data, enabling businesses to adapt quickly to evolving needs.

Its powerful querying capabilities and horizontal scaling make it ideal for modern, data-intensive applications. MongoDB simplifies development and offers robust data management features, making it a top choice for developers and enterprises worldwide.

In this tutorial, you will learn how to install MongoDB 6.0 on Ubuntu 22.04. We will also address a few FAQs on how to install MongoDB on Ubuntu 22.04.

Advantages of MongoDB

  1. Flexible Data Model: MongoDB's document-oriented structure allows for easy and dynamic data modeling, adapting to changing requirements effortlessly.
  2. Scalability: MongoDB's distributed architecture enables horizontal scaling, handling massive data volumes and high traffic loads efficiently.
  3. High Performance: With its in-memory computing and indexing features, MongoDB delivers fast query responses, ensuring optimal application performance.
  4. Seamless Integration: MongoDB integrates well with popular programming languages, frameworks, and analytics tools, enabling smooth data integration and analysis.
  5. Developer Productivity: MongoDB's intuitive query language, rich APIs, and extensive documentation streamline development, reducing time and effort for developers.

Uses of MongoDB in IT Sector

  • Using MongoDB's document architecture, virtually any data structure can be simply specified and modified.
  • If you are used to having to shut down your website or application in order to change the data format, MongoDB is for you.
  • As a result of MongoDB's decision to represent and store data in a document format, you may access it from any language by using data structures that are specific to that language, such as dictionaries in Python, objects in JavaScript, maps in Java, etc.
  • Build clusters with real-time replication and shard large or high-throughput collections across several clusters to maintain performance and scale horizontally.
  • MongoDB makes data access simple since it rarely requires joins or transactions.
  • Using MongoDB's document model, information may be combined into a single document rather than having to perform costly join processes from traditional relational databases. This speeds up queries and returns all necessary information in a single database call.
  • MongoDB supports a number of flexible ways. The cloud-based MongoDB Atlas allows you to choose an instance size that fits your current requirements.
  • With the help of MongoDB Atlas, a MongoDB cluster can be set up and created more quickly.
    With the help of MongoDB Atlas, a MongoDB cluster can be set up and created more quickly.
    With just a few clicks in the intuitive user interface, you may start a brand-new instance that is forever free.

MongoDB 6.0 New Features

MongoDB 6.0 includes the following exciting new features:

  • One of the most important modifications makes it easier for users to index data that changes over time by supporting secondary indexes on time series measurements.
  • The interface, drivers, and connectors for Atlas SQL are presently available for public preview.
    Data analysts, many of whom are accustomed to working with SQL, are given additional freedom by the ability to query and analyze Atlas data using current expertise and preferred tools.
  • New atlas search features. Atlas Search can help you integrate relevance-based search into your applications.
  • Atlas instances that are serverless. By adopting serverless technologies, developers may build applications without worrying about resource provisioning or scale.
  • To solve the issues with inter-cluster data synchronization, MongoDB 6.0 added Cluster-to-Cluster Synchronization as a feature. With two MongoDB clusters, whether they are in the same environment or a hybrid one, it enables ongoing unidirectional data synchronization.
  • With this preview release of Queryable Encryption, users can encrypt sensitive data on the client side, store it as fully randomized encrypted data on the database server side, and run expressive queries on the encrypted data.
  • With MongoDB 6.0 Enterprise, you may securely manage the keys for encrypting the MongoDB audit log on an outside Key Management Interoperability Protocol (KMIP) server. KMIP streamlines the maintenance of cryptographic keys by removing the requirement for unusual key management procedures.

MongoDB HA(High Availability) Architecture

MongoDB automatically maintains replica sets, which are several copies of data scattered across servers, racks, and data centers. Native replication and automatic failover are used in replica sets to help prevent database downtime.

In MongoDB, a replica set is a collection of mongod processes that look after the same data set. All production deployments are built on replica sets, which provide redundancy and high availability.

You will require a minimum of 3 nodes to achieve MongoDB high availability, where a replica set consists of a primary node and two secondary nodes. A third node can be created by combining these first two nodes with an arbitrator.

Install MongoDB 6.0 on Ubuntu 22.04

The steps below will walk you through installing MongoDB 6.0 on Ubuntu 22.04.

MongoBD Community & Enterprise Editions

The Mongo Enterprise Advanced subscription includes access to both the free Community edition and the Enterprise edition of MongoDB.

The Enterprise edition now includes on-disk encryption, LDAP and Kerberos support, auditing, and other features.

Step 1: Perform System Update

Let us update the Ubuntu 20.04|18.04 system and install the necessary packages before moving further with our installation:

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

Step 2: Import the public key

Import the public GPG key for MongoDB by running the following command:

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

Add the repository as soon as possible to your system by using the procedures shown below:

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 6.0 on Ubuntu 22.04, 20.04, or 18.04

To install MongoDB 6.0 on Ubuntu, run the following command after adding the correct repository.

Ubuntu 22.04:

wget http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2.17_amd64.deb
sudo dpkg -i ./libssl1.1_1.1.1f-1ubuntu2.17_amd64.deb
sudo apt update
sudo apt install mongodb-org

Ubuntu 20.04 / 18.04:

sudo apt update
sudo apt install mongodb-org

Dependency tree:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following package was automatically installed and is no longer required:
  libfwupdplugin1
Use 'sudo apt autoremove' to remove it.
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 4 not upgraded.
Need to get 134 MB of archives.
After this operation, 458 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

Start and enable MongoDB after a successful installation:

sudo systemctl enable --now mongod

Verify MongoDB Status:

systemctl status mongod
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2022-07-29 04:11:46 EAT; 8s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 430451 (mongod)
     Memory: 61.6M
     CGroup: /system.slice/mongod.service
             └─430451 /usr/bin/mongod --config /etc/mongod.conf

Jul 29 04:11:46 frank-PC systemd[1]: Started MongoDB Database Server.

Verify MongoDB installed version:

mongod --version
db version v6.0.0
Build Info: {
    "version": "6.0.0",
    "gitVersion": "e61bf27c2f6a83fed36e5a13c008a32d563babe2",
    "openSSLVersion": "OpenSSL 1.1.1f  31 Mar 2020",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "ubuntu2004",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

Step 5: Configure MongoDB 6.0

MongoDB's configuration file may be found at /etc/mongod.conf. The database path, logs directory, and any other necessary configurations can be made in this file. Some MongoDB configurations are mentioned below.

Enable Password Authentication on MongoDB 6.0

When this is enabled, users must enter a password in order to read and edit databases.

Uncomment the following line to do this:

security:
  authorization: enabled

Enable Remote Access on MongoDB 6.0

MongoDB is commonly set up to only permit local access. If you want to access it remotely, you must change the code below to include the server IP or hostname, whichever is preferable.

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

Set the following to bind to every IPv4 and IPv6 address:

 bindIp: 0.0.0.0

After making the necessary modifications, save the file, and then restart the service:

sudo systemctl restart mongod

Remote access to the service will be possible if the port is allowed beyond the firewall:

sudo ufw allow 27017

Step 6: Modify the MongoDB 6.0 default Data Path

The default path for MongoDB's data storage is /var/lib/mongodb. However, this can be changed if another path is chosen. To do this, stop the currently operating instance:

sudo systemctl stop mongod.service

Create the required path and grant it the necessary permissions to store data:

sudo mkdir -p /newdata/mongo
sudo chown -R mongodb:mongodb  /newdata/mongo

The contents of the previous path must be copied into the new directory:

sudo rsync -av /var/lib/mongodb  /newdata/mongo

After that, change the name of the old directory to a backup file:

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

Make a symbolic link now to point to the new directory:

sudo ln -s /newdata/mongo /var/lib/mongodb

Start the MongoDB Service:

sudo systemctl daemon-reload
sudo systemctl start mongod

Step 7: Using MongoDB 6.0 Database

To access the MongoDB shell, issue the following command:

 mongosh

The output is as follows:

Current Mongosh Log ID:	62e33c2dfed670cec73bbf7f
Connecting to:		mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.5.3
Using MongoDB:		6.0.0
Using Mongosh:		1.5.3

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.

Warning: Found ~/.mongorc.js, but not ~/.mongoshrc.js. ~/.mongorc.js will not be loaded.
  You may want to copy or rename ~/.mongorc.js to ~/.mongoshrc.js.

Create a User and Assign a Role in MongoDB

For the purposes of this tutorial, we will create a user called mongdbuser and assign them the admin roles.

However, this command can be used to create the desired user.

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

Use the command indicated below to close the shell after providing the user with the correct password.

> exit
bye

To check if the user has been created, log in to the shell using the user credentials.

mongosh -u mongdbuser -p --authenticationDatabase admin

Create a database in MongoDB

You can look through the available databases:

> show dbs
admin   132.00 KiB
config   12.00 KiB
local    72.00 KiB

With MongoDB, creating a database is as simple as switching to a blank database. For instance, mongotestdb:

> use mongotestdb

Create a collection in MongoDB

In SQL databases, a collection is comparable to a table. Here, the following commands can be used to create a table in the appropriate database:

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

Use the following command to view the collections after they have been created:

> show collections
employeedetails

FAQs to Install MongoDB 6.0 on Ubuntu 22.04

What are the system requirements for MongoDB 6.0 on Ubuntu 22.04?

MongoDB 6.0 requires a 64-bit operating system, at least 4 GB of RAM, and sufficient disk space to accommodate your data.

Can I upgrade from a previous version of MongoDB to 6.0 on Ubuntu 22.04?

Yes, you can upgrade from a previous version of MongoDB to 6.0 on Ubuntu 22.04. MongoDB provides a detailed upgrade guide that you can follow to ensure a smooth transition.

How can I verify the installation of MongoDB 6.0 on Ubuntu 22.04?

You can verify the installation by running the mongod --version command in the terminal. It will display the MongoDB version installed on your system.

Are there any security considerations for MongoDB 6.0 on Ubuntu 22.04?

Yes, MongoDB 6.0 has security features such as authentication and encryption. It's recommended to configure authentication and enable SSL/TLS encryption to protect your data.

How can I start and stop the MongoDB service on Ubuntu 22.04?

You can start and stop the MongoDB service using the systemctl command. For example, sudo systemctl start mongodb to start the service and sudo systemctl stop mongodb to stop it.

Where are the configuration files located for MongoDB 6.0 on Ubuntu 22.04?

The configuration files for MongoDB 6.0 are typically located in the /etc/mongodb directory on Ubuntu 22.04.

How can I access the MongoDB shell after installing MongoDB 6.0 on Ubuntu 22.04?

You can access the MongoDB shell by running the mongo command in the terminal. This allows you to interact with the MongoDB instance and execute queries.

Is it possible to uninstall MongoDB 6.0 from Ubuntu 22.04?

Yes, you can uninstall MongoDB 6.0 from Ubuntu 22.04 by following the official MongoDB uninstallation instructions, which include removing the MongoDB package and configuration files.

Conclusion

The installation of MongoDB 6.0 on Ubuntu 22.04 has been covered in this tutorial. Also, you learnt how to create users, collections, and databases. We genuinely hope that this information was useful to you.

If you have any suggestions or queries, kindly leave them in the comments section.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.