Hosting + Ai Website Builder + Free Domain (3 Month Free Credit)
Shop Today

How to Use MongoDB on Linux for Peak Performance [Ultimate Guide]

Use MongoDB on a Linux server to manage your data with a flexible, scalable, and high-performance NoSQL database. MongoDB stores data in JSON-like documents with dynamic schemas, making it ideal for applications requiring rapid development, real-time analytics, and large-scale data handling.

Use MongoDB on Linux

This guide covers how to install and use MongoDB on Linux, starting and enabling its service, securing your setup, and performing basic operations to get you started smoothly.

Prerequisites

  • A Linux server running a supported distribution like Ubuntu, Debian, CentOS, Red Hat, or similar
  • Root or sudo privileges to install software and manage services
  • Terminal or SSH access to your Linux server
  • Basic familiarity with the Linux command line

Use MongoDB on a Linux Server

MongoDB is a popular NoSQL database known for its flexibility, scalability, and ease of use. Running MongoDB on a Linux server provides a stable, high-performance environment for managing unstructured or semi-structured data, making it ideal for modern applications that require speed and adaptability.

Install MongoDB on the Linux Server

To run MongoDB efficiently on a Linux server, you need to install the official MongoDB packages. This ensures you get the latest stable version, along with tools for managing and securing your database.

  • On Ubuntu/Debian

Import the MongoDB public GPG key:

curl -fsSL https://pgp.mongodb.com/server-8.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg --dearmor

Create a source list file for MongoDB:

echo "deb [ signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list

Update the package database and install MongoDB:

sudo apt update
sudo apt install -y mongodb-org
  • On CentOS/Red Hat

Create the MongoDB repository file:

cat <<EOF | sudo tee /etc/yum.repos.d/mongodb-org-8.0.repo
[mongodb-org-8.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/\$releasever/mongodb-org/8.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://pgp.mongodb.com/server-8.0.asc
EOF

Install MongoDB packages:

sudo yum install -y mongodb-org

OR on newer systems with dnf:

sudo dnf install -y mongodb-org

Start and Enable the MongoDB Service

After installation, start the MongoDB daemon and enable it to launch on system boot.

  • Start MongoDB:
sudo systemctl start mongod
  • Enable MongoDB to start automatically on boot:
sudo systemctl enable mongod
  • Check the MongoDB service status:
sudo systemctl status mongod

Active status confirms MongoDB is running properly.

Verify MongoDB Installation

Verify that MongoDB is working by connecting to its shell client:

mongosh

Once connected, type:

db.version()

You should see the installed MongoDB version. Exit the shell with:

exit

Secure Your MongoDB Installation

By default, MongoDB allows connections without authentication from localhost, which is suitable for development only. For production:

  • Enable authentication by editing /etc/mongod.conf:
security:
authorization: "enabled"
  • Restart MongoDB:
sudo systemctl restart mongod
  • Connect to MongoDB, create an admin user with privileges:
mongosh
use admin
db.createUser({
user: "admin",
pwd: "strong_password",
roles: [{ role: "root", db: "admin" }]
})

Use these credentials to connect going forward.

Basic MongoDB Commands

  • Show databases:
show dbs
  • Create or switch to a database:
use mydatabase
  • Create a collection and insert a document:
db.users.insertOne({ name: "John", age: 30 })
  • Query documents:
db.users.find({ age: { $gte: 18 } })
  • Delete documents:
db.users.deleteOne({ name: "John" })

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

Manage MongoDB Logs and Data Locations

Understanding where MongoDB stores its logs and data is essential for monitoring performance, troubleshooting issues, and ensuring smooth operation. On most Linux distributions:

  • MongoDB logs are usually at:
    • /var/log/mongodb/mongod.log
  • MongoDB data files are stored under:
    • /var/lib/mongo

Check these paths for monitoring and troubleshooting.

Conclusion

To use MongoDB on a Linux server, install the MongoDB packages from the official repositories, start and enable the mongod service, and optionally configure authentication for production security. MongoDB offers a rich, flexible platform for document-oriented data storage suited for diverse applications. Use the Mongo shell (mongosh) to interact with your databases, create users, and perform CRUD operations easily. For comprehensive instructions, detailed configuration options, and advanced features, refer to the official MongoDB documentation.

Himanshu Joshi

Leave a Comment

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

Scroll to Top