Cloud Service >> Knowledgebase >> Database >> How to Install MongoDB on Ubuntu in 2024
submit query

Cut Hosting Costs! Submit Query Today!

How to Install MongoDB on Ubuntu in 2024

MongoDB is a popular NoSQL database recognized for its high performance, flexibility, and scalability. This guide will walk you through setting up MongoDB on a 2024 Ubuntu system. Whether you're putting in an improvement environment or making ready a manufacturing server, those steps will help you get MongoDB up and going for walks.

Prerequisites

Before you begin, ensure you have the following:

- An Ubuntu 20.04 or later system.

- A user account with sudo privileges.

- Internet access for downloading the required packages.

 

Step 1: Update System Packages

First, replacing your gadget's package index is vital to ensure you have the trendy facts about to-be-had packages.

sudo apt update

sudo apt upgrade -y

 

Step 2: Import the MongoDB GPG Key

MongoDB provides a GPG key for package verification. Import this key to your system to ensure the packages are from the correct source.

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

You should see an OK message indicating that the key was successfully added.

 

Step 3: Create a MongoDB Source List

Next, create a MongoDB list file for apt to get the MongoDB packages from the official MongoDB repository.

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

Replace focal with your Ubuntu launch name in case you use a version apart from Ubuntu 20.04.

 

Step 4: Update Package Index Again

After adding the MongoDB repository, update the package index again to include the MongoDB packages.

sudo apt update

 

Step 5: Install MongoDB

Now, install MongoDB using the apt package manager.

sudo apt install -y MongoDB-org

This command will install the latest stable version of MongoDB along with its associated tools.

 

Step 6: Start and Enable MongoDB Service

After the installation, start the MongoDB service and enable it to start on boot.

sudo systemctl start mongod

sudo systemctl enable mongod

You can check the status of the MongoDB service to ensure it's running correctly.

sudo systemctl status mongod

If MongoDB is walking efficiently, you should see an output indicating that the carrier is energetic and jogging.

 

Step 7: Verify MongoDB Installation

Hatch up with the MongoDB server to verify the installation to use the Mongo shell.

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

You ought to see an output indicating a hit connection to the MongoDB server in conjunction with a few additional statistics about the hosting server.

 

Step 8: Configuring MongoDB

By default, MongoDB listens on port 27017 and is configured to accept connections handiest from localhost simply. You can adjust the configuration record to permit remote connections or alternate settings.

The MongoDB configuration file is located at:

Sudo nano /etc/mongod.conf

Find the binder under the net segment to permit far-flung connections and upload your server's IP deal with it. You can also use zero. 0.0.Zero will allow connections from all IP addresses.

 

Net:

port: 27017

bind: 127.0.0.1,192.168.1.100  # Replace with your server's IP address

After making changes to the configuration file, restart MongoDB to apply the changes.

sudo systemctl restart mongod

 

Step 9: Securing MongoDB

For a production environment, it's crucial to secure your MongoDB instance. Start by creating an administrative user.

Connect to MongoDB shell:

mongo

Switch to the admin database:

use admin

Create an administrative user:

db.createUser({

user: "admin,"

pwd: "your password,"  // Replace with a strong password

roles: [{ role: "userAdminAnyDatabase," db: "admin"}]

})

 

Enable authentication by editing the MongoDB configuration file:

Sudo nano /etc/mongod.conf

Add the following lines under the security section:

 

Security:

authorization: "enabled"

Restart MongoDB:

sudo systemctl restart mongod

Now, to connect to MongoDB, you will need to authenticate:

mongo -u admin -p --authenticationDatabase admin

 

Step 10: Enabling Firewall (Optional)

If your server is available from the net, allowing the firewall to restrict the right of entry to MongoDB is really useful. Use ufw to enable entry to port 27017 only from unique IP addresses.

Sudo ufw allow from your_ip_address to any port 27017

Enable the firewall:

Sudo ufw enable

 

Conclusion

You have successfully installed MongoDB on your Ubuntu system by following these steps. You have also secured your installation by enabling authentication and configuring the firewall. MongoDB is now ready for use, whether for development or production purposes. Always regularly update your packages and back up your data to ensure smooth and secure operations.

 

If you encounter any issues or need further assistance, refer to the MongoDB documentation or seek help from the community.

Cut Hosting Costs! Submit Query Today!

Grow With Us

Let’s talk about the future, and make it happen!