我使用的软件版本是MongoDB 6.0.4和Mongosh 1.8.0。因为我的机器不多,所以我在本地机器上用不同的端口搭建了sharded cluster。分片集群配置如下:
[direct: mongos] test> sh.status()
shardingVersion
{
_id: 1,
minCompatibleVersion: 5,
currentVersion: 6,
clusterId: ObjectId("643fd1088dc14852e495a094")
}
---
shards
[
{
_id: 'shard001',
host: 'shard001/127.0.0.1:27117,127.0.0.1:27118,127.0.0.1:27119',
state: 1,
topologyTime: Timestamp({ t: 1681904960, i: 7 })
},
{
_id: 'shard002',
host: 'shard002/127.0.0.1:27217,127.0.0.1:27218,127.0.0.1:27219',
state: 1,
topologyTime: Timestamp({ t: 1681904968, i: 8 })
},
{
_id: 'shard003',
host: 'shard003/127.0.0.1:27317,127.0.0.1:27318,127.0.0.1:27319',
state: 1,
topologyTime: Timestamp({ t: 1681904975, i: 6 })
}
]
我创建了一个数据库“shardddb”和集合“user”和“users”。我为“shardddb”数据库启用了分片,并对“user”和“users”集合执行了分片操作。我还在集合中插入了一些数据。结果是:
{
database: {
_id: 'sharddb',
primary: 'shard002',
partitioned: false,
version: {
uuid: new UUID("e2603a9b-53e3-4ecd-8ef9-f35786431451"),
timestamp: Timestamp({ t: 1682148090, i: 1 }),
lastMod: 1
}
},
collections: {
'sharddb.user': {
shardKey: { name: 1 },
unique: false,
balancing: true,
chunkMetadata: [ { shard: 'shard002', nChunks: 1 } ],
chunks: [
{ min: { name: MinKey() }, max: { name: MaxKey() }, 'on shard': 'shard002', 'last modified': Timestamp({ t: 1, i: 0 }) }
],
tags: []
},
'sharddb.users': {
shardKey: { name: 'hashed' },
unique: false,
balancing: true,
chunkMetadata: [
{ shard: 'shard001', nChunks: 2 },
{ shard: 'shard002', nChunks: 2 },
{ shard: 'shard003', nChunks: 2 }
],
chunks: [
{ min: { name: MinKey() }, max: { name: Long("-6148914691236517204") }, 'on shard': 'shard001', 'last modified': Timestamp({ t: 1, i: 0 }) },
{ min: { name: Long("-6148914691236517204") }, max: { name: Long("-3074457345618258602") }, 'on shard': 'shard001', 'last modified': Timestamp({ t: 1, i: 1 }) },
{ min: { name: Long("-3074457345618258602") }, max: { name: Long("0") }, 'on shard': 'shard002', 'last modified': Timestamp({ t: 1, i: 2 }) },
{ min: { name: Long("0") }, max: { name: Long("3074457345618258602") }, 'on shard': 'shard002', 'last modified': Timestamp({ t: 1, i: 3 }) },
{ min: { name: Long("3074457345618258602") }, max: { name: Long("6148914691236517204") }, 'on shard': 'shard003', 'last modified': Timestamp({ t: 1, i: 4 }) },
{ min: { name: Long("6148914691236517204") }, max: { name: MaxKey() }, 'on shard': 'shard003', 'last modified': Timestamp({ t: 1, i: 5 }) }
],
tags: []
}
}
}
但是,'shardddb'的'partitioned'属性仍然是假的,我认为这是不正常的。我希望有人能帮我解决这个问题。
[direct: mongos] config> sh.enableSharding("sharddb");
{
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1683086356, i: 1 }),
signature: {
hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
keyId: Long("0")
}
},
operationTime: Timestamp({ t: 1683086356, i: 1 })
}
[direct: mongos] config> db.databases.find()
[
{
_id: 'sharddb',
primary: 'shard002',
partitioned: false,
version: {
uuid: new UUID("e2603a9b-53e3-4ecd-8ef9-f35786431451"),
timestamp: Timestamp({ t: 1682148090, i: 1 }),
lastMod: 1
}
},
看起来你还没有分片键。请参阅以下文章以了解更多信息。