On this page本页内容
The tutorial is specific to MongoDB 5.0. For earlier versions of MongoDB, refer to the corresponding version of the MongoDB Manual.本教程特定于MongoDB 5.0。有关MongoDB的早期版本,请参阅相应版本的MongoDB手册。
Starting in MongoDB 3.2, config servers for sharded clusters can be deployed as a replica set. 从MongoDB 3.2开始,可以将分片集群的配置服务器部署为副本集。The replica set config servers must run the WiredTiger storage engine. 副本集配置服务器必须运行WiredTiger存储引擎。MongoDB 3.2 deprecates the use of three mirrored MongoDB 3.2不推荐将三个镜像mongod
instances for config servers.mongod
实例用于配置服务器。
This procedure moves the components of the sharded cluster to a new hardware system without downtime for reads and writes.此过程将分片集群的组件移动到新的硬件系统,而不会因读写而停机。
While the migration is in progress, do not attempt to change to the Sharded Cluster Metadata. 在迁移过程中,不要尝试更改为分片群集元数据。Do not use any operation that modifies the cluster metadata in any way. 不要使用任何以任何方式修改集群元数据的操作。For example, do not create or drop databases, create or drop collections, or use any sharding commands.例如,不要创建或删除数据库、创建或删除集合或使用任何分片命令。
Disable the balancer to stop chunk migration and do not perform any metadata write operations until the process finishes. 禁用平衡器以停止区块迁移,并且在进程完成之前不要执行任何元数据写入操作。If a migration is in progress, the balancer will complete the in-progress migration before stopping.如果正在进行迁移,平衡器将在停止之前完成正在进行的迁移。
To disable the balancer, connect to one of the cluster's 要禁用平衡器,请连接到集群的一个mongos
instances and issue the following method: mongos
实例并发出以下方法:[1]
sh.stopBalancer()
To check the balancer state, issue the 要检查平衡器状态,请发出sh.getBalancerState()
method.sh.getBalancerState()
方法。
For more information, see Disable the Balancer.有关更多信息,请参阅禁用平衡器。
[1] | sh.stopBalancer() also disables auto-splitting for the sharded cluster.sh.stopBalancer() 还禁用了分片集群的自动拆分。 |
Changed in version 3.4.在版本3.4中更改。
Starting in MongoDB 3.2, config servers for sharded clusters can be deployed as a replica set (CSRS) instead of three mirrored config servers (SCCC). 从MongoDB 3.2开始,可以将分片集群的配置服务器部署为一个副本集(CSRS),而不是三个镜像配置服务器(SCCC)。Using a replica set for the config servers improves consistency across the config servers, since MongoDB can take advantage of the standard replica set read and write protocols for the config data. 为配置服务器使用副本集可以提高配置服务器之间的一致性,因为MongoDB可以利用配置数据的标准副本集读写协议。In addition, using a replica set for config servers allows a sharded cluster to have more than 3 config servers since a replica set can have up to 50 members. 此外,为配置服务器使用副本集允许分片集群拥有3个以上的配置服务器,因为副本集最多可以有50个成员。To deploy config servers as a replica set, the config servers must run the WiredTiger storage engine.要将配置服务器部署为副本集,配置服务器必须运行WiredTiger存储引擎。
In version 3.4, MongoDB removes support for SCCC config servers.在3.4版中,MongoDB删除了对SCCC配置服务器的支持。
The following restrictions apply to a replica set configuration when used for config servers:以下限制适用于用于配置服务器的副本集配置:
members[n].buildIndexes
setting set to false).members[n].buildIndexes
设置为false
)。For each member of the config server replica set:对于配置服务器副本集的每个成员:
Start a 启动一个mongod
instance, specifying the --configsvr
, --replSet
, --bind_ip
options, and other options as appropriate to your deployment.mongod
实例,根据您的部署指定--configsvr
、--replSet
、--bind_ip
选项和其他选项。
Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured 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.至少,考虑启用身份验证和加强网络基础设施。
mongod --configsvr --replSet <replicaSetName> --bind_ip localhost,<hostname(s)|ip address(es)>
Connect 将mongosh
to the primary of the config server replica set and use rs.add()
to add the new member.mongosh
连接到配置服务器副本集的主副本集,并使用rs.add()
添加新成员。
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 如果您运行的MongoDB版本早于5.0,并且添加了一个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. votes
和priority
设置大于零的二级数据库,则可能会导致大多数投票成员在线,但无法选择一级数据库。To avoid such situations, consider adding the new secondary initially with 为避免出现这种情况,请考虑在开始时添加新的次要级别,priority :0
and votes :0
. priority :0
,votes :0
。Then, 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()
更新其优先级和投票。
rs.add( { host: "<hostnameNew>:<portNew>", priority: 0, votes: 0 } )
The initial sync process copies all the data from one member of the config server replica set to the new member without restarting.初始同步进程将所有数据从配置服务器副本集的一个成员复制到新成员,而无需重新启动。
mongos
instances automatically recognize the change in the config server replica set members without restarting.实例自动识别配置服务器副本集成员中的更改,而无需重新启动。
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()
Reconfigure the replica set to update the votes and priority of the new member:重新配置副本集以更新新成员的投票和优先级:
var cfg = rs.conf(); cfg.members[n].priority = 1; // Substitute the correct array index for the new member cfg.members[n].votes = 1; // Substitute the correct array index for the new member rs.reconfig(cfg)
where 其中n是n
is the array index of the new member in the members
array.members
数组中新成员的数组索引。
rs.reconfig()
shell method can force the current primary to step down, which causes an election. rs.reconfig()
shell方法可以强制当前主节点下移,从而导致选举。mongod
closes all client connections. mongod
将关闭所有客户端连接。Upon completion of initial sync of the replacement config server, from a 在完成替换配置服务器的初始同步后,从连接到主服务器的mongosh
session that is connected to the primary, use rs.remove()
to remove the old member.mongosh
会话中,使用rs.remove()
删除旧成员。
rs.remove("<hostnameOld>:<portOld>")
mongos
instances automatically recognize the change in the config server replica set members without restarting.实例自动识别配置服务器副本集成员中的更改,而无需重新启动。
mongos
Instancesmongos
实例Changed in version 3.2.在版本3.2中更改。
mongos
instances specify in the --configdb
or sharding.configDB
setting the config server replica set name and at least one of the replica set members. mongos
实例在--configdb
或sharding.configDB
设置中指定配置服务器副本集名称和至少一个副本集成员。mongos
instances for the sharded cluster must specify the same config server replica set name but can specify different members of the replica set.mongos
实例必须指定相同的配置服务器副本集名称,但可以指定副本集的不同成员。
If a 如果mongos
instance specifies a migrated replica set member in the --configdb
or sharding.configDB
setting, update the config server setting for the next time you restart the mongos
instance.mongos
实例在--configdb
或sharding.configDB
设置中指定了迁移的副本集成员,请在下次重新启动mongos
示例时更新配置服务器设置。
For more information, see Start a 有关更多信息,请参阅为分片群集启动mongos
for the Sharded Cluster.mongos
。
Migrate the shards one at a time. 一次迁移一个分片。For each shard, follow the appropriate procedure in this section.对于每个分片,请遵循本节中的相应过程。
To migrate a sharded cluster, migrate each member separately. 要迁移分片集群,请分别迁移每个成员。First migrate the non-primary members, and then migrate the primary last.首先迁移非主要成员,然后最后迁移主要成员。
If the replica set has two voting members, add an arbiter to the replica set to ensure the set keeps a majority of its votes available during the migration. 如果副本集有两个投票成员,请向副本集添加一个仲裁器,以确保该集在迁移过程中保持其大多数投票可用。You can remove the arbiter after completing the migration.您可以在完成迁移后删除仲裁程序。
mongod
process. mongod
进程。shutdown
command.shutdown
命令。dbPath
) to the new machine.dbPath
)移动到新机器。mongod
process at the new location.mongod
进程。If the hostname of the member has changed, use 如果成员的主机名已更改,请使用rs.reconfig()
to update the replica set configuration document with the new hostname.rs.reconfig()
以新主机名更新副本集配置文档。
For example, the following sequence of commands updates the hostname for the instance at position 例如,以下命令序列更新2
in the members
array:members
数组中位置2
处实例的主机名:
cfg = rs.conf() cfg.members[2].host = "pocatello.example.net:27018" rs.reconfig(cfg)
For more information on updating the configuration document, see Examples.有关更新配置文档的更多信息,请参阅示例。
rs.conf()
.rs.conf()
。rs.status()
.rs.status()
。While migrating the replica set's primary, the set must elect a new primary. 迁移副本集的主副本时,副本集必须选择一个新的主副本。This failover process which renders the replica set unavailable to perform reads or accept writes for the duration of the election, which typically completes quickly. 此故障切换过程使副本集在选择期间无法执行读取或接受写入操作,这通常会很快完成。If possible, plan the migration during a maintenance window.如果可能,请在维护窗口期间计划迁移。
Step down the primary to allow the normal failover process. 逐步关闭主服务器以允许正常故障转移过程。To step down the primary, connect to the primary and issue the either the 要单步执行主节点,请连接到主节点,然后发出replSetStepDown
command or the rs.stepDown()
method. replSetStepDown
命令或rs.stepDown()
方法。The following example shows the 以下示例显示rs.stepDown()
method:rs.stepDown()
方法:
rs.stepDown()
Once the primary has stepped down and another member has become 一旦主成员退出,另一个成员成为PRIMARY
state. PRIMARY
状态。To migrate the stepped-down primary, follow the Migrate a Member of a Replica Set Shard procedure要迁移逐步减少的主服务器,请执行迁移副本集分片的成员过程
You can check the output of 您可以检查rs.status()
to confirm the change in status.rs.status()
的输出以确认状态的更改。
To complete the migration, re-enable the balancer to resume chunk migrations.要完成迁移,请重新启用平衡器以恢复区块迁移。
Connect to one of the cluster's 连接到集群的一个mongos
instances and pass true
to the sh.startBalancer()
method: mongos
实例,并将true
传递给shstartBalancer()
方法:[2]
sh.startBalancer()
To check the balancer state, issue the 要检查平衡器状态,请发出sh.getBalancerState()
method.sh.getBalancerState()
方法。
For more information, see Enable the Balancer.有关更多信息,请参阅启用平衡器。
[2] | sh.startBalancer() also enables auto-splitting for the sharded cluster.sh.startBalancer() 还支持自动分割分片集群。 |