简体   繁体   中英

mongoDB replica set

I am trying to understand the concept of replica sets in MongoDB.

Taking a simple example of 2 mongoDB instances A (primary) and B (secondary).

If my client is happily querying AI understand that writes get replicated to B but what happens if server A becomes inaccessible?

Whilst in terms of mongo replication I can see that B gets elected as the new primary, how does the client know to now channel its queries to B and not A? Is this all done internally to mongo?

I ask because my client's configuration has a uri pointing to A yet nothing for B. Is that correct?

Taking a simple example of 2 mongoDB instances A (primary) and B (secondary).

You should not use two instance as part of your replica set. You should either have an arbiter or another secondary so that elections can take place.

Please see here for gotchas, here for replication fundamentals and here for information on elections.

If my client is happily querying AI understand that writes get replicated to B but what happens if server A becomes inaccessible?

The service should failover with server B becoming primary. However, with two instances, server B will not be able to elect itself as primary because there is no majority.

Whilst in terms of mongo replication I can see that B gets elected as the new primary, how does the client know to now channel its queries to B and not A? Is this all done internally to mongo?

This is typically performed through the driver as the driver is aware of the replica set configuration. Please see here for the relevant driver documentation (depending on what driver you are using).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM