Docs HomeMongoDB Manual

Add a Member to a Shard将成员添加到分片

You can add members to an existing shard in a sharded cluster. 您可以将成员添加到分片集群中的现有分片。You might want to add a member to a shard for the same reasons you'd want to add a member to any replica set. 您可能希望向分片添加成员,原因与您希望向任何副本集添加成员的原因相同。For example, increasing the number of members provides additional candidates to replace a primary in the event of a failover. 例如,在发生故障切换时,增加成员数量可以提供其他候选成员来替换主成员。Additional members also increase data redundancy and replica set availability.额外的成员还增加了数据冗余和副本集的可用性。

For more information, refer to Replica Set Deployment Architectures.有关详细信息,请参阅复制副本集部署体系结构

About This Task任务介绍

Before MongoDB 5.0, a newly added secondary still counts as a voting member even though it can neither serve reads nor become primary until its data is consistent. 在MongoDB 5.0之前,新添加的次要成员仍然算作投票成员,即使在数据一致之前,它既不能提供读取服务,也不能成为主要成员。If you are running a MongoDB version earlier than 5.0 and add a secondary with its votes and priority settings greater than zero, this can lead to a case where a majority of the voting members are online but no primary can be elected. 如果您运行的MongoDB版本早于5.0,并且添加了一个votespriority设置大于零的次要成员,这可能会导致大多数投票成员都在线,但无法选出主要成员。To avoid such situations, consider adding the new secondary initially with priority :0 and votes :0. 为了避免出现这种情况,请考虑添加新的次级,初始priority :0votes :0Then, run rs.status() to ensure the member has transitioned into SECONDARY state. 然后,运行rs.status()以确保成员已转换到SECONDARY状态。Finally, use rs.reconfig() to update its priority and votes.最后,使用rs.reconfig()更新其优先级和投票。

Before You Begin开始之前

To add a member to a shard replica set, you need:要将成员添加到分片复制集,您需要:

  1. An active sharded cluster replica set.一个活动的分片集群复制集。
  2. A new host server for the new member. 新成员的新主机服务器。The new host server must be able to support your sharded data set and be accessible by the active replica set through the network.新的主机服务器必须能够支持您的分片数据集,并且可以由活动副本集通过网络访问。

Steps步骤

1

Prepare the Data Directory准备数据目录

Prepare the new member's data directory using one of the following strategies:使用以下策略之一准备新成员的数据目录

  • Have the new member automatically sync data from an existing member. 让新成员自动同步现有成员的数据。This process takes time but does not require administrator intervention.此过程需要时间,但不需要管理员干预。

    Make sure the new member's data directory does not contain data. 请确保新成员的数据目录包含数据。The new member will copy the data from an existing member.新成员将从现有成员复制数据。

    If the new member is in a recovering state, it must exit and become a secondary before MongoDB can copy all data as part of the replication process.如果新成员处于恢复状态,则必须退出并成为辅助成员,然后MongoDB才能作为复制过程的一部分复制所有数据。

  • Manually copy the data directory from an existing member. 从现有成员手动复制数据目录。The new member becomes a secondary member and will catch up to the current state of the replica set. 新成员将成为辅助成员,并将赶上副本集的当前状态。Copying the data over may shorten the amount of time it takes for the new member to sync with the other replica set members.复制数据可能会缩短新成员与其他副本集成员同步所需的时间。

    Ensure that you can copy the data directory to the new member and begin replication within the window allowed by the oplog. 确保您可以将数据目录复制到新成员,并在oplog允许的窗口内开始复制。Otherwise, the new instance will have to perform an initial sync, which completely resynchronizes the data, as described in Resync a Member of a Replica Set.否则,新实例将不得不执行初始同步,这将完全重新同步数据,如重新同步副本集的成员中所述。

    To check the current state of replica set members with regards to the oplog, use rs.printReplicationInfo().要检查复制集成员相对于操作日志的当前状态,请使用rs.printReplicationInfo()

For background on replication deployment patterns, see the Replica Set Deployment Architectures document.有关复制部署模式的背景信息,请参阅副本集部署体系结构文档。

2

Start the new mongod instance启动新的mongod实例

Specify the data directory and the replica set name. 指定数据目录和复制副本集名称。The following example specifies the /srv/mongodb/db0 data directory and the rs0 replica set:以下示例指定/srv/mongodb/db0数据目录和rs0副本集:

mongod --dbpath /srv/mongodb/db0 --replSet rs0  --bind_ip localhost,<ip address of the mongod host>
Warning

Before you bind your instance to a publicly-accessible IP address, you must secure your cluster from unauthorized access. 在将实例绑定到可公开访问的IP地址之前,必须保护群集不受未经授权的访问。For a complete list of security recommendations, see Security Checklist. 有关安全建议的完整列表,请参阅安全检查表At minimum, consider enabling authentication and hardening network infrastructure.至少,考虑启用身份验证强化网络基础设施

For more information on configuration options, see the mongod manual page.有关配置选项的更多信息,请参阅mongod手册页面。

Tip

You can specify the data directory, replica set name, and the ip binding in the mongod.conf configuration file, and start the mongod with the following command:您可以在mongod.conf配置文件中指定数据目录、副本集名称和ip绑定,并使用以下命令启动mongod

mongod --config /etc/mongod.conf
3

Connect to the replica set's primary连接到复制副本集的主

You can only add members while connected to the primary. 您只能在连接到主节点时添加成员。To connect to the primary, use mongosh. Replace the host and port values with relevant values for your deployment:要连接到主,请使用mongosh。将hostport值替换为部署的相关值:

mongosh --host mongodb0.example.com --port 28015

If you do not know which member is the primary, connect to any member of the replica set and issue the db.hello() command.如果您不知道哪个成员是主成员,请连接到副本集的任何成员,然后发出db.hello()命令。

4

Use rs.add() to add the new member to the replica set使用rs.add()将新成员添加到副本集中

Pass the member configuration document to the method. 成员配置文档传递给方法。For example, to add a member at host mongodb3.example.net, issue the following command:例如,要在主机mongodb3.example.net上添加成员,请发出以下命令:

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

Ensure that the new member has reached SECONDARY state确保新成员已达到SECONDARY状态

To check the state of the replica set members, run rs.status():要检查副本集成员的状态,请运行rs.status()

rs.status()

Next Steps下一步

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.一旦新添加的成员转换到SECONDARY状态,请使用rs.reconfig()更新新添加成员的 优先级,并在需要时进行投票

Warning
  • The rs.reconfig() shell method can force the current primary to step down, which causes an election. rs.reconfig()shell方法可以强制当前的primary退出,从而导致选举When the primary steps down, the mongod closes all client connections. 当primary关闭时,mongod会关闭所有客户端连接。While this typically takes 10-20 seconds, try to make these changes during scheduled maintenance periods.虽然这通常需要10-20秒,但请尝试在计划维护期间进行这些更改。
  • Avoid reconfiguring replica sets that contain members of different MongoDB versions as validation rules may differ across MongoDB versions.避免重新配置包含不同MongoDB版本成员的副本集,因为验证规则可能因MongoDB版本而异。

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:如果rs.conf()返回mongodb3.example.net:27017的配置文档作为members数组中的第五个元素,则要更新其优先级并将其投票数设置为1,请使用以下操作序列:

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