我运行命令以使用此命令对集合进行分片
sh.shardCollection("db.collection_name",{"_id":"hashed"})
我得到以下错误
{
"ok" : 0.0,
"errmsg" : "Please create an index that starts with the proposed shard key before sharding the collection",
"code" : 72,
"codeName" : "InvalidOptions",
"operationTime" : Timestamp(1582011118, 65),
"$clusterTime" : {
"clusterTime" : Timestamp(1582011118, 65),
"signature" : {
"hash" : { "$binary" : "jOIjTJkZKkC2ZI5lFQwX4Q7QNfs=", "$type" : "00" },
"keyId" : NumberLong(6774859010160984065)
}
}
}
已经有索引
_id
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "db.collection_name"
}
我正在运行 Mongo 4.0.0 分片集群设置
如果你正在分片已经填充的集合。那你应该试试 :
我有数据库测试和集合名称作为人。基本上你需要创建一个索引来分片已经填充的集合。从集合中选择密钥并对其进行散列
mongos> db.people.createIndex({ "user_id" : "hashed" }) ///
db.collection.createIndex({ "key" : "hashed"})
{
"raw" : {
"foo/localhost:27017,localhost:27018,localhost:27019" : {
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 4,
"numIndexesAfter" : 5,
"ok" : 1
}
},
"ok" : 1,
"operationTime" : Timestamp(1593154931, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1593154931, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
mongos> sh.shardCollection("test.people", { "user_id": "hashed" })
{
"collectionsharded" : "test.people",
"collectionUUID" : UUID("4ee30d04-8dc9-43dc-94f6-a898bc96da89"),
"ok" : 1,
"operationTime" : Timestamp(1593154958, 11),
"$clusterTime" : {
"clusterTime" : Timestamp(1593154958, 11),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
mongos> sh.isBalancerRunning()
真实
希望对您有所帮助!
感谢 Neha 分享步骤。我有类似的情况,并且能够使用上述步骤进行补救