Convert a Shard Standalone to a Shard Replica Set将独立分片转换为分片副本集

Starting in MongoDB 3.6, all shards must be replica sets. 从MongoDB 3.6开始,所有分片都必须是副本集。Before you can upgrade your 3.4 sharded cluster to version 3.6, you must convert any shards that are running as standalone mongod instances to replica set shards.在将3.4分片集群升级到3.6版本之前,必须将作为独立mongod实例运行的任何分片转换为副本集分片。

This tutorial describes the process for converting a shard standalone into a shard replica set. 本教程描述了将独立分片转换为分片副本集的过程。The procedure is specific to a shard standalone. 该过程特定于单独的分片。To convert just a standalone to a replica set (i.e. not a part of any sharded cluster), see Convert a Standalone to a Replica Set instead.要仅将单机版转换为副本集(即不属于任何分片群集),请参阅将单机版转化为副本集

Procedure过程

Important重要

The following procedure converts a standalone shard to a single-member replica set shard. 以下过程将独立分片转换为单成员副本集分片。The procedure assumes that the single member runs on the same host and port as before.该过程假定单个成员与以前一样在同一主机和端口上运行。

  1. Shut down the shard standalone mongod instance.关闭分片独立mongod实例。
  2. Restart the shard instance with the --replSet option to specify the name of the new replica set. 使用--replSet选项重新启动分片实例,以指定新副本集的名称。Ensure that the name is distinct (for instance, you could use the shard name as the replica set name); in particular, shard replica sets must not use the same name as the config server replica set.确保名称是不同的(例如,可以使用分片名称作为副本集名称);特别是,分片副本集不能使用与config服务器副本集相同的名称。

    The other options can remain the same.其他选项可以保持不变。

    For example, the following command starts a standalone instance as a member of a new replica set named shardA. 例如,以下命令以名为shardA的新副本集的成员身份启动独立实例。The other options stay the same as before; e.g. --dbpath uses the standalone's existing database path of /srv/mongodb/db0 and --port is the same as before:其他选项保持不变;例如,--dbpath使用独立数据库的现有数据库路径/srv/mongodb/db0--port与之前相同:

    mongod --port 27018 --dbpath /srv/mongodb/db0 --shardsvr --replSet shardA --bind_ip localhost,<ip address of the mongod host>

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

  3. Connect mongosh to the shard mongod instance.mongosh连接mongosh到分片mongod实例。
  4. Use rs.initiate() to initiate the new replica set:使用rs.initiate()启动新副本集:

    rs.initiate()

    The replica set is now operational. 副本集现在可以运行了。To view the replica set configuration, use rs.conf(). 要查看副本集配置,请使用rs.conf()To check the status of the replica set, use rs.status().要检查副本集的状态,请使用rs.status()

  5. Disconnect from the instance.断开与实例的连接。
  6. Connect mongosh to one of the sharded cluster's mongos instances and retrieve the shard information:mongosh连接到分片集群的mongos实例之一,并检索分片信息:

    var myShard = db.getSiblingDB("config").shards.findOne( { _id: "<name>"} )

    Replace <name> with the name of the shard. <name>替换为分片的名称。The <name> of the shard is separate from the shard replica set name (unless you are using the shard name as the replica set name). 分片的<name>与分片副本集名称是分开的(除非您使用分片名称作为副本集名称)。To retrieve the name of the shard, see the shards section in the results from the sh.status() method. 要检索分片的名称,请参阅sh.status()方法结果中的shards部分。For example, if the result of sh.status() includes the following shards section, the name of the two shards are "shard0000" and "shard0001" respectively:例如,如果sh.status()的结果包含以下分片部分,则这两个分片的名称分别为"shard0000""shard0001"

    shards:
          {  "_id" : "shard0000",  "host" : "mongodb1.example.net:27018",  "state" : 1 }
          {  "_id" : "shard0001",  "host" : "mongodb2.example.net:27018",  "state" : 1 }
  7. Update the host information with the replica set information:使用副本集信息更新host信息:

    myShard.host = "<replica-set>/<member>"

    Replace <replica-set> with the name of the replica set. 用副本集的名称替换<replica set>Replace <member> with the replica set member. <member>替换为副本集成员。For example shardA/mongodb1.example.net:27018.例如shardA/mongodb1.example.net:27018

  8. Save the information.保存信息。

    db.getSiblingDB("config").shards.save(myShard, { writeConcern: { w: "majority" } } )
  9. Repeat for the next standalone shard in the sharded cluster. 对分片集群中的下一个独立分片重复上述步骤。Ensure that you use a distinct name for each shard replica set.确保为每个分片副本集使用不同的名称。
  10. Once you have finished converting shard standalone instances to shard replica sets, force the members of sharded cluster to update their knowledge of other shards' connection strings by restarting all members of the sharded cluster:完成将分片独立实例转换为分片副本集后,通过重新启动分片群集的所有成员,强制分片群集的成员更新对其他分片连接字符串的了解:

    • config server replica sets配置服务器副本集
    • mongos instances实例
    • shard replica sets分片副本集

Additional Information附加信息

To add members to this replica set, use the rs.add() method. 要将成员添加到此副本集,请使用rs.add()方法。For more information on adding members to a replica set, see Add Members to a Replica Set.有关将成员添加到副本集的详细信息,请参阅将成员添加至副本集

To convert a non-shard standalone to a non-shard replica set, see Convert a Standalone to a Replica Set instead.要将非分片独立副本集转换为非分片副本集,请参阅将独立副本转换为副本集

←  Convert a Replica Set to a Sharded ClusterSharding Reference →