Setting Up MongoDB Replicas Locally
Table of contents
In a typical production environment, MongoDB replicas are deployed across multiple machines to ensure high availability and data redundancy. However, there are scenarios, especially in development and testing, where you might want to set up a replica on a single machine. This blog post will guide you through setting up a MongoDB replica set on a single Linux server with two hard drives, ensuring data is spread across both drives.
Why Set Up a Replica on a Single Machine?
Before diving into the setup, it's essential to understand the benefits and limitations of running a replica on a single machine:
Benefits:
Testing and Development: Ideal for development environments where you need to test replication features without additional hardware.
Backups: Provides a way to keep a secondary database copy on a different hard drive for redundancy.
Limitations:
No High Availability: If the machine goes down, all replicas will be unavailable.
Resource Intensive: Running multiple instances on a single machine can be resource-intensive and may affect performance.
Let's Setup a MongoDB replica locally
1. Install MongoDB
Follow the official MongoDB installation guide for your Linux distribution to install MongoDB.
2. Stop MongoDB Service
First, stop the running MongoDB service to make configuration changes:
sudo systemctl stop mongod
3. Configure MongoDB Instances
Create configuration files for the primary and secondary MongoDB instances. Start by copying the existing MongoDB configuration file:
sudo cp /etc/mongod.conf /etc/mongod_primary.conf
sudo cp /etc/mongod.conf /etc/mongod_secondary.conf
Edit /etc/mongod_primary.conf
:
# /etc/mongod_primary.conf
storage:
dbPath: /var/lib/mongodb_primary
systemLog:
path: /var/log/mongodb/mongod_primary.log
net:
port: 27017
replication:
replSetName: rs0
Edit /etc/mongod_secondary.conf
:
# /etc/mongod_secondary.conf
storage:
dbPath: /var/lib/mongodb_secondary
systemLog:
path: /var/log/mongodb/mongod_secondary.log
net:
port: 27018
replication:
replSetName: rs0
4. Create Data and Log Directories
Create directories for data and log files, and set appropriate permissions:
sudo mkdir /var/lib/mongodb_primary /var/lib/mongodb_secondary
sudo mkdir /var/log/mongodb
sudo chown -R mongodb:mongodb /var/lib/mongodb_primary /var/lib/mongodb_secondary /var/log/mongodb
5. Create Systemd Service Files
Copy and modify the MongoDB service file for the secondary instance:
sudo cp /lib/systemd/system/mongod.service /etc/systemd/system/mongod_secondary.service
Edit /etc/systemd/system/mongod_secondary.service
:
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network.target
[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongod --config /etc/mongod_secondary.conf
PIDFile=/var/run/mongodb/mongod_secondary.pid
LimitNOFILE=64000
[Install]
WantedBy=multi-user.target
Reload the system to recognize the new service:
sudo systemctl daemon-reload
6. Start MongoDB Instances
Start both MongoDB instances:
sudo systemctl start mongod
sudo systemctl start mongod_secondary
Enable them to start on boot:
sudo systemctl enable mongod
sudo systemctl enable mongod_secondary
7. Initialize the Replica
Connect to the primary MongoDB instance and initiate the replica:
mongosh --port 27017
In the MongoDB shell, run:
rs.initiate()
rs.add("127.0.0.1:27018")
8. Verify the Replica
Check the replica status to ensure everything is configured correctly:
rs.status()
Setting up a MongoDB replica on a single machine is useful for development and testing scenarios. It allows you to simulate a replica set environment without additional hardware, making it a cost-effective solution. However, it's important to note that this setup does not provide true high availability and should not be used in production environments.
Subscribe to my newsletter
Read articles from Nirmal Sankalana directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by