Learn MongoDB fundamentals including installation, replica set architecture, Primary, Secondary, Arbiter roles, elections, and replication management on Linux.
What Is MongoDB?
MongoDB is a document-oriented NoSQL database designed for scalability, flexibility, and high availability. It stores data in BSON (JSON-like) documents and is widely used in modern web, microservices, and cloud-native applications.
MongoDB achieves high availability using replica sets, which automatically handle failover and data replication.
MongoDB Installation
Ubuntu
sudo apt update
sudo apt install -y mongodb-org
sudo systemctl enable mongod
sudo systemctl start mongod
RHEL / Rocky / Alma
sudo dnf install -y mongodb-org
sudo systemctl enable mongod
sudo systemctl start mongod
Verify Installation
mongod --version
Basic MongoDB Configuration
Edit the configuration file:
/etc/mongod.conf
Enable replication and allow network access:
replication:
replSetName: rs0
net:
bindIp: 0.0.0.0
Restart MongoDB:
systemctl restart mongod
Perform this configuration on all replica set nodes.
What Is a MongoDB Replica Set?
A replica set is a group of MongoDB instances that maintain the same data set. It provides:
- High availability
- Automatic failover
- Data redundancy
A typical replica set consists of:
- 1 Primary
- 1 or more Secondaries
- Optional Arbiter
MongoDB Node Roles Explained
Primary
- Accepts all write operations
- Can serve read operations
- Only one Primary exists at a time
Secondary
- Replicates data from the Primary using the oplog
- Can serve read-only queries (if enabled)
- Eligible to become Primary during failover
Arbiter
- Does not store data
- Does not serve reads or writes
- Participates only in elections
Arbiters are used to maintain an odd number of voting members, preventing split-brain situations.
MongoDB Replication Setup (Replica Set)
Step 1: Initialize the Replica Set (Primary Node)
mongosh
rs.initiate()
Step 2: Add Secondary Nodes
rs.add("mongo2:27017")
rs.add("mongo3:27017")
These nodes automatically sync data from the Primary.
Step 3: Add an Arbiter (Optional)
rs.addArb("mongo-arbiter:27017")
Use an arbiter when:
- You need quorum but want to save storage
- You want fast elections
- You have limited infrastructure
How MongoDB Elections Work
MongoDB uses a majority-based election mechanism:
- Nodes send heartbeat signals
- If the Primary becomes unreachable, an election is triggered
- A node with majority votes becomes the new Primary
Voting Rules
| Member Type | Votes |
|---|---|
| Primary | 1 |
| Secondary | 1 |
| Arbiter | 1 |
A majority is required to elect a Primary.
Checking Replica Set Status and Roles
Replica Set Health
rs.status()
View Configuration
rs.conf()
Check Primary
rs.isMaster()
(ismaster: true indicates Primary)
Check Replication Lag
rs.printSecondaryReplicationInfo()
Testing Failover (Optional)
Force the Primary to step down:
rs.stepDown(60)
One of the Secondaries will be elected automatically.
Common Replica Set Topologies
| Topology | Use Case |
|---|---|
| 1 Primary + 2 Secondaries | Recommended for production |
| 1 Primary + 1 Secondary + Arbiter | Small setups |
| 1 Primary + 3+ Secondaries | Read-heavy workloads |
Security Best Practices
- Enable authentication and authorization
- Use keyfiles for internal replica authentication
- Restrict network access
- Enable TLS for replication traffic
Operational Best Practices
- Use an odd number of voting members
- Monitor replication lag
- Avoid arbiters in large critical systems
- Take backups from secondary nodes
- Monitor elections and failovers
Conclusion
MongoDB replica sets provide built-in high availability through Primary, Secondary, and Arbiter roles. Understanding how replication and elections work is essential for running MongoDB reliably in production environments.



