How to Install MongoDB on Ubuntu

You require a powerful and flexible database system like MongoDB to manage vast amounts of information. Its document-oriented architecture allows for the storage and retrieval of unstructured or semi-structured data, making it an ideal choice for various applications. If you're looking to harness the capabilities of MongoDB on your Ubuntu system, here’s a step-by-step guide to walk you through MongoDB installation on Ubuntu.

What is MongoDB?

MongoDB is a widely-used open-source, document-oriented NoSQL database that stores data in JSON-like BSON (Binary JSON) format. It’s different from traditional relational databases because it doesn't depend on a fixed schema, making it exceptionally adaptable to evolving data structures. 

This flexibility makes it an excellent choice for projects that involve large amounts of unstructured or semi-structured data, such as social media sites, content management systems, and real-time analytics.

Each document contains key-value pairs, providing a more intuitive representation of data, especially for developers accustomed to working with JavaScript and similar languages.

Prerequisites

  • Ubuntu operating system with non-root administrative user

Step-by-Step MongoDB Installation Process

Here are the steps to follow to install MongoDB on Ubuntu.

Update The System Repositories

Before installing any new software, it's essential to ensure your system is up to date. Open a terminal and execute the following commands:

sudo apt update

sudo apt upgrade

Add MongoDB Repository

Ubuntu’s package repositories usually have a stable version of MongoDB. However, these default repositories might not always have the latest version of MongoDB. Adding the official MongoDB repository ensures you get the most recent stable release. 

To start the process, you need to import the public key for MongoDB using the wget command: 

sudo apt install gnupg

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

Once the public key is added, the output will display “OK.”

Next, you need to add the MongoDB repository to your sources list:

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

Install MongoDB on Ubuntu

Having introduced the repository, it's time to install MongoDB on Ubuntu itself. However, before proceeding, refresh the local package index using this command: 

sudo apt update

With this command, you will refresh all local repositories and make Ubuntu aware of the new MongoDB repository.

Install the meta package by running this command:

sudo apt install mongodb-org

Start MongoDB

Although MongoDB is now installed, it's not automatically started. You need to enable and start the MongoDB service. To start it, use this command:

sudo systemctl start mongod

Verify and Enable MongoDB

Next, confirm whether the service is running using this command:

sudo systemctl status mongod

If everything is in order, you should see the "active (running) "message indicating that MongoDB is operating. After verifying that the service is okay, you can enable MongoDB to start on boot using this command: 

sudo systemctl enable mongod

How To Create a Database and User in MongoDB

With MongoDB installed and operational, delve into the creation of databases and users.

Access the MongoDB Shell

Initiate interaction with MongoDB by running the following command:

mongosh

You’ll see some details, such as the MongoDB version and the URL for documentation. Additionally, you’ll see pertinent warnings with information on whether access control has been enabled on the database. 

Typically, three databases are created after installation: admin, config, and local.`To list them, run the command:

> show dbs

To create a database, run the “use” command followed by your database name. For instance, if the database is named “Team,” the command will be:

>use team

Create a New User

To manage your database, MongoDB allows you to create a new user. You have to add a username, password, and roles that you need to assign the user. All the details are presented in JSON format.

Here’s how this will appear:

db.createUser({

user: "<username>",

pwd: "<password>",

roles: [{ role: "readWrite", db: "mydatabase" }]

})

Replace the <username> and <password> with your desired values. You should also replace "mydatabase" with the name of the database you want the user to have access to. The “readWrite” section shows the user’s permissions. 

How To Secure MongoDB 

Databases mandate robust security measures. Although MongoDB has some security settings, it's essential to add more precautions. Without proper controls, any user accessing your database can view, delete or add data. This can harm your company because it exposes you to a potential data breach.

Here’s how to secure MongoDB:

Since you have the admin user in place, the next step is enabling authentication. Edit the MongoDB configuration file by running this command:

sudo nano /etc/mongod.conf

Locate the “security” section and uncomment the line #security: by removing the # at the beginning. Include the authorization directive and enable it. Save these changes and exit from the file. Subsequently, restart MongoDB to ensure that the changes have been saved:

sudo systemctl restart mongod

How To Configure MongoDB For Remote Access

Configuring MongoDB for remote access allows you to interact with the database on the same server. However, this must be done carefully to ensure security.

The first step is editing the main configuration file, which holds settings for networking, storage location, and process management. To edit this file, run: 

sudo nano /etc/mongod.conf

Find the line bindIp: 127.0.0.1 and replace it with the IP address of the machine you want to allow remote access from. If you want to allow access from any IP, replace it with bindIp: 0.0.0.0.

Save the changes and restart MongoDB as follows:

sudo systemctl restart mongod

Conclusion

MongoDB's adaptability and scalability make it a versatile choice for various applications, from content management systems to real-time analytics platforms. As you delve deeper into MongoDB's features and capabilities, you'll find yourself equipped to handle the challenges of modern data management and leverage its power to drive innovation. From this guide, you've not only learned how to successfully install MongoDB on your Ubuntu system but also learned how to create databases, users, secure MongoDB, and configure it for remote access. 

Items from an article
  • KVM NVMe VPS 8 GB
    • Country: France
    • City: Strasbourg
    • CPU 4 x Epyc Core
    • RAM 8 GB
    • HDD NVMe : 80GB
    • BANDWIDTH 1 Gbps port
    • IPv4 / IPv6
    € 40/month
    40/month
Blog company: