Introduction
Automatic failover helps keep your database working even if one server stops. It switches to a backup server without you needing to do anything.
Automatic failover is not a command but a feature of MongoDB replica sets. You set up a replica set with multiple members. If the primary member fails, the replica set automatically elects a new primary.
rs.initiate()
// Starts a replica set with the current server as primaryrs.add("mongodb1.example.net:27017") // Adds a new member to the replica set
rs.status()
// Shows the current status of the replica setrs.initiate({
_id: "rs0",
members: [
{ _id: 0, host: "localhost:27017" },
{ _id: 1, host: "localhost:27018" },
{ _id: 2, host: "localhost:27019" }
]
})
// After this, if the primary at localhost:27017 fails,
// MongoDB will automatically elect one of the other members as primary.rs.initiate().db.startReplica(), replicaSet.start(), and mongo.startReplicaSet() do not exist in MongoDB shell.Pass the test and this pattern moves unknown