9

我必须设置一个可以处理故障转移的数据库(如果一个崩溃另一个接管)。为此,我决定使用 mongodb:我设置了一个包含两个实例的副本集。每个实例都在单独的 VM 上运行。我有几个问题:

  • 建议在副本集中至少使用 3 个实例。只用两个可以吗?

  • 我有两个实例,然后是两个 IP 地址。我应该为需要在数据库中读/写的应用程序提供哪个 IP?当数据库关闭时,请求将如何重定向到仍在运行的实例?

一些帮助开始会很棒!

4

1 回答 1

12

It is recommended to use at least 3 instances in a replica set. Is it ok to use only two ?

No, the minimum requirement for a replica set is three processes (docs), but the third could be an arbiter even though it is not recommended.

I have two instances, and then two IP addresses. Which IP should I give to my application that will need to read/write in the database ? When a database is down, how the request will be redirected to the instance that is still up ?

There are two alternatives:

#1 (recommended)

You provide the driver with all addresses (for more detailed information how, visit the docs), example with nodejs driver (it is similar with the other). This way, the driver will know all, or at least more then one of, the instances directly, which will prevent problems if all of the specified instances are down (see #2).

var MongoClient = require('mongodb').MongoClient;

MongoClient.connect('mongodb://[server1],[server2],[...]/[database]?replicaSet=[name]', function(err, db) {
});

#2

You provide the driver with one of them (probably the primary) and mongodb will figure out the rest of them. However, if your app starts up when the specified instance(s) is down, the driver would not be able to find the other instances, and therefore cannot connect to mongodb.

于 2013-02-10T16:07:00.163 回答