Install MongoDB on Ubuntu: A Step-by-Step Tutorial

Prerequisites for Installing MongoDB on Ubuntu


Prerequisites for Installing MongoDB on Ubuntu

If you’re planning to install MongoDB on Ubuntu, there are several prerequisites that you need to consider before installing the software. MongoDB is a popular open-source NoSQL database that is designed to be scalable and flexible. It is widely used for building applications that require high-performance and real-time data processing. In this tutorial, we will explore the prerequisites for installing MongoDB on Ubuntu.

1. Operating System

Before installing MongoDB on Ubuntu, you must ensure that your system is running on a supported Ubuntu version. MongoDB is supported on Ubuntu 16.04, 18.04, and 20.04. Therefore, you must choose one of these versions to run on your system if you want to install MongoDB. If you’re running an older version of Ubuntu or a different distribution of Linux, you may need to upgrade or migrate to a new version of Ubuntu that is compatible with MongoDB.

2. Minimum System Requirements

Before installing MongoDB on your system, you should ensure that your system meets the minimum hardware and software requirements. The system requirements may vary depending on the version of MongoDB you’re installing, but in general, you should have at least 2 GB of RAM and 3 GB of free disk space. You should also have a compatible CPU and network interface card (NIC) that is supported by the Ubuntu version you’re running.

3. User Permissions

When installing MongoDB on Ubuntu, you must ensure that you have the necessary user permissions to install and configure the software. Typically, you will need root or sudo privileges to install MongoDB on Ubuntu. This is because some of the installation steps require administrative privileges to create new files, directories, and users on your system. Therefore, you should log in as a root user or use the sudo command when installing and configuring MongoDB.

4. MongoDB Community Edition

Finally, before installing MongoDB on Ubuntu, you should ensure that you have downloaded the latest version of MongoDB Community Edition. MongoDB offers several editions of its software, including Enterprise, Atlas, and Community. The Community Edition is a free and open-source version of MongoDB that is comprehensive and suitable for most use cases. You can download the MongoDB Community Edition from the official MongoDB website by selecting the Ubuntu version that you’re running.

After downloading the MongoDB Community Edition, you can follow the installation instructions provided in the MongoDB documentation to install and configure MongoDB on Ubuntu. These instructions will guide you through the setup process, including creating a new MongoDB user, configuring network settings, and starting the MongoDB service.

In conclusion, installing MongoDB on Ubuntu requires careful consideration of several prerequisites, including the operating system version, minimum system requirements, user permissions, and MongoDB edition. By ensuring that you meet these prerequisites, you can install and configure MongoDB properly and benefit from its powerful features and capabilities.

Installing MongoDB via Package Manager


Installing MongoDB via Package Manager

Installing MongoDB via Package Manager is one of the easiest ways to get MongoDB up and running on your Ubuntu machine. By using the Package Manager, you can install MongoDB with a single command. This guide will show you how to install MongoDB via Package Manager on Ubuntu.

Step 1: First, open the terminal on your Ubuntu machine by pressing Ctrl+Alt+T. This will launch the terminal window.

Step 2: Next, you need to import the MongoDB public key into your system. To import the key, use the following command:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10

This command will import the MongoDB public key into your system.

Step 3: After importing the key, you need to add the MongoDB repository to the sources list. To do this, create a file named /etc/apt/sources.list.d/mongodb-org-4.2.list with the following contents:

deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -sc)/mongodb-org/4.2 multiverse

Step 4: After creating the file, run the following command to update the package list:

sudo apt-get update

Step 5: Once the package list is updated, run the following command to install MongoDB:

sudo apt-get install -y mongodb-org

This command will install MongoDB on your system. Once installed, MongoDB will start automatically.

Step 6: To check if MongoDB is running, use the following command:

systemctl status mongod

This command will display the status of MongoDB and whether it is running or not.

Step 7: By default, MongoDB is configured to start automatically when the system boots up. If you want to disable this behavior, use the following command:

sudo systemctl disable mongod

This command will disable the automatic startup of MongoDB.

Step 8: To enable MongoDB to start automatically at boot time, use the following command:

sudo systemctl enable mongod

This command will enable the automatic startup of MongoDB.

By following these simple steps, you can easily install MongoDB via Package Manager on your Ubuntu machine. Once installed, you can start using MongoDB to store and manipulate data.

Installing MongoDB from Official MongoDB Repository


Official MongoDB Repository

Installing MongoDB on Ubuntu is quite easy, thanks to the Official MongoDB Repository. In this tutorial, we will show you how to install MongoDB on Ubuntu using the Official MongoDB repository.

Before we proceed with the installation process, make sure that your Ubuntu system is up to date by running the following commands:

sudo apt update
sudo apt upgrade

Once your system is up to date, you can proceed with the installation process. Follow the steps given below:

Step 1: Add the MongoDB GPG Key

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

This command will download the MongoDB GPG key and add it to your Ubuntu System.

Step 2: Create the MongoDB Repository File

Next, you need to create a MongoDB Repository file in the /etc/apt/sources.list.d/ directory. You can create it using the following command:

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

The above command will create a file called mongodb-org-5.0.list in the sources.list.d directory. This file will contain the links to the MongoDB repository.

Step 3: Update the Package List and Install MongoDB

Now that you have added the MongoDB repository to your Ubuntu system, you can update the package list using the following command:

sudo apt update

After updating the package list, you need to install the MongoDB packages using the following command:

sudo apt-get install -y mongodb-org

This command will install all the MongoDB packages, including the server, client, and other necessary tools.

Step 4: Start and Enable MongoDB

After installing MongoDB, you can start the MongoDB service using the following command:

sudo systemctl start mongod

Once you have started the MongoDB service, you can enable it to start automatically on system boot using the following command:

sudo systemctl enable mongod

Step 5: Verify MongoDB Installation

To verify that MongoDB is installed and working correctly, you can run the following command:

mongo --eval 'db.runCommand({ connectionStatus: 1 })' 

If everything is working correctly, you will see the following output:

{ 
    "authInfo" : {
        "authenticatedUsers" : [ ],
        "authenticatedUserRoles" : [ ]
    },
    "ok" : 1 
}

Congratulations! You have successfully installed MongoDB on your Ubuntu system using the Official MongoDB repository.

In conclusion, using the Official MongoDB repository is the recommended way to install MongoDB on Ubuntu. It is a simple and straightforward process that can be completed in just a few minutes. Follow these steps, and you’ll have MongoDB up and running on your Ubuntu system in no time.

Starting and Stopping MongoDB Service


Starting and Stopping MongoDB Service

If you have already installed MongoDB on your Ubuntu machine, you’ll need to know how to start and stop the MongoDB service. The service is a background process that runs on your system, and it’s responsible for running the MongoDB database.

There are a few different ways you can start and stop the MongoDB service depending on your system setup. In this tutorial, we’ll cover the most common methods:

Starting MongoDB Service

To start the MongoDB service in Ubuntu, you can use the following command:

sudo systemctl start mongod

Alternatively, you can start the service using the init script:

sudo service mongod start

You can also verify that the service is running by checking its status:

sudo systemctl status mongod

If the service is running correctly, the output will show its status as “active” or “running”.

Stopping MongoDB Service

To stop the MongoDB service in Ubuntu, you can use the following command:

sudo systemctl stop mongod

You can also stop the service using the init script:

sudo service mongod stop

If you need to restart the service, you can use the following command:

sudo systemctl restart mongod

This will stop and then start the service again.

Enabling MongoDB Service at Startup

If you want MongoDB to start automatically when you boot your Ubuntu machine, you can enable it as a system service using the following command:

sudo systemctl enable mongod

This will add the MongoDB service to the list of system services that are started automatically on boot.

Disabling MongoDB Service at Startup

If you want to disable MongoDB from starting automatically when you boot your Ubuntu machine, you can use the following command:

sudo systemctl disable mongod

This will remove the MongoDB service from the list of system services that are started automatically on boot.

Conclusion

Starting and stopping the MongoDB service in Ubuntu is a simple process that can be done using a few different methods. Whether you prefer to use the systemctl command or the init script, you can easily start or stop the service as needed. Additionally, enabling or disabling MongoDB from starting automatically on boot is also a straightforward task that can be accomplished with just a few commands.

With these tools at your disposal, you should be able to manage your MongoDB service on Ubuntu with ease.

Verifying MongoDB Installation and Configuration on Ubuntu


Ubuntu Logo

After installing MongoDB on Ubuntu, verifying the installation and configuration is crucial to ensure that the database management system is up and running correctly. Here are the essential steps to verify that your installation is successful:

Step 1: Check if MongoDB Service is Running

MongoDB Service is Running

After installing MongoDB, you should check whether the mongodb service is running on your Ubuntu system. Type the following command in the terminal to check the service status:

sudo systemctl status mongodb

If the service is running, you should see the following output:

● mongodb.service - MongoDB Database Server
   Loaded: loaded (/lib/systemd/system/mongodb.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2021-09-13 14:17:10 UTC; 8h ago
 Main PID: 1391 (mongod)
    Tasks: 23 (limit: 18461)
   Memory: 72.5M
   CGroup: /system.slice/mongodb.service
           └─1391 /usr/bin/mongod --config /etc/mongodb.conf

The Active: active (running) line indicates that the MongoDB service is running correctly.

Step 2: Test MongoDB Connection

MongoDB Connection

The next step is to test the MongoDB connection. Type the following command in the terminal:

mongo

This will start the MongoDB shell, and you should see the following prompt:

MongoDB shell version v4.4.9
...\>

Next, type in the following command to verify your MongoDB connection:

db.runCommand({ ping: 1 })

If the output of this command is { "ok" : 1 }, you are successfully connected to your MongoDB instance.

Step 3: Verify MongoDB Version

MongoDB Version

To verify the installed version of MongoDB, run the following command:

mongod --version

The output should be similar to the following:

db version v4.4.9
Build Info: {
    "version": "4.4.9",
    "gitVersion": "876ebee8c7dd0e2d4c6b8386bcbee82632b08268",
    "openSSLVersion": "OpenSSL 1.1.1f  31 Mar 2020",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "ubuntu1804",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

The db version line displays the installed version of MongoDB.

Step 4: Check MongoDB Logs

MongoDB Logs

MongoDB logs can provide useful information about the status and operation of the database. You can check the logs using the following command:

sudo tail -n 20 /var/log/mongodb/mongod.log

This will display the last 20 lines of the MongoDB log file. It can be used to troubleshoot issues in case of any problems arise.

Step 5: Create and Verify a Test Database

MongoDB Test Database

Creating a test database and verifying it will confirm MongoDB’s proper functioning on Ubuntu. Follow the steps below:

Step 5.1: Create a test database

You can create a test database using the following command:

use testdb

This will create a new database named testdb.

Step 5.2: Create a collection and insert data

You can create a collection in the testdb database using the following command:

db.createCollection("testcoll")

This creates a new collection named testcoll.

You can insert data into the collection using the following command:

db.testcoll.insert({
  "name" : "John",
  "age" : 30,
  "address" : {
    "street" : "123 Main St",
    "city" : "New York",
    "state" : "NY",
    "zip" : "10001"
  }
})

Step 5.3: Verify the data

You can verify the inserted data using the following command:

db.testcoll.find()

This will display all the data in the testcoll collection, including the data that you have just inserted.

Verifying your MongoDB installation is crucial to ensure that the database management system is up and running correctly. By following the above steps, you can verify the successful installation and configuration of MongoDB on your Ubuntu system.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top