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 在将3.4分片集群升级到3.6版本之前,必须将作为独立mongod
instances to replica set shards.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.要仅将单机版转换为副本集(即不属于任何分片群集),请参阅将单机版转化为副本集。
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.该过程假定单个成员与以前一样在同一主机和端口上运行。
mongod
instance.mongod
实例。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
手册页。
mongosh
to the shard mongod
instance.mongosh
连接mongosh
到分片mongod
实例。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()
。
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 }
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
。
Save the information.保存信息。
db.getSiblingDB("config").shards.save(myShard, { writeConcern: { w: "majority" } } )
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:完成将分片独立实例转换为分片副本集后,通过重新启动分片群集的所有成员,强制分片群集的成员更新对其他分片连接字符串的了解:
mongos
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.要将非分片独立副本集转换为非分片副本集,请参阅将独立副本转换为副本集。