3.使用分片集群

  集群分片的本质就是将待插入集合的数据按一定规则分散存储到集群中的各个分片上,查询时先根据规则计算出待返回数据所在的分片,再将数据从具体的分片返回客户端。

  我们在上篇中已经搭建好了分片集群,因此,这里就需要怎样用好分片集群

1.第一步

  通过mongos,连接到集群中

2.第二步

  在集群中新建一个数据库crm和集合customers 

  mongos> use crm;
  switched to db crm
  mongos> db.customers.insert({cust_id:1,cust_name:"bruce",city:"beijing"})

  当执行完之后,再通过sh.status进行查看  

{  "_id" : "crm",  "primary" : "rs1",  "partitioned" : false,  "version" : {  "uuid" : UUID("32c963ac-bcba-46ae-8b90-c672f41797d0"),  "lastMod" : 1 } }
  • "partitioned":"false":表示此时crm数据库还未支持分片。
  • "primary":"rs1":表示crm数据库中所有分分片的集合将保存在rs1中

  与此同时,我们可以看到该数据库crm确实在rs1上而没有在rs0上。  

复制代码
rs1:PRIMARY> show dbs;
admin   0.000GB
config  0.001GB
crm     0.000GB            //这里看到rs1中确实存在crm
local   0.001GB
rs1:PRIMARY> 

rs0:PRIMARY> show dbs;
admin   0.000GB
config  0.000GB            //rs0分片中没有crm数据库
local   0.001GB
rs0:PRIMARY> 
复制代码

3.第三步

  修改配置,使上面创建的customers集合分片存储

  首先想要使用集合分片就必须先使其所在的数据库支持分片,执行语句如下:

复制代码
mongos> sh.enableSharding("crm")   //开启数据库分片功能
{
    "ok" : 1,
    "operationTime" : Timestamp(1651658325, 4),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1651658325, 4),
        "signature" : {
            "hash" : BinData(0,"liGwlx3V5oYuK0KK8pjhksHYq/M="),
            "keyId" : NumberLong("7093727239165968407")
        }
    }
}
复制代码

  对已有的业务数据集合分片,必须现在所选择的片键上创建一个索引,如果集合初始时没有任何业务数据,则MongoDB会自动在所选择的片键上创建一个索引。

  比如说这里使用"_id"作为片键(hash) 哈希片键,先创建hash索引 

复制代码
mongos> db.customers.ensureIndex({_id:1})
{
    "raw" : {
        "rs1/172.16.40.23:27017,172.16.40.23:27018,172.16.40.23:27019" : {
            "createdCollectionAutomatically" : true,
            "numIndexesBefore" : 1,
            "numIndexesAfter" : 2,
            "commitQuorum" : "votingMembers",
            "ok" : 1
        }
    },
    "ok" : 1,
    "operationTime" : Timestamp(1651660144, 3),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1651660144, 3),
        "signature" : {
            "hash" : BinData(0,"ZtoXsgPKIFpcnZH7WKs1vTK2e8w="),
            "keyId" : NumberLong("7093727239165968407")
        }
    }
}
复制代码

  然后对这个collection(这里指的是customers)启用分片 

复制代码
mongos> sh.shardCollection("crm.customers",{_id:"hashed"})
{
    "collectionsharded" : "crm.customers",
    "collectionUUID" : UUID("9a9e676e-49b6-46c7-94cc-623492e62704"),
    "ok" : 1,
    "operationTime" : Timestamp(1651660499, 24),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1651660499, 24),
        "signature" : {
            "hash" : BinData(0,"JFFASwvoe0fcgBDbIW8myz3NZJc="),
            "keyId" : NumberLong("7093727239165968407")
        }
    }
}
mongos> 
复制代码

4.第四步

  插入数据,观察分片状态 

复制代码
//这里是插入数据
mongos> for(var i=1;i<1000;i++){db.customers.insert({"number":i})} WriteResult({ "nInserted" : 1 })
//使用printShardingStatus查看分片状态 mongos
> db.printShardingStatus() --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("6271fb5740017b4bb9234448") } shards: //这里可以看出来设计的分片有两个了,分别是复制集rs0合复制集rs1... { "_id" : "rs0", "host" : "rs0/172.16.40.22:27017,172.16.40.22:27018,172.16.40.22:27019", "state" : 1 } { "_id" : "rs1", "host" : "rs1/172.16.40.23:27017,172.16.40.23:27018,172.16.40.23:27019", "state" : 1 } active mongoses: "4.4.12" : 1 autosplit: Currently enabled: yes balancer: Currently enabled: yes Currently running: no Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: 512 : Success databases: { "_id" : "config", "primary" : "config", "partitioned" : true } config.system.sessions shard key: { "_id" : 1 } unique: false balancing: true chunks: rs0 512 rs1 512 too many chunks to print, use verbose if you want to force print { "_id" : "crm", "primary" : "rs1", "partitioned" : true, "version" : { "uuid" : UUID("32c963ac-bcba-46ae-8b90-c672f41797d0"), "lastMod" : 1 } } crm.customers shard key: { "_id" : "hashed" } unique: false balancing: true chunks: rs0 2 rs1 2 { "_id" : { "$minKey" : 1 } } -->> { "_id" : NumberLong("-4611686018427387902") } on : rs0 Timestamp(1, 0) { "_id" : NumberLong("-4611686018427387902") } -->> { "_id" : NumberLong(0) } on : rs0 Timestamp(1, 1) { "_id" : NumberLong(0) } -->> { "_id" : NumberLong("4611686018427387902") } on : rs1 Timestamp(1, 2) { "_id" : NumberLong("4611686018427387902") } -->> { "_id" : { "$maxKey" : 1 } } on : rs1 Timestamp(1, 3) mongos>
复制代码

  此时可以查看数据的分布情况,在复制集rs0上可以看到:

复制代码
rs0:PRIMARY> db.customers.find()
{ "_id" : ObjectId("627257bfc161ca8ef2fee0b2"), "number" : 4 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0b4"), "number" : 6 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0b6"), "number" : 8 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0b7"), "number" : 9 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0b8"), "number" : 10 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0ba"), "number" : 12 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0bc"), "number" : 14 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0bd"), "number" : 15 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0be"), "number" : 16 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0bf"), "number" : 17 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0c1"), "number" : 19 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0c2"), "number" : 20 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0c3"), "number" : 21 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0c4"), "number" : 22 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0c9"), "number" : 27 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0cb"), "number" : 29 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0cc"), "number" : 30 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0ce"), "number" : 32 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0d0"), "number" : 34 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0d3"), "number" : 37 }
Type "it" for more
复制代码

  在复制集rs1上可以看到:

复制代码
rs1:PRIMARY> db.customers.find()
{ "_id" : ObjectId("627257bfc161ca8ef2fee0af"), "number" : 1 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0b0"), "number" : 2 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0b1"), "number" : 3 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0b3"), "number" : 5 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0b5"), "number" : 7 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0b9"), "number" : 11 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0bb"), "number" : 13 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0c0"), "number" : 18 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0c5"), "number" : 23 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0c6"), "number" : 24 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0c7"), "number" : 25 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0c8"), "number" : 26 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0ca"), "number" : 28 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0cd"), "number" : 31 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0cf"), "number" : 33 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0d1"), "number" : 35 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0d2"), "number" : 36 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0d4"), "number" : 38 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0d6"), "number" : 40 }
{ "_id" : ObjectId("627257bfc161ca8ef2fee0d8"), "number" : 42 }
Type "it" for more
复制代码

   ok!!!可以在看数据已经分布到不同的复制集中了

  

测试实例:

use crm;
sh.enableSharding("crm")
sh.shardCollection("crm.customers",{"_id":"hashed"})
for(var i=1;i<1000;i++){db.customers.insert({"number":i})}
 
 
 
 

  

posted on   太白金星有点烦  阅读(121)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示