下面是分片创建从无到有的过程举例:
1、创建表,只有一个字段name,并插入数据 mongos> use aaa switched to db aaa mongos> db.aaa.insert({name:1}) WriteResult({ "nInserted" : 1 }) mongos> db.aaa.insert({name:2}) WriteResult({ "nInserted" : 1 }) mongos> db.aaa.insert({name:3}) WriteResult({ "nInserted" : 1 }) mongos> db.aaa.insert({name:4}) WriteResult({ "nInserted" : 1 }) mongos> 2、查看数据 mongos> db.aaa.find() { "_id" : ObjectId("5fdb7d54d91f2f9bae3b09a1"), "name" : 1 } { "_id" : ObjectId("5fdb7d56d91f2f9bae3b09a2"), "name" : 2 } { "_id" : ObjectId("5fdb7d59d91f2f9bae3b09a3"), "name" : 3 } { "_id" : ObjectId("5fdb7d5cd91f2f9bae3b09a4"), "name" : 4 } 3、允许数据库分片 mongos> sh.enableSharding("aaa") { "ok" : 1, "operationTime" : Timestamp(1608220038, 3), "$clusterTime" : { "clusterTime" : Timestamp(1608220038, 3), "signature" : { "hash" : BinData(0,"shemm3xvSYrMiy9t7gSYcVtFUuE="), "keyId" : NumberLong("6894922308364795934") } } } mongos> 4、在name字段创建hash索引 mongos> db.aaa.createIndex({name:"hashed"},{background:true}) { "raw" : { "sharding_yeyz/127.0.0.1:27018,127.0.0.1:27019,127.0.0.1:27020" : { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } }, "ok" : 1, "operationTime" : Timestamp(1608220115, 3), "$clusterTime" : { "clusterTime" : Timestamp(1608220115, 3), "signature" : { "hash" : BinData(0,"S3Wz9G26eJyOcwa1OLS6TVYu6SE="), "keyId" : NumberLong("6894922308364795934") } } } 5、以name字段作为片键创建哈希分片 mongos> sh.shardCollection("aaa.aaa",{name:"hashed"}) { "collectionsharded" : "aaa.aaa", "collectionUUID" : UUID("20a3895e-d821-43ae-9d28-305e6ae03bbc"), "ok" : 1, "operationTime" : Timestamp(1608220238, 10), "$clusterTime" : { "clusterTime" : Timestamp(1608220238, 10), "signature" : { "hash" : BinData(0,"qeQlD3jsSvRZkyamEa2hjbezEdM="), "keyId" : NumberLong("6894922308364795934") } } } 6、查看分片信息 mongos> db.printShardingStatus() --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("5fafaf4f5785d9965548f687") } shards: { "_id" : "sharding_yeyz", "host" : "sharding_yeyz/127.0.0.1:27018,127.0.0.1:27019,127.0.0.1:27020", "state" : 1 } { "_id" : "sharding_yeyz1", "host" : "sharding_yeyz1/127.0.0.1:27024,127.0.0.1:27025,127.0.0.1:27026", "state" : 1 } active mongoses: "4.0.6" : 1 autosplit: Currently enabled: yes balancer: Currently enabled: yes Currently running: no Failed balancer rounds in last 5 attempts: 2 Last reported error: Could not find host matching read preference { mode: "primary" } for set sharding_yeyz Time of Reported error: Wed Nov 18 2020 17:08:14 GMT+0800 (CST) Migration Results for the last 24 hours: No recent migrations databases: { "_id" : "aaa", "primary" : "sharding_yeyz", "partitioned" : true, "version" : { "uuid" : UUID("26e55931-d1c1-4dc5-8a03-b5b0e70f6f43"), "lastMod" : 1 } } aaa.aaa shard key: { "name" : "hashed" } unique: false balancing: true chunks: sharding_yeyz 1 { "name" : { "$minKey" : 1 } } -->> { "name" : { "$maxKey" : 1 } } on : sharding_yeyz Timestamp(1, 0)
以上就是详解MongoDB范围片键和哈希片键的详细内容,更多关于MongoDB范围片键和哈希片键的资料请关注其它相关文章!