- 分片 >
- Sharded Cluster Administration >
- 迁移集群到新的硬件系统中
迁移集群到新的硬件系统中¶
On this page
The tutorial is specific to MongoDB 3.4. For earlier versions of MongoDB, refer to the corresponding version of the MongoDB Manual.
在 3.2 版更改.
Starting in MongoDB 3.2, config servers for sharded clusters can be deployed as a replica set. The replica set config servers must run the WiredTiger storage engine. MongoDB 3.2 deprecates the use of three mirrored mongod instances for config servers.
这篇教程在不中断集群读写的情况下,将一个完整的 sharded cluster 迁移到新的硬件系统中.
重要
在迁移进行时,不要试图改变 集群的元信息. 不要使用可能导致集群元信息改变的任何操作,比如创建或者删除数据库,集合,或者使用分片命令.
如果你的集群包含了 standalone 的 mongod ,使用 将单机mongod转化为复制集 将其转化为复制集,只有这样,以后的迁移过程才能变得简单,并且可以避免宕机.集群中存在单机节点,在迁移时会有宕机时间.
禁用均衡¶
禁用均衡以停止 数据块迁移,并且在流程结束之前不要更改集群的元信息.如果有迁移正在运行中,均衡器会在这次迁移结束后再停止.
要禁用均衡,连接到一个 mongos 上,并执行以下方法:
sh.stopBalancer()
使用 sh.getBalancerState() 检查均衡状态.
参见 Disable the Balancer 获得更多信息.
单独迁移每台配置服务器¶
在 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). 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. 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. To deploy config servers as a replica set, the config servers must run the WiredTiger storage engine.
In version 3.4, MongoDB removes support for SCCC config servers. To upgrade your config servers from SCCC to CSRS, see Upgrade Config Servers to Replica Set.
The following restrictions apply to a replica set configuration when used for config servers:
- Must have zero arbiters.
- Must have no delayed members.
- Must build indexes (i.e. no member should have buildIndexes setting set to false).
For each member of the config server replica set:
重要
Replace the secondary members before replacing the primary.
Add the new config server to the replica set.¶
Connect a mongo shell to the primary of the config server replica set and use rs.add() to add the new member.
rs.add("<hostnameNew>:<portNew>")
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.
Shut down the member to replace.¶
If replacing the primary member, step down the primary first before shutting down.
Remove the member to replace from the config server replica set.¶
Upon completion of initial sync of the replacement config server, from a mongo shell connected to the primary, use rs.remove() to remove the old member.
rs.remove("<hostnameOld>:<portOld>")
mongos instances automatically recognize the change in the config server replica set members without restarting.
重启 mongos¶
在 3.2 版更改: With replica set config servers, the 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. The mongos instances for the sharded cluster must specify the same config server replica set name but can specify different members of the replica set.
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.
参见 假设一个分片使用了复制集,复制集名字为 rs1 ,有一个运行在 mongodb0.example.net 且端口为 27017 的成员,使用以下命令添加这个分片: 获得更多信息.
迁移分片¶
一次迁移一个数据分片,对于每个分片,按照以下章节进行迁移.
迁移一个复制集的分片¶
要迁移一个集群,需要单独迁移每个成员,首先迁移非主成员,最后迁移 primary .
如果复制集有两个投票成员,添加一个 仲裁节点 以保证在迁移过程中能够保证有大多数节点在线.在迁移结束后可以移除这个仲裁节点.
迁移复制集的一个成员¶
将数据目录(即 dbPath )转移到新机器上.
在新机器上启动 mongod .
连接到复制集当前的主节点上.
如果新节点的域名发生变化,使用 rs.reconfig() 更新 复制集配置文档 .
举例,下面的命令过程将成员中位于第 2 位的域名进行更新:
cfg = rs.conf() cfg.members[2].host = "pocatello.example.net:27017" rs.reconfig(cfg)
参见 Examples 获得更多更新配置文档的信息.
使用 rs.conf() 确认使用了新的配置.
等待所有成员恢复正常,使用 rs.status() 检测成员状态.
迁移复制集中的主节点¶
在迁移主节点的时候,需要复制集选举出一个新的主节点,在进行选举的时候,复制集将读写,通常,这只会持续很短的时间,不过,应该尽可能在影响较小的时间段内迁移主节点.
将主节点降级,以使得正常的 failover 开始.要将主节点降级,连接到一个主节点,使用 replSetStepDown 方法或者使用 rs.stepDown() 方法,下面的例子使用了 rs.stepDown() 方法进行降级:
rs.stepDown()
等主节点降级为从节点,另一个成员成为 PRIMARY 之后,可以按照 迁移复制集的一个成员 迁移这个降级了的节点.
可以使用 rs.status() 来确认状态的改变.
迁移一个单机分片¶
迁移一个单节点分片合理的方法是首先按照 将单节点mongod转化为复制集 ,再按照 迁移复制集分片 进行迁移.在生产环境中,所有的分片都应该是复制集.
Migrating a shard as standalone is a multi-step process during which part of the shard may be unavailable. If the shard is the primary shard for a database,the process includes the movePrimary command. While the movePrimary runs, you should stop modifying data in that database. To migrate the standalone shard, use the 从集群中删除分片 procedure.
重新开启均衡¶
重新打开均衡器,恢复 数据块迁移 以完成迁移.
连接到一个 mongos 上,使用 sh.setBalancerState() 方法并传递 true 参数.
sh.setBalancerState(true)
使用 sh.getBalancerState() 检查均衡状态.
For more information, see 开启均衡过程.