mongodb副本集添加新的节点

 

monodb副本集集群,能支撑多个secondary节点,一般刚开始可能部署少量一些节点,减轻成本费用,随着业务增加,会需要增加更多的节点,需要手动在上面添加新节点,那如何添加新节点呢?官方上有详细的文档说明,见:https://docs.mongodb.com/manual/tutorial/expand-replica-set/,这是域名在国外,有时候会打开比较慢,需要多点耐心去打开的。

 

 

Maximum Voting Members

A replica set can have a maximum of seven voting members. To add a member to a replica set that already has seven voting members, you must either add the member as a non-voting member or remove a vote from an existing member.

一个副本集,最多可以拥有50个secondary,最多可以有7个投票成员,在副本集里面添加一个新成员,如果之前副本集已经有了7个成员,那么可以设置成非投票成员,或者你从移除之前一个投票成员出来。

 

Requirements

  1. An active replica set.
  2. A new MongoDB system capable of supporting your data set, accessible by the active replica set through the network.

添加之前,需要保障的是:

1、一个正在使用的副本集,一个新的MongoDB成员,网络互通。

2、保证新成员和老的成员有同样的数据目录、配置文件目录、日志目录等

 

Add a Member to an Existing Replica Set

操作步骤有大概以下:

1、启动一个新的mongod进程服务

Start the new mongod instance. Specify the data directory and the replica set name. The following example specifies the /srv/mongodb/db0 data directory and the rs0 replica set:

mongod --dbpath /srv/mongodb/db0 --replSet rs0 --bind_ip localhost,<hostname(s)|ip address(es)>

2、确认副本集的master,Connect to the replica set's primary.

You can only add members while connected to the primary. If you do not know which member is the primary, log into any member of the replica set and issue the db.isMaster() command.

3、开始添加成员

Use rs.add() to add the new member to the replica set. Pass the member configuration document to the method. For example, to add a member at host mongodb3.example.net, issue the following command:

rs.add( { host: "mongodb3.example.net:27017", priority: 0, votes: 0 } )

这里votes:0,就是没有投票特性,这一步耗时比较长,因为是从主库拉全量数据进行数据同步,所以需要注意对线上业务的影响

When a newly added secondary has its votes and priority settings greater than zero, during its initial sync, the secondary still counts as a voting member even though it cannot serve reads nor become primary because its data is not yet consistent.

This can lead to a case where a majority of the voting members are online but no primary can be elected. To avoid such situations, consider adding the new secondary initially with priority :0 and votes :0. Then, once the member has transitioned into SECONDARY state, use rs.reconfig() to update its priority and votes.

4、查看新成员状态

Ensure that the new member has reached SECONDARY state. To check the state of the replica set members, run rs.status():

rs.status()

5、重置priority和votes属性

Once the newly added member has transitioned into SECONDARY state, use rs.reconfig() to update the newly added member's priority and votes if needed.

For example, if rs.conf() returns the configuration document for mongodb3.example.net:27017 as the fifth element in the members array, to update its priority and votes to 1, use the following sequence of operations:

var cfg = rs.conf();
cfg.members[4].priority = 1
cfg.members[4].votes = 1
rs.reconfig(cfg)

 

上一篇:spark 之 UDF的两种方式


下一篇:Week1.Interview