Learn How to Configure MongoDB on Linux: A Step-by-Step Guide

Configure MongoDB on Linux is a popular, open-source NoSQL database that offers high performance, high availability, and easy scalability. It’s designed to handle large amounts of unstructured data and is widely used for applications that require flexible schema management. Configuring MongoDB on Linux is a straightforward process that allows you to set up and optimize the database for your needs.

MongoDB on a Linux Server

In this article, we’ll guide you through the process of installing and configuring MongoDB on a Linux server.

Prerequisites

Before installing and configuring MongoDB on Linux, ensure that your system meets the following requirements:

  • Linux Distribution: MongoDB can be installed on a variety of Linux distributions, including Ubuntu, CentOS, and Debian.
  • Root Access: You will need root or sudo access to install MongoDB and modify system configurations.
  • System Requirements:
    • At least 2 GB of RAM (4 GB or more is recommended for production environments).
    • 10 GB of free disk space for MongoDB data storage.
    • A stable internet connection to download the necessary packages.
  • Software Dependencies: Make sure you have wget or curl installed to download the MongoDB packages.

Configure MongoDB on Linux

Configuring MongoDB on Linux involves installing the MongoDB server, adjusting system settings, and configuring the database for optimal performance. This process ensures a smooth setup for managing large-scale, high-performance databases with ease, making MongoDB a great choice for handling big data on Linux systems.

Install MongoDB on Linux

Installing MongoDB on Linux is simple and can be done via the official package repositories. Let’s go through the installation steps for popular distributions.

  • For Ubuntu/Debian

MongoDB is available in the official repositories for both Ubuntu and Debian. To install MongoDB, follow these steps:

sudo apt update
sudo apt install -y mongodb
  • For CentOS/RHEL

To install MongoDB on CentOS, first enable the MongoDB repository:

echo "[mongodb-org-5.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/centos/7/mongodb-org/5.0/x86_64/
gpgcheck=1
enabled=1" | sudo tee /etc/yum.repos.d/mongodb-org-5.0.repo

After adding the repository, install MongoDB:

sudo yum install -y mongodb-org

For both distributions, MongoDB is now ready to be configured.

Start and Enable MongoDB

Once MongoDB is installed, you need to start the MongoDB service and enable it to start automatically upon system boot.

Start MongoDB

  • For Ubuntu/Debian:
sudo systemctl start mongodb
  • For CentOS/RHEL:
sudo systemctl start mongod

Enable MongoDB to Start on Boot

Set MongoDB to automatically start when the server boots:

  • For Ubuntu/Debian:
sudo systemctl enable mongodb
  • For CentOS/RHEL:
sudo systemctl enable mongod

To confirm that MongoDB is running, use the following command:

sudo systemctl status mongodb    # Ubuntu/Debian
sudo systemctl status mongod      # CentOS/RHEL

Check Out | How to Configure Redis on Linux Server Step-by-Step Guide

Configure MongoDB for Production Use

After installation, there are several configuration settings you can modify to optimize MongoDB for production environments.

  • Edit MongoDB Configuration File

MongoDB’s configuration file is located at /etc/mongodb.conf on Ubuntu/Debian systems or /etc/mongod.conf on CentOS/RHEL. You can modify it using any text editor:

sudo nano /etc/mongod.conf      # Ubuntu/Debian
sudo nano /etc/mongodb.conf    # CentOS/RHEL
  • Bind IP Address

By default, MongoDB only listens on 127.0.0.1 (localhost), which means it can only be accessed from the same server. To allow remote access, change the bind IP to 0.0.0.0 or specify your server’s IP:

bindIp: 0.0.0.0

Alternatively, if you want to restrict access to specific IP addresses, you can replace 0.0.0.0 with the desired IP address.

  • Enable Authentication

MongoDB does not require authentication by default. However, it’s highly recommended to enable authentication in a production environment to secure your database. To enable authentication, find the following line in the configuration file and uncomment it:

security: authorization: "enabled"

After enabling authentication, you will need to create an admin user (covered below).

Create MongoDB Admin User

  • Access MongoDB Shell

Use the mongo shell to connect to the MongoDB instance:

mongo
  • Switch to the Admin Database

In the shell, switch to the admin database:

use admin
  • Create the Admin User

Create an admin user with the necessary privileges. Replace admin_user and password123 with your desired username and password:

db.createUser({
  user: "admin_user",
  pwd: "password123",
  roles: [{ role: "root", db: "admin" }]
})
  • Exit the MongoDB Shell

Type exit to leave the MongoDB shell.

exit

Test MongoDB Authentication

After setting up the admin user and enabling authentication, test the authentication by logging into MongoDB using the admin credentials.

  • Access MongoDB with Authentication

Use the following command to authenticate:

mongo -u admin_user -p password123 --authenticationDatabase admin
  • Check Connection
    After authenticating, you can perform operations as the admin user. You can also check if the user was created successfully by running:
db.auth("admin_user", "password123")

This should return 1 if authentication is successful.

Enable Replica Set

In a production environment, you may want to set up a MongoDB replica set for high availability and data redundancy. A replica set is a group of MongoDB instances that maintain the same data set.

  • Modify Configuration File for Replica Set

Edit the MongoDB configuration file to enable replica sets:

replicaSet: "rs0"
  • Restart MongoDB

After modifying the configuration file, restart MongoDB to apply the changes:

sudo systemctl restart mongod
  • Initialize the Replica Set
    Use the MongoDB shell to initialize the replica set: bashCopymongo Then, in the shell:
rs.initiate()

This will initiate the replica set and ensure the data is replicated across all nodes in the set.

Conclusion

In this article, we covered how to configure MongoDB on Linux, from installation to basic production configuration. We discussed the steps required to install MongoDB, enable authentication, create an admin user, and configure MongoDB for secure and efficient use in production environments.

By following these steps, you can ensure that your MongoDB installation is secure, optimized, and ready for handling high volumes of data. Whether you need a simple, standalone instance or a full replica set for redundancy, MongoDB provides the flexibility and scalability your applications need.

Leave A Comment