Deployment Overview
Version Requirements
- MongoDB: 7.x / 8.0
- Ports: 37017, 37018, 37019
Configuration
Primary Node Configuration
dbpath=/data/mongo/data/server1
bind_ip=0.0.0.0
port=37017
fork=true
logpath=/data/mongo/logs/server1.log
replSet=WzkCluster
Initialize Replica Set
var cfg = {
"_id": "WzkCluster",
"protocolVersion": 1,
"members": [
{"_id": 1, "host": "10.10.52.38:37017", "priority": 10},
{"_id": 2, "host": "10.10.52.38:37018"}
]
}
rs.initiate(cfg)
Add/Remove Nodes
// Add node
rs.add("ip:37019")
// Remove node
rs.remove("ip:37019")
Node Roles
- PRIMARY: Can query and insert data
- SECONDARY: Can only query, cannot insert, can be elected as primary
- ARBITER: Cannot query or insert data, cannot become primary node
Common Errors
| Problem | Root Cause | Solution |
|---|
| Node not joined | replSetName mismatch | Unify replSetName |
| Election issue | Less than majority | Add third node |
| Secondary read failure | Read preference not set | Add readPreference |
Rollback Operations
// Demote primary node
rs.stepDown(60)
// Modify member priority
cfg = rs.conf()
cfg.members[2].priority = 0
rs.reconfig(cfg)
Key Points
- By default, secondary nodes cannot read data, need to use
rs.slaveOk()
- When primary node fails, secondary nodes automatically elect a new primary