Install Mongodb Ubuntu 20.04

admin9 April 2024Last Update :

Understanding MongoDB and Its Importance

MongoDB is a powerful, open-source NoSQL database that has gained significant popularity due to its flexible schema, scalability, and performance. It stores data in flexible, JSON-like documents, meaning fields can vary from document to document and data structure can be changed over time. This flexibility is particularly useful for applications that handle large volumes of diverse and evolving data.

Prerequisites for Installing MongoDB on Ubuntu 20.04

Before diving into the installation process, it’s essential to ensure that your system meets the necessary requirements. Here are the prerequisites for installing MongoDB on Ubuntu 20.04:

  • A machine running Ubuntu 20.04
  • Access to a terminal window/command line (Ctrl-Alt-T)
  • A user account with sudo privileges
  • An internet connection to download the necessary packages
  • Basic knowledge of Linux commands

Step-by-Step Guide to Installing MongoDB on Ubuntu 20.04

Step 1: Update the Package Database

Before installing any new software, it’s a good practice to update the package database. This ensures that you have the latest updates and dependencies.

sudo apt update
sudo apt upgrade

Step 2: Import the MongoDB Repository GPG Key

MongoDB is not included in the default Ubuntu repositories. To install it, you need to add the MongoDB repository to your system. First, import the MongoDB GPG key using the following command:

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

This command downloads the official MongoDB GPG key and adds it to your system’s list of trusted keys, ensuring that the packages you download are authentic.

Step 3: Add the MongoDB Repository

With the GPG key added, the next step is to add the MongoDB repository to your system’s list of sources. Create a list file for MongoDB using the following command:

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

This command creates a new file in /etc/apt/sources.list.d/ containing the repository information for MongoDB.

Step 4: Update the Package Database Again

After adding the repository, update the package database again to include the newly added MongoDB repository.

sudo apt update

Step 5: Install MongoDB Packages

Now you can install MongoDB by running the following command:

sudo apt install -y mongodb-org

This command installs several packages, including mongodb-org-server, mongodb-org-mongos, mongodb-org-shell, and mongodb-org-tools.

Configuring MongoDB on Ubuntu 20.04

Starting and Enabling the MongoDB Service

After installation, start the MongoDB service and enable it to start on boot with the following commands:

sudo systemctl start mongod
sudo systemctl enable mongod

Verifying the MongoDB Service Status

To ensure that MongoDB is running correctly, check the service status:

sudo systemctl status mongod

You should see an active (running) status indicating that MongoDB has started successfully.

Adjusting the MongoDB Configuration File (Optional)

If you need to adjust the default MongoDB configuration, edit the /etc/mongod.conf file using a text editor of your choice. This file allows you to change settings such as the port number, storage engine, and security options.

sudo nano /etc/mongod.conf

After making changes, save the file and restart the MongoDB service to apply them.

sudo systemctl restart mongod

Securing MongoDB on Ubuntu 20.04

Enabling Authentication

By default, MongoDB does not require authentication, which is not recommended for production environments. To enable authentication, you need to create an administrative user and then modify the MongoDB configuration file to enforce authentication.

mongo
use admin
db.createUser({
  user: "admin",
  pwd: "your_secure_password",
  roles: [{ role: "userAdminAnyDatabase", db: "admin" }]
})
exit

Next, open the MongoDB configuration file and enable authentication by adding the following line under the security section:

security:
  authorization: "enabled"

Restart the MongoDB service to apply the changes.

sudo systemctl restart mongod

Configuring Firewall Rules (Optional)

If you have a firewall enabled, you may need to configure it to allow traffic on MongoDB’s default port, 27017. Use the following command to allow traffic on this port:

sudo ufw allow 27017

Remember to adjust the firewall settings according to your security policies and network architecture.

Managing the MongoDB Service

Starting, Stopping, and Restarting MongoDB

You can manage the MongoDB service using the systemctl command. Here are the commands to start, stop, and restart MongoDB:

sudo systemctl start mongod
sudo systemctl stop mongod
sudo systemctl restart mongod

Checking the MongoDB Logs

To troubleshoot issues or monitor MongoDB activity, you can check the logs located at /var/log/mongodb/mongod.log. Use the following command to view the logs:

sudo tail -f /var/log/mongodb/mongod.log

Connecting to MongoDB

Accessing the MongoDB Shell

To interact with MongoDB, use the MongoDB shell by typing mongo in your terminal. If authentication is enabled, you’ll need to log in with a user account:

mongo -u admin -p your_secure_password --authenticationDatabase admin

Creating and Managing Databases and Collections

Within the MongoDB shell, you can create databases, collections, and documents. Here are some basic commands to get you started:

use myNewDatabase
db.createCollection("myCollection")
db.myCollection.insert({ name: "John Doe", age: 30 })
db.myCollection.find()

Backing Up and Restoring MongoDB Data

Using mongodump and mongorestore

To back up your MongoDB data, use the mongodump command. This creates a binary export of the contents of your database. To restore data, use the mongorestore command.

mongodump --db myNewDatabase --out /path/to/backup
mongorestore /path/to/backup

Uninstalling MongoDB from Ubuntu 20.04

Removing MongoDB Packages

If you need to uninstall MongoDB, use the following commands to remove the packages and data:

sudo systemctl stop mongod
sudo apt purge mongodb-org*
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb

Frequently Asked Questions

How do I update MongoDB to the latest version?

To update MongoDB, you can use the apt package manager. First, update the package database, then upgrade the packages:

sudo apt update
sudo apt upgrade mongodb-org

Can I install a specific version of MongoDB?

Yes, you can install a specific version by specifying the package version in the apt install command. For example:

sudo apt install -y mongodb-org=4.4.0 mongodb-org-server=4.4.0 mongodb-org-shell=4.4.0 mongodb-org-mongos=4.4.0 mongodb-org-tools=4.4.0

Is MongoDB free to use on Ubuntu?

MongoDB offers a Community Edition that is free to use. It is suitable for many applications and is available through the MongoDB repository.

How do I enable remote access to my MongoDB server?

To enable remote access, you need to edit the /etc/mongod.conf file and change the bindIp value under the net section to allow connections from other IP addresses. Don’t forget to update your firewall settings accordingly.

net:
  bindIp: 0.0.0.0

What should I do if I encounter errors during the installation?

If you encounter errors during the installation, check the MongoDB logs for detailed error messages. Ensure that you have added the MongoDB repository correctly and that your system meets all the prerequisites.

References

Leave a Comment

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


Comments Rules :

Breaking News