Announcement

Collapse
No announcement yet.

How to Install MongoDB on Ubuntu 24.04

Collapse
X
Collapse
  •  

  • How to Install MongoDB on Ubuntu 24.04


    When it comes to databases, you can have either relational or non-relational databases. Relational databases follow the SQL syntax, and their data is stored in structured tables. Examples of such databases are MySQL. However, non-relational databases store data in other formats, such as graphs, documents, etc., and a good example is MongoDB.

    MongoDB is a non-relational database that stores data as documents. The database is open-source, and by adding its repository to your source list, you will manage to install it on your Ubuntu 24.04. This post walks you through every required step.

    MongoDB Installation Guide on Ubuntu 24.04

    There are numerous reasons to choose MongoDB over other databases. For instance, if you want to achieve scalability and flexibility, MongoDB is the ideal non-relational database. Besides, MongoDB accommodates 32 and 64-bit systems.

    To install MongoDB, use the following steps.

    Step 1: Update the Repository

    When installing MongoDB, the recommended approach is to get its latest version by adding the MongoDB repository to your source list. Thus, we must first refresh the package index by updating it.

    $ sudo apt update




    Step 2: Install Dependency Packages

    Before we install MongoDB, a few packages need to be available on our Ubuntu 24.04. Some of them are already installed but you should run the below command to handle these dependency packages.

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




    Step 3: Import MongoDB’s GPG Key

    When installing packages from a third-party repository, you should import a GPG key to verify the authenticity of the packages to ensure no harm comes to your system. For the MongoDB repository, we will import the below GPG key.

    $ wget -qO- https://pgp.mongodb.com/server-7.0.asc | gpg --dearmor | sudo tee /usr/share/keyrings/mongodb-server-7.0.gpg >/dev/null




    Step 4: Add MongoDB’s Repository

    The next step is to add the MongoDB repository for your Ubuntu Noble Numbat. We’ve used the echo command to add the repository to our source list. The lsb_release -cs will fetch the codename for Ubuntu 24.04 and use it to fetch the respective MongoDB repository.

    $ echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/7.0 multiverse" | sudo tee -a /etc/apt/sources.list.d/mongodb-org-7.0.list




    Again, update your repository to refresh the newly added repository.

    $ sudo apt update


    Step 5: Install MongoDB

    With the MongoDB repository on our system, we can now install MongoDB using the following command.

    $ sudo apt install mongodb-org




    Step 6: Enable and Start the MongoDB Service

    After installing MongoDB, we still need to enable it and check to ensure that it is active and running. We will reference the mongod.service with the following commands.

    $ sudo systemctl enable mongod.service

    $ sudo systemctl status mongod.service




    Step 7: Test the MongoDB Connection

    We’ve installed MongoDB on our system, but we need to test the connection to ensure everything checks out. An excellent way is by running a connection status using the mongosh shell tool. If the connection to the database can be established, we will get an ‘OK’ return status.

    $ mongosh --eval 'db.runCommand({ connectionStatus: 1 })’




    Step 8: Configure MongoDB for Better Security

    MongoDB has basic configurations that work okay for basic usage. However, it’s advisable to edit the security section of its configuration file, especially if using it in a production environment. Open your MongoDB’s config file using a text editor and edit the security section to have it as in the image below.



    Save the file, exit the text editor, and restart the MongoDB service.

    $ sudo systemctl restart mongod.service




    Step 9: Create an Admin User

    As part of enhancing your database security, you need to create an administrative user. To do so, access the MongoDB shell with the below command and select which database you want to use.

    $ mongosh

    $ use admin




    Next, create an admin user for your database and set their password and username. Moreover, set their roles, and lastly, quit the shell to close it.



    To verify that your admin user was successfully created, log in to your MongoDB using their username and the assigned password.

    $ mongosh -u username -p




    Once you enter your password, you should gain access to MongoDB. The last step is to select the admin database and display the available users.



    The output should confirm that your admin user exists.

    That’s how to install MongoDB on Ubuntu 24.04.

    Conclusion

    MongoDB is a preferred non-relational database for developers working on scalable projects. To install MongoDB, add its repository to your source list, then install it using APT. Next, configure the database and set your admin user. We’ve discussed what steps you should follow and give practical examples.





    More...
      Posting comments is disabled.

    Categories

    Collapse

    Article Tags

    Collapse

    There are no tags yet.

    Latest Articles

    Collapse

    • How to Disable UEFI Secure Boot on a Proxmox VE Virtual Machine
      by Kasimba
      For GPU passthrough to work or for installing specific device drivers on a Proxmox VE virtual machine, you may need to disable UEFI Secure Boot on your Proxmox VE virtual machine.

      In this article, I will show you how to disable UEFI secure boot on a Proxmox VE virtual machine.

      The procedures shown in this article will also work on any KVM/QEMU/libvirt virtual machines and virtual machines running on other virtualization platforms based on the KVM/QEMU/libvirt hyperviso...
      Yesterday, 07:36 AM
    • Simplify Your Life with Taskwarrior's Intuitive Linux Job Scheduling
      by Kasimba



      by George Whittaker


      Introduction

      In the digital age, the ability to effectively manage time and tasks is invaluable, especially for those who work in technology and software development. Linux users, known for their preference for powerful, flexible tools, have various options for task management and scheduling. One of the standout tools in this area is Taskwarrior, a command-line task management utility that...
      Yesterday, 07:36 AM
    • How to Set and Use Environment Variables In Bash Script
      by Kasimba
      In bash scripting, environment variables are used to save and manage the data to modify the processes in the system. Using the bash scripts, you can use the environment variables to configure the system process. Environment variables also offer security and transmit the information from one script to another.
      Hence, there are multiple uses of environment variables that you can use to enhance the automation in the Linux system. So, if you want to learn the simple ways to set and use environment...
      05-03-2024, 08:50 PM
    • How to Check Bash Version
      by Kasimba
      Checking the bash version helps Linux users in many cases, including troubleshooting and finding the currently available version in the system. If you are a regular bash user, it is good to check the bash version and keep it up to date as per the latest updates available. However, if you are a newbie in bash scripting, then you probably have never encountered a situation where you need to check the bash version.
      So, in this guide, we will explain the simplest way to check the bash version...
      05-03-2024, 08:50 PM
    • What is Shebang: Bash Script Header on First Line?
      by Kasimba
      As Linux enthusiasts, we all come across bash scripting as an essential part of the Linux system. However, many Linux users don’t know why the first line of the bash script contains #!. So this combination of # and ! is called shebang or shebang.
      Although shebang looks like a bash script header, it initially instructs the system about interpreters to execute the script. This concept feels a bit complicated, but that’s not true, as this guide is all about the shebang: bash script header....
      05-02-2024, 07:59 PM
    • Bash Script Loops Examples
      by Kasimba
      A loop in programming is a control structure that allows a specific code to be executed repeatedly until a condition is met. This process is repeated until no further action is required. Loop allows you to repeat the desired set of instructions numerous times to attain the desired outcome. These recursions can be useful for all tasks that require repetitive operations or when working with data collections.
      In this article, we will explore the meaning, types, and examples of loops, further...
      05-02-2024, 07:59 PM
    Working...
    X