Site icon VPS Windows

How to Install MongoDB on Ubuntu 22.04 Server

Install MongoDB on Ubuntu 22.04

MongoDB is a popular, open-source NoSQL database known for its flexibility, scalability, and support for high-velocity data applications. Install MongoDB on a server like Ubuntu 22.04 gives developers a robust environment to host their applications. In this article, we’ll walk you through the process of installing MongoDB on an Ubuntu 22.04 server, setting up your database, and configuring it for secure, efficient performance.

What is MongoDB?

MongoDB is a NoSQL document database, meaning it stores data in flexible, JSON-like documents instead of traditional table-based structures. It is schema-less, which means it doesn’t require a fixed structure, making it perfect for dynamic and rapidly evolving data. MongoDB supports high-volume data handling and is popular for use cases including real-time analytics, content management, IoT applications, and more.

Key features of MongoDB:

In a nutshell, MongoDB stands as a versatile, powerful database choice that’s tailor-made for modern needs where scalability, flexibility, and performance align seamlessly.

Why Install MongoDB on Ubuntu 22.04?

Using MongoDB on Ubuntu 22.04 combines the latest features of MongoDB with the reliable and lightweight Ubuntu server environment. Advantages include:

Buying a VPS Ubuntu 22.04 at vpswindows.com

Before you can install MongoDB, you need a reliable VPS. At vpswindows.com, we offer optimized VPS solutions, including those running Ubuntu 24.04, designed to meet the needs of developers and businesses. Our VPS plans come with high-performance SSD storage, customizable resource allocation, and full root access. With 24/7 support and seamless scalability, our VPS options are perfect for hosting MongoDB and any other applications you want to run efficiently.

Here’s why using VPSWindows.com is a strategic choice:

Prerequisites

Before starting, ensure you have:

Install MongoDB on Ubuntu 22.04

The first step is to install the prerequisite packages needed during the installation. To do so, run the following command.

sudo apt install software-properties-common gnupg apt-transport-https ca-certificates -y

To install the most recent MongoDB package, you need to add the MongoDB package repository to your sources list file on Ubuntu. Before that, you need to import the public key for MongoDB on your system using the wget command as follows:

curl -fsSL https://pgp.mongodb.com/server-7.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg – – dearmor

Next, add MongoDB 7.0 APT repository to the /etc/apt/sources.list.d directory.

echo “deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list

The command adds the MongoDB 7.0 sources list file to the /etc/apt/sources.list.d/ directory. This file contains a single line that reads:

echo “deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse”

Once the repository is added, reload the local package index.

sudo apt update

The command refreshes the local repositories and makes Ubuntu aware of the newly added MongoDB 7.0 repository.

With that out of the way, install the mongodb-org meta-package that provides MongoDB.

sudo apt install mongodb-org -y

The command installs the MongoDB database server along with the database core components including the shell tools. Once the installation is complete, verify the version of MongoDB installed:

mongod –version

This lists a bunch of information including the version, Git, and OPenSSL version among other details.

Start MongoDB service

The MongoDB service is disabled upon installation by default, and you can verify this by running the below command:

sudo systemctl status mongod

To start the MongoDB service, execute the command:

sudo systemctl start mongod

Once again, confirm if the service is running:

sudo systemctl status mongod


From the above output, you can see that MongoDB is up and running. Additionally, you can confirm that the database is up and running by checking if the server is listening on its default port which is port 27017.

To do so, run the following ss command:

sudo ss -pnltu | grep 27017

You should see the following output on your terminal.

After you have verified the service is running as expected, you can now enable MongoDB to start on boot as shown.

sudo systemctl enable mongod

So far, MongoDB has successfully been installed and configured to start on boot.

Securing MongoDB on Ubuntu

Authentication is not enabled by default in MongoDB, which means that any user with access to the database server can view, add, and delete data. This vulnerability can cause a serious breach of your data, which is why securing MongoDB is important. In this section, we will show you how you can secure MongoDB on Ubuntu 22.04.

Access Mongo Shell

The first step is to create an administrative user, and to do so, first access Mongo Shell.

mongosh

Connect or switch to the admin database.

use admin

Create an admin user named Adminvps

Then create a database user by pasting these lines and pressing ENTER on your keyboard.

admin> db.createUser(
{
user: “Adminvps”,
pwd: passwordPrompt(),
roles: [ { role: “userAdminAnyDatabase”, db: “admin” }, “readWriteAnyDatabase” ] }
)

In there:

Run the exit command or press CTRL + C to exit the Mongo Shell.

Enable Authentication:

With the Adminvps Administrator user already in place, the next step is to enable authentication. To do this, exit the MongoDB shell and open the mongod.conf file.

vi /etc/mongod.conf

Go to security section, remove comment (#) add authorization and set it to enabled

security:
authorization: enabled

Note that the authorization parameter is indented while security has no leading space.

Restart MongoDB

Save the changes and exit the configuration file. To apply the changes, restart MongoDB as shown.

systemctl restart mongod

Check Status

systemctl status mongod

Log into MongoDB Shell, now no longer see the warning as above

mongosh

However, if you try to perform any database related tasks, such as viewing the database, you will get some output suggesting that authentication is required.

show dbs

To authenticate, first log out of Mongo Shell by running the exit command.

Log in as the admin user as follows.

mongosh -u Adminvps -p –authenticationDatabase admin

From now on, only the admin user can view, create, and modify data in the database.

To exit the shell, run the exit command.

This tutorial covered how to install MongoDB on Ubuntu 22.04 and start using this powerful open-source database. On their official website, you can find further documentation on MongoDB’s advanced features and use cases.

Conclusion

Install MongoDB on Ubuntu 22.04 is straightforward and provides your server with a robust, high-performance database solution. MongoDB’s flexibility and scalability make it ideal for various applications, from small projects to large-scale, real-time data processing systems.

With this guide, you’ve not only install MongoDB but also configured it securely, performed basic commands, and learned how to back up and restore data. A properly configured MongoDB database on an Ubuntu 22.04 server will empower you to build dynamic, scalable applications that meet the needs of modern web and enterprise applications.

Thank you for reading this tutorial! For more in-depth guides and useful tech knowledge, follow the VPSWindows blog. Don’t forget VPSWindows offers affordable, reputable, and quality VPS services to enhance your server performance and reliability.

Exit mobile version