top of page

🗃️ How to Install MongoDB on Ubuntu in 2024

  • Foto del escritor: PixelHost
    PixelHost
  • 14 ago 2024
  • 6 Min. de lectura

MongoDB is one of the most popular open-source NoSQL database management systems. Instead of storing data in a tabular format like SQL, MongoDB stores data in a document format, allowing for greater scalability and flexibility.

This database management system (DBMS) is popular among developers due to its performance and compatibility with various programming languages. However, installing MongoDB on Ubuntu can be a bit challenging as users need to execute several commands.

With that in mind, we’ll explain how to install MongoDB on a virtual private server (VPS) with Ubuntu. We’ll also cover the steps to create a new database, set up a user, and enable remote authentication.


🛠️ What is MongoDB?

MongoDB is one of the most popular open-source NoSQL database management systems, widely used for large-scale applications or websites.

Unlike SQL, MongoDB stores data in a BSON document with a flexible schema, making it more scalable. It is also compatible with various operating systems, programming languages, and frameworks.


📋 Prerequisites for Installing MongoDB on Ubuntu

Before installing the official MongoDB package, check the compatibility of your hardware and software. Your VPS hosting plan should support Ubuntu, offer SSH access, and provide full root access.

PixelHost’s VPS hosting offers various Linux distributions, including Ubuntu. You can easily install it by going to the VPS Panel → Operating System and Panel.

We recommend using the latest operating system to ensure compatibility and security. However, the steps for installing MongoDB on Ubuntu 20.04 or other versions are similar.

Our VPS hosting plans also allow you to connect remotely via SSH. In addition to using applications like PuTTY and Terminal, you can execute Linux commands directly from your web browser using our Browser Terminal.

PixelHost’s VPS also provides full root access, allowing users to install MongoDB without permission issues.


🚀 How to Install MongoDB on Ubuntu

In this section, we will explain how to install MongoDB on Ubuntu 20.04 or other versions.

Before proceeding, make sure you are connected to your VPS via SSH using a root or superuser account.


  1. ⚙️ Installing MongoDB

    Before installing the MongoDB package, download GnuPG and the cURL utility. Then, use cURL and GnuPG to import the MongoDB public GPG key and obtain the installation package.

    Be sure to use the correct URL for your MongoDB package. For this tutorial, we will install MongoDB Community Edition 7.0, the latest stable version at the time of writing.

    After importing the official MongoDB packages, create a list file for installation. The command may vary depending on your Ubuntu version. Update the APT repository to synchronize the local package database, then install the latest stable version of MongoDB using the APT package management system.

    Optionally, you can choose a specific version of the mongodb-org package. If you specify only the MongoDB version and not the other components, APT will install the newest package available.

    Since APT automatically updates the component package, pin the installation to keep the current version. This prevents unintended upgrades.

    Common Issue: If you do not import the public GPG key into your local package database, APT may install an unofficial MongoDB package, leading to potential compatibility, security, and legal issues, as well as lack of support.

  2. 🔄 Starting the MongoDB Service

    After installation, start MongoDB Community Edition. The systemctl command is essential for managing the MongoDB service. If you encounter any errors, you may need to reload the system's service configuration and try starting MongoDB again.

    To verify if the MongoDB server is running correctly, check its status. If the service shows as "active (running)," MongoDB is functioning properly.

    By default, the service does not start automatically at boot. To ensure MongoDB loads automatically during startup, enable it using systemctl. The main MongoDB daemon process, called mongod, handles data requests, manages access, and performs background management operations.

  3. 🔧 Configuring MongoDB

    You can modify MongoDB's database settings by editing the main configuration file, which is automatically created during installation and is accessible through /etc/mongod.conf.

    Use a text editor to open and modify the file. There are various configuration options that determine the behavior of the MongoDB server, such as systemLog, which defines the database’s logging settings, and net, which allows you to modify network-related configurations.

    ⚠️ Warning: Be careful when editing the mongod configuration file, as you could break the service or expose your server to security risks.

    Instead of deleting parameters and options, it’s a good practice to disable them by adding a hash symbol (#) at the beginning. This way, you can easily reactivate them by removing the character. After making changes, restart MongoDB to apply the new settings.

  4. 📁 Creating a New Database

    The MongoDB installation process automatically creates the admin database, which is generally used for administrative purposes like storing authentication data. However, we recommend creating a new database.

    To do this, enter the Mongo shell. MongoDB uses the same command to create and access databases. If a database with the specified name already exists, the shell will switch to it.

    For example, you can create a new database named "customers" or switch to an existing one. To view all MongoDB databases on your system, use the appropriate command, and the terminal will list all available databases.

  5. 👤 Creating a New User

    Since MongoDB does not have a default account, you must create a new user for each database and assign privileges. You can do this by using the db.createUser() function.

    For instance, you can create a new MongoDB root user with full permissions. MongoDB offers various roles, such as dbAdmin, dbOwner, and readWrite. We recommend granting minimal privileges to avoid unauthorized modifications and data access.

    Important: MongoDB user management is database-specific, meaning you need to switch to the correct database before creating a user.

    To test the connection to the database, exit the MongoDB shell and reconnect using the appropriate user credentials.

  6. 🌐 Enabling Remote Authentication

    By default, MongoDB authorizes logins only from the host machine. While this is sufficient for local deployments, it may cause issues when running an application from another device.

    To access MongoDB from another system, modify the bindIP parameter in the mongod.conf file. The default value is set to the local IP address, which denies access from machines other than the host server.

    ⚠️ Warning: Be cautious when changing IP addresses in the MongoDB configuration file, as you could make your database publicly accessible, exposing your service to significant security risks.

    Add your VPS’s public IP address to the bindIP parameter in the mongod.conf file. Avoid changing the value to 0.0.0.0, as allowing connections from all IP addresses could expose your server to cyberattacks.

    To ensure that only permitted users can access your database, enable authentication by adding the authorization setting under the security section in the configuration file.

    After making these changes, save the file and restart MongoDB. Once you’ve installed the Mongo utility on your local system, you should be able to connect to the MongoDB server using the correct connection string.

    To enhance remote access security, limit MongoDB’s listening port to your local system’s IP address using the Uncomplicated Firewall (UFW) on Ubuntu.


📝 Conclusion

MongoDB is a popular, open-source NoSQL database management system, ideal for large-scale websites or applications. Its flexibility and scalability make it a preferred choice over SQL databases.

In this article, we’ve provided a step-by-step guide to installing MongoDB on Ubuntu. Here’s a summary:

  1. Installing MongoDB: Import the official MongoDB repository key using GnuPG and cURL, create the list file, and install it using the APT package manager.

  2. Starting MongoDB: Launch the MongoDB instance and enable the daemon to ensure it starts automatically during system boot.

  3. Configuring MongoDB: Open the mongod.conf configuration file with a text editor, modify your database settings, and restart MongoDB to apply the changes.

  4. Creating a New Database: Enter the Mongo shell and use the appropriate command to create a new MongoDB database or access an existing one.

  5. Creating a New User: Access the MongoDB shell and use the db.createUser() function to create a new user in your current database, setting their password and privileges.

  6. Enabling Remote Authentication: Modify the bindIP setting in the mongod.conf file to allow remote access to your database. To enhance security, enable MongoDB authentication and restrict access using UFW.

When deploying MongoDB, use a VPS with full root access and broad software compatibility like PixelHost to ensure a smooth setup process. We also offer a one-click OS installer and a browser-based terminal to simplify your tasks.

If you have any questions, feel free to ask!


Best Regards,

João @PixelHost


 
 
 

Commenti


bottom of page