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)

 

### 如何向 MongoDB 副本添加副本节点 要将副本节点成功添加到现有的 MongoDB 副本中,可以按照以下方法操作: #### 方法一:使用 `rs.add` 添加节点 可以通过主节点执行 `rs.add()` 方法来动态添加一个新的副本节点。以下是具体的操作流程和注意事项。 1. **准备节点** 配置并启动新的 MongoDB 实例作为目标副本节点。确保该实例能够正常运行,并监听正确的端口以及绑定 IP 地址[^2]。 2. **连接至主节点** 使用 MongoDB Shell 或其他客户端工具连接到当前副本中的主节点(Primary Node)。只有主节点才能接受配置更改请求。 3. **执行 `rs.add` 命令** 在主节点上输入如下命令以添加成员: ```javascript rs.add("new_node_hostname:port") ``` 替换 `"new_node_hostname"` 和 `port` 为实际的节点主机名/IP地址及其对应的端口号。例如: ```javascript rs.add("nosql03.example.com:27017") ``` 4. **验证新增加的节点状态** 执行以下命令查看副本的状态,确认节点已被正确加入: ```javascript rs.status() ``` 输出结果应显示所有现有节点的信息,包括刚刚添加的那个节点[^5]。 #### 方法二:重定义整个配置对象 (适用于复杂场景) 如果需要调整更多高级选项或者批量修改多个参数,则可以选择重写整个 Replica Set 的配置结构并通过 `rs.reconfig` 应用这些变化。 1. 获取当前配置副本: ```javascript cfg = rs.conf(); ``` 2. 修改配置文件内容,比如增加额外字段 `_id`, `priority`, etc., 来满足特定需求。 3. 将更后的配置应用回去: ```javascript rs.reconfig(cfg); ``` 以上两种方式都可以实现扩展副本合体的目的;对于简单情况推荐直接调用 `rs.add()` 函数即可完成任务[^3]。 --- ### 示例代码展示 假设我们有一个名为 `myReplSet` 的三节点副本,现在想要再追加一台机器成为第四号成员: ```bash # 启动新的mongod进程, 并指定所属副本名称(--replSet myReplSet) 及存储路径(--dbpath). mongod --port 27018 --dbpath /data/db/new_member --logpath /var/log/mongodb.log --fork --replSet myReplSet --bind_ip_all ``` 接着登录任意一个现存的primary server shell session里头下达指令: ```javascript // Add new secondary member. rs.add('localhost:27018'); ``` 最后再次查询整体状况: ```javascript printjson(rs.status()); ``` 这样就完成了全部过程! ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值