其他分享
首页 > 其他分享> > 3.使用分片集群

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 } }

  与此同时,我们可以看到该数据库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:"hashed"})
{
    "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!!!可以在看数据已经分布到不同的复制集中了

  

  

标签:ObjectId,分片,number,rs1,集群,使用,id,crm
来源: https://www.cnblogs.com/zmc60/p/16221943.html