Easily Install MongoDB on Ubuntu 24.04

|
Published:
|
|
install mongodb

In this tutorial to learn how to easily install MongoDB on Ubuntu 24.04. According to MongoDB page, “MongoDB is a general purpose, document-based, distributed database built for modern application developers and for the cloud era...It is a scalable, flexible NoSQL document database platform designed to overcome the relational databases approach and the limitations of other NoSQL solutions.

Installing MongoDB on Ubuntu 24.04

Features of MongoDB

Read about the top features of MongoDB by following the link below.

Top 5 features of MongoDB

MongoDB is available in both a community and an enterprise version;

  • MongoDB Community is the source available and free to use edition of MongoDB.
  • MongoDB Enterprise is available as part of the MongoDB Enterprise Advanced subscription and includes comprehensive support for your MongoDB deployment. It also adds enterprise-focused features such as LDAP and Kerberos support, on-disk encryption, and auditing.

In this tutorial, we will learn how to install MongoDB community edition on Ubuntu 24.04.

Install MongoDB Community Edition on Ubuntu 24.04

Install MongoDB APT Repo on Ubuntu 24.04

To be able to install the latest stable versions of MongoDB on Ubuntu 24.04, you need to install MongoDB APT repository list.

According to MongoDB release notes page, MongoDB 7.0 is the current stable LTS release version, as of this writing.

Install the MongoDB APT repo signing key;

sudo apt install gnupg

Replace value of the VER variable below with the correct MongoDB release version number.

VER=7.0
wget -qO - https://www.mongodb.org/static/pgp/server-${VER}.asc | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/mongo.gpg

Next, install MongoDB APT Repository. Note that, as of this writing, there are no offical MongoDB repos released for Ubuntu 24.04 Noble Numbat. As such, we will use Ubuntu 22.04 repositories.

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

In the future when you are installing MongoDB, repos might have been released, and instead of the command above, use the command below to install the repository.

echo "deb [arch=amd64,arm64] https://repo.mongodb.org/apt/ubuntu ${lsb_release -sc}/mongodb-org/${VER} multiverse" | sudo tee /etc/apt/sources.list.d/mongodb.list

Update package cache;

sudo apt update

Install MongoDB Community Edition on Ubuntu

You can now install the latest version of MongoDB.

sudo apt install -y mongodb-org

Running MongoDB on Ubuntu 24.04

When installed, MongoDB creates a systemd unit file called, mongod.service, under /lib/systemd/system/. This service unit file can be used to manage MongoDB.

To start MongoDB service, execute the command;

sudo systemctl start mongod

To check the status;

systemctl status mongod
● mongod.service - MongoDB Database Server
     Loaded: loaded (/usr/lib/systemd/system/mongod.service; disabled; preset: enabled)
     Active: active (running) since Sun 2024-04-21 06:10:47 UTC; 5s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 9888 (mongod)
     Memory: 72.5M (peak: 73.0M)
        CPU: 463ms
     CGroup: /system.slice/mongod.service
             └─9888 /usr/bin/mongod --config /etc/mongod.conf

Apr 21 06:10:47 noble-numbat systemd[1]: Started mongod.service - MongoDB Database Server.
Apr 21 06:10:47 noble-numbat mongod[9888]: {"t":{"$date":"2024-04-21T06:10:47.606Z"},"s":"I",  "c":"CONTROL",  "id":7484500, "ctx":"main","msg":"Environment variable MONGODB_CONF

To enable it to start on boot;

sudo systemctl enable mongod

You can restart or stop is by executing the commands below respectively;

sudo systemctl restart mongod
sudo systemctl stop mongod

Connecting to MongoDB on Ubuntu

By default, MongoDB listens on TCP port 27017 by default, on a localhost.

sudo apt install net-tools
ss -altnp | grep :27
LISTEN 0      4096       127.0.0.1:27017      0.0.0.0:*    users:(("mongod",pid=9888,fd=13))

In order to connect to it, you need to be logged in to the same host on which MongoDB is running and simply execute the command below;

mongosh

Or;

mongosh "mongodb://localhost:27017"

The command above will launch an interactive MongoDB shell;

Current Mongosh Log ID:	6624af029b974a7352c934dc
Connecting to:		mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.4
Using MongoDB:		7.0.8
Using Mongosh:		2.2.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
   2024-04-21T06:10:47.674+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
   2024-04-21T06:10:48.114+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
------

test>

By default, mongosh prompt will show the current default database (test, for example above). You can customize the shell prompt to your liking.

To obtain help while logged into the MongoDB interactive shell, run the help command.

help
test> help

  Shell Help:

    use                                        Set current database
    show                                       'show databases'/'show dbs': Print a list of all available databases.
                                               'show collections'/'show tables': Print a list of all collections for current database.
                                               'show profile': Prints system.profile information.
                                               'show users': Print a list of all users for current database.
                                               'show roles': Print a list of all roles for current database.
                                               'show log ': log for current connection, if type is not set uses 'global'
                                               'show logs': Print all logs.

    exit                                       Quit the MongoDB shell with exit/exit()/.exit
    quit                                       Quit the MongoDB shell with quit/quit()
    Mongo                                      Create a new connection and return the Mongo object. Usage: new Mongo(URI, options [optional])
    connect                                    Create a new connection and return the Database object. Usage: connect(URI, username [optional], password [optional])
    it                                         result of the last line evaluated; use to further iterate
    version                                    Shell version
    load                                       Loads and runs a JavaScript file into the current shell environment
    enableTelemetry                            Enables collection of anonymous usage data to improve the mongosh CLI
    disableTelemetry                           Disables collection of anonymous usage data to improve the mongosh CLI
    passwordPrompt                             Prompts the user for a password
    sleep                                      Sleep for the specified number of milliseconds
    print                                      Prints the contents of an object to the output
    printjson                                  Alias for print()
    convertShardKeyToHashed                    Returns the hashed value for the input using the same hashing function as a hashed index.
    cls                                        Clears the screen like console.clear()
    isInteractive                              Returns whether the shell will enter or has entered interactive mode

  For more information on usage: https://docs.mongodb.com/manual/reference/method

And that is it. You can now start using your MongoDB database.

Uninstall MongoDB CE on Ubuntu 24.04

If for some reasons you want to uninstall MongoDB and remove it completely on Ubuntu 24.04, you would simply have to stop and purge its by running the commands below;

sudo systemctl stop mongod
sudo apt remove --purge --auto-remove mongodb-org

Remove MongoDB databases;

sudo rm -rf /var/lib/mongodb

Learn how to enable MongoDB authentication by following the link below;

Enable Authentication on MongoDB

Reference

Install MongoDB community Edition on Ubuntu

Further Reading

Getting Started with MongoDB

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
Kifarunix
Linux Certified Engineer, with a passion for open-source technology and a strong understanding of Linux systems. With experience in system administration, troubleshooting, and automation, I am skilled in maintaining and optimizing Linux infrastructure.

Leave a Comment