How to Install MongoDB on a Linux Server

MongoDB is a popular NoSQL database that is highly scalable and flexible, designed to handle large volumes of unstructured data. It provides high performance, high availability, and easy scalability for modern applications.

In this guide, we’ll show you how to install MongoDB on your Linux server.

Why Use MongoDB on Your Linux Server?

MongoDB on a Linux Server

MongoDB is widely adopted because of its flexibility in handling large datasets and unstructured data. It is designed to scale horizontally, making it suitable for both small projects and large, data-intensive applications.

  • Flexibility: MongoDB stores data in JSON-like format (BSON), which makes it easy to handle unstructured or semi-structured data.
  • Scalability: MongoDB supports horizontal scaling through sharding, making it perfect for applications with growing data.
  • High Performance: Thanks to its in-memory processing and flexible indexing, MongoDB can quickly access and process data.

Use cases for MongoDB include real-time analytics, content management systems, IoT applications, and big data solutions.

Prerequisites

Before you install MongoDB, ensure that:

  • You have root or sudo privileges on your server.
  • Your server is running a compatible Linux distribution such as Ubuntu, Debian, CentOS, or RHEL.
  • You have an active internet connection to download MongoDB and its dependencies.
  • You are familiar with basic command-line operations.

Install MongoDB on a Linux Server

Installing MongoDB on your Linux server is a straightforward process. The steps below guide you through the installation process, regardless of the Linux distribution you are using.

Update Your Package List

It’s essential to update your package repositories to ensure that you’re installing the latest version of MongoDB along with any necessary dependencies.

  • For Ubuntu/Debian-based systems:
sudo apt update
  • For CentOS/RHEL-based systems:
sudo yum update

This command will fetch the latest package updates and make sure that you’re working with the most up-to-date versions of the software.

Install MongoDB

Now that the package list is updated, you can install MongoDB. Depending on your Linux distribution, the installation process varies slightly.

For Ubuntu/Debian-based systems:

  • Install Required Dependencies
sudo apt install -y gnupg curl
  • Import the MongoDB Public GPG Key
curl -fsSL https://www.mongodb.org/static/pgp/server-6.0.asc | \
gpg --dearmor | \
sudo tee /usr/share/keyrings/mongodb-server-6.0.gpg > /dev/null
  • Add the MongoDB source list to your system:
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
  • Install MongoDB:
sudo apt update sudo apt install -y mongodb-org

For CentOS/RHEL-based systems:

  • Create the MongoDB repository file:
echo "[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/4.4/x86_64/
gpgcheck=1
enabled=1" | sudo tee /etc/yum.repos.d/mongodb-org-4.4.repo
  • Install MongoDB:
sudo yum install -y mongodb-org

Once the installation completes, MongoDB and its associated tools are installed on your system.

Check Out | How to Install MySQL on a Linux Server

Start and Enable MongoDB

After MongoDB is installed, you need to start the MongoDB service and enable it to start automatically with your system’s boot sequence.

To start MongoDB:

sudo systemctl start mongod

To enable MongoDB to start on boot:

sudo systemctl enable mongod

You can check the status of MongoDB to verify that it is running properly:

sudo systemctl status mongod

Test MongoDB Installation

Once MongoDB is up and running, test the installation by connecting to the MongoDB shell.

Run the following command:

mongo

This opens the MongoDB shell, where you can execute commands. To check the MongoDB version, you can run:

db.version()

You should see the version of MongoDB that you just installed.

Configure MongoDB

You can modify the default MongoDB configuration by editing the mongod.conf file. This file is usually located in /etc/mongod.conf.

Some common configuration changes include:

  • Bind IP: To allow MongoDB to listen on a specific IP address, modify the bindIp setting under net in the configuration file.
  • Port: You can change the default MongoDB port (27017) by modifying the port setting.

To edit the configuration file:

sudo nano /etc/mongod.conf

After making changes, restart MongoDB for the changes to take effect:

sudo systemctl restart mongod

Enable MongoDB Authentication

For added security, MongoDB can be configured to require authentication. To enable authentication:

  • Open the MongoDB configuration file:
  • Uncomment the security section and add authorization: "enabled":
security: authorization: "enabled"
  • Restart MongoDB:
sudo systemctl restart mongod
  • Create an admin user to authenticate with:
mongo
use admin
db.createUser({user: "admin", pwd: "your_password", roles: [{role: "userAdminAnyDatabase", db: "admin"}]})

Now, MongoDB requires a valid username and password for all operations.

Enable MongoDB Persistence

MongoDB supports persistence through journaling, which logs every operation and ensures that data is not lost in the event of a failure. By default, journaling is enabled.

To check the journaling settings or change the persistence behavior, edit the mongod.conf file under the storage section:

sudo nano /etc/mongod.conf

Make changes, save the file, and restart MongoDB.

Verify MongoDB Operation

To verify that MongoDB is running correctly, check the logs:

sudo less /var/log/mongodb/mongod.log

You can also run a few sample operations to ensure MongoDB is working:

mongo
use testdb
db.test.insert({name: "MongoDB"})
db.test.find()

Conclusion

MongoDB is a powerful and flexible NoSQL database that is ideal for handling large-scale, unstructured data. Following this guide, you have successfully installed MongoDB on your Linux server. Remember to secure your MongoDB instance and monitor its performance regularly to ensure it operates smoothly.

Leave A Comment