Database

How to install and configure MongoDB 6 on Ubuntu 22.04

Pinterest LinkedIn Tumblr

In this guide we are going to learn how to install MongoDB 6.0 Community Edition on an Ubuntu 22.04 server.

MongoDB is a cross-platform document-oriented NoSQL database program that uses JSON-like documents with optional schemas. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License.

MongoDB was built for people building internet and business applications who need to evolve quickly and scale elegantly. Companies and development teams of all sizes use MongoDB for a wide variety of reasons.

Instead of storing data in tables of rows or columns like SQL databases, each record in a MongoDB database is a document described in BSON, a binary representation of the data. Applications can then retrieve this information in a JSON format.

Here’s a simple JSON document describing a historical figure.

{
  "_id": 1,
  "name": {
    "first": "Ada",
    "last": "Lovelace"
  },
  "title": "The First Programmer",
  "interests": ["mathematics", "programming"]
}

Related Content

Prerequisites

To follow along, ensure you have the following:

  • An up to date Ubuntu 22.04 or latest server
  • Knowledge of Linux Terminal
  • Access to the internet
  • Root access to the server or User with Sudo access

Table of Content

  1. Ensuring the server is up to date
  2. Instaling MongoDB
  3. Starting and enabling MongoDB service
  4. Connecting to Mongodb and Executing some test commands
  5. Uninstall MongoDB Community Edition

1. Ensuring the server is up to date

Before proceeding, let us make sure that our server repos and packages are in the latest versions by updating using this command:

sudo apt update
sudo apt upgrade -y

2. Installing MongoDB Community Edition

The Mongodb packages are not available in the default Ubuntu repos. To add the repo, do the following:

Import the public key used by the package management system. From a terminal, issue the following command to import the MongoDB public GPG Key from https://www.mongodb.org/static/pgp/server-5.0.asc:

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

The operation should respond with an OK.

However, if you receive an error indicating that gnupg is not installed, you can install gnupg and its required libraries using the following command.

sudo apt-get install gnupg

Create the list file using the command for your version of Ubuntu:

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

Issue the following command to reload the local package database:

sudo apt update

You can install either the latest stable version of MongoDB or a specific version of MongoDB

sudo apt install -y mongodb-org

If you run into issues installing related to libssl like shown below?

$ sudo apt install -y mongodb-org
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

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
E: Unable to correct problems, you have held broken packages.

You can install the package from http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/.

curl -LO http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1-1ubuntu2.1~18.04.20_amd64.deb
sudo dpkg -i ./libssl1.1_1.1.1-1ubuntu2.1~18.04.20_amd64.deb

Once installed, check the version of MongoDB installed using the following command:

$ mongod -version
db version v6.0.0
Build Info: {
    "version": "6.0.0",
    "gitVersion": "e61bf27c2f6a83fed36e5a13c008a32d563babe2",
    "openSSLVersion": "OpenSSL 1.1.1  11 Sep 2018",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "ubuntu2204",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

4. Starting and Enabling MongoDB Service

Once the service has been installed, it will not be started by default. Start the mongodb service using this command:

sudo systemctl start mongod

Confirm that the service is up and running by checking its status:

$ sudo systemctl status mongod
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
     Active: active (running) since Tue 2022-08-09 01:59:04 UTC; 8s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 17388 (mongod)
     Memory: 64.8M
        CPU: 905ms
     CGroup: /system.slice/mongod.service
             └─17388 /usr/bin/mongod --config /etc/mongod.conf

Aug 09 01:59:04 local-kip-ubuntu2204-srv systemd[1]: Started MongoDB Database Server.

Our mongodb service is up and running. Let us enable it to start on boot using this command:

sudo systemctl enable mongod

5. Connecting to Mongodb and Executing some test commands

Use the mongo command to connect to the mongo shell.

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

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.

------
   The server generated these startup warnings when booting
   2022-08-09T01:59:04.457+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
   2022-08-09T01:59:05.281+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
   2022-08-09T01:59:05.281+00:00: vm.max_map_count is too low
------

------
   Enable MongoDB's free cloud-based monitoring service, which will then receive and display
   metrics about your deployment (disk utilization, CPU, operation statistics, etc).

   The monitoring data will be available on a MongoDB website with a unique URL accessible to you
   and anyone you share the URL with. MongoDB may use this information to make product
   improvements and to suggest MongoDB products and deployment options to you.

   To enable free monitoring, run the following command: db.enableFreeMonitoring()
   To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
------

test> db.version()
6.0.0

Let us execute some test commands to confirm its proper workings:

> use citizix_db;
switched to db citizix_db
>
> db.users.save({
...     name: "etowett",
...     location: "Arctic Vault"
... });
WriteResult({ "nInserted" : 1 })
>
> db.users.find();
{ "_id" : ObjectId("619d34f11e21af9862e25c5d"), "name" : "etowett", "location" : "Arctic Vault" }
>

6. Uninstall MongoDB Community Edition

To completely remove MongoDB from a system, you must remove the MongoDB applications themselves, the configuration files, and any directories containing data and logs.

This process will completely remove MongoDB, its configuration, and all databases. This process is not reversible, so ensure that all of your configuration and data is backed up before proceeding.

The following section guides you through the necessary steps.

Stop MongoDB

Stop the mongod process by issuing the following command:

sudo service mongod stop

Remove Packages.

Remove any MongoDB packages that you had previously installed.

sudo apt remove mongodb-org

Remove Data Directories.

Remove MongoDB databases and log files.

sudo rm -r /var/log/mongodbsudo rm -r /var/lib/mongo

Conclusion

In this guide, we managed to Install and do some basic operations with MongoDB on our Ubuntu Server.

I am a Devops Engineer, but I would describe myself as a Tech Enthusiast who is a fan of Open Source, Linux, Automations, Cloud and Virtualization. I love learning and exploring new things so I blog in my free time about Devops related stuff, Linux, Automations and Open Source software. I can also code in Python and Golang.

Write A Comment