Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Deploying MongoDB On-Premises

Introduction

Deploying MongoDB on-premises provides full control over your database environment. This guide will walk you through the steps to install and configure MongoDB on your own servers.

Prerequisites

  • A server or machine with a supported operating system (e.g., Linux, Windows, macOS).
  • Administrator or root access to install software.
  • Internet connection to download MongoDB packages.

Installation Steps

Step 1: Download and Install MongoDB

Download the appropriate MongoDB package for your operating system from the MongoDB Download Center. Follow the installation instructions provided for your OS.


// For Linux (Ubuntu example)
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
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
sudo apt-get update
sudo apt-get install -y mongodb-org

// Start MongoDB
sudo systemctl start mongod
sudo systemctl enable mongod

    

Step 2: Verify Installation

After installation, verify that MongoDB is running:


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

    

This command should return the status of the MongoDB server, indicating that it is running.

Basic Configuration

Configuration File

The MongoDB configuration file is typically located at /etc/mongod.conf. You can edit this file to adjust settings such as the data directory, log directory, and network interfaces.


# Example configuration
storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log
net:
  bindIp: 127.0.0.1
  port: 27017

    

Security Configuration

Enabling Authentication

Enable authentication to secure your MongoDB instance:


# /etc/mongod.conf
security:
  authorization: "enabled"

    

Restart MongoDB to apply the changes:


sudo systemctl restart mongod

    

Creating an Admin User

Create an admin user to manage your MongoDB instance:


use admin
db.createUser({
  user: "admin",
  pwd: "password",
  roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
})

    

Replace password with a strong password of your choice.

Conclusion

Deploying MongoDB on-premises gives you complete control over your database environment. By following these steps, you can install and configure MongoDB on your own servers, ensuring that your data is securely managed and accessible.