0
0
MongoDBquery~5 mins

Primary and secondary nodes in MongoDB

Choose your learning style9 modes available
Introduction

Primary and secondary nodes help keep your data safe and available. The primary node handles writes, and secondary nodes copy data to protect against failures.

When you want your database to keep working even if one server stops.
When you want to read data from multiple servers to reduce load on the main server.
When you want to back up your data automatically without stopping the database.
When you want to improve read speed by spreading read requests across servers.
When you want to make sure your data is consistent and safe in case of hardware problems.
Syntax
MongoDB
rs.initiate({
  _id: "replicaSetName",
  members: [
    { _id: 0, host: "primaryHost:port", priority: 2 },
    { _id: 1, host: "secondaryHost1:port", priority: 1 },
    { _id: 2, host: "secondaryHost2:port", priority: 1 }
  ]
})

The rs.initiate() command starts a replica set with one primary and multiple secondary nodes.

Priority controls which node becomes primary; higher priority means higher chance.

Examples
Starts a replica set with one primary (localhost:27017) and one secondary (localhost:27018).
MongoDB
rs.initiate({
  _id: "myReplicaSet",
  members: [
    { _id: 0, host: "localhost:27017", priority: 2 },
    { _id: 1, host: "localhost:27018", priority: 1 }
  ]
})
Edge case: No members means no primary or secondary nodes. This setup is invalid.
MongoDB
rs.initiate({
  _id: "emptySet",
  members: []
})
Only one node acts as primary because there are no secondaries.
MongoDB
rs.initiate({
  _id: "singleNodeSet",
  members: [
    { _id: 0, host: "localhost:27017", priority: 1 }
  ]
})
Replica set with one primary and two secondary nodes for better data safety and read scaling.
MongoDB
rs.initiate({
  _id: "multiSecondarySet",
  members: [
    { _id: 0, host: "primaryHost:27017", priority: 2 },
    { _id: 1, host: "secondary1:27018", priority: 1 },
    { _id: 2, host: "secondary2:27019", priority: 1 }
  ]
})
Sample Program

This command starts a replica set named exampleSet with one primary node on port 27017 and two secondary nodes on ports 27018 and 27019. Then rs.status() shows which node is primary and which are secondary.

MongoDB
/* Connect to MongoDB shell and run this to create a replica set */
rs.initiate({
  _id: "exampleSet",
  members: [
    { _id: 0, host: "localhost:27017", priority: 2 },
    { _id: 1, host: "localhost:27018", priority: 1 },
    { _id: 2, host: "localhost:27019", priority: 1 }
  ]
})

/* Check the status to see primary and secondary nodes */
rs.status()
OutputSuccess
Important Notes

Primary node handles all writes and replicates data to secondary nodes.

Secondary nodes replicate data from primary and can serve read requests if configured.

Failover happens automatically: if primary fails, a secondary becomes primary.

Summary

Primary node is the main server that accepts writes.

Secondary nodes copy data from primary to keep backups and help with reads.

Replica sets improve data safety and availability by having multiple nodes.