zoukankan      html  css  js  c++  java
  • mongodb 手动分片的命令汇总

    手动分片的操作

    自动分片会带来性能的下降. 所以要合理使用手动分片. 并且配合Tag一起使用.

    # 对于4个shard的程序, 预先处理的指令
    1. 加入分片服务器
    sh.addShard( "192.168.1.60:27017" )
    sh.addShard( "192.168.1.61:27017" )
    sh.addShard( "192.168.1.62:27017" )
    sh.addShard( "192.168.1.63:27017" )

    2. 启动collection分片, 并指定分片键
    db.location.ensureIndex({"hostid":1})
    sh.enableSharding("mydb")
    sh.shardCollection("mydb.location", { "hostid": 1})

    运行 sh.status()

    { "_id" : "mydb", "partitioned" : true, "primary" : "shard0003" }
    mydb.location
    shard key: { "hostid" : 1 }
    chunks:
    shard0003 1
    { "hostid" : { "$minKey" : 1 } } -->> { "hostid" : { "$maxKey" : 1 } } on : shard0003 Timestamp(1, 0)

    sh.addShardTag("shard0000", "TAG0")
    sh.addShardTag("shard0001", "TAG1")
    sh.addShardTag("shard0002", "TAG2")
    sh.addShardTag("shard0003", "TAG3")

    # 具体操作
    1. 加入tagrange,
    加入两个
    sh.addTagRange("mydb.location", { hostid: "0000000" }, { hostid: "3100000" }, "TAG0")
    sh.addTagRange("mydb.location", { hostid: "3100000" }, { hostid: "3200000" }, "TAG1")

    由于当前的primary位 shard0003,
    sh.addTagRange("mydb.location", { hostid: "3200000" }, { hostid: "3300000" }, "TAG2")
    sh.addTagRange("mydb.location", { hostid: "3300000" }, { hostid: "3500000" }, "TAG3")
    sh.addTagRange("mydb.location", { hostid: "3500000" }, { $maxKey: 1 }, "TAG3")


    4. add chunck
    // 将primary 放到第二个分片上
    db.runCommand({movePrimary:"mydb",to:"shard0001"})
    查看分片情况
    db.chunks.find({ns:"mydb.location"})

    手动建立空的chunk
    for ( var x=300; x<350; x++ ){
    var prefix = String(x*10000);
    sh.splitAt( "mydb.location", { "hostid":prefix } )
    }

    然后移动chunk到对应的地方, 移动chunk的命令
    sh.moveChunk("mydb.location", { hostid: "3000000"}, "shard0000")

    ==shard0000
    for ( var x=300; x<310; x++ ){
    var prefix = String(x*10000);
    sh.moveChunk( "mydb.location", { "hostid":prefix }, "shard0000" )
    }

    ==shard0001 310 - 320
    ==shard0002 320 - 330
    ==shard0003 330 - 350


    其他一些查询操作
    // 查找大于 340000的数据, 并且
    db.chunks.find({ns:"mydb.location", min:{$gte:{ "hostid" : "3400000" }}})

    db.getSiblingDB("admin").runCommand( { moveChunk : "mydb.location" ,
    find : { "hostid" : "3400000" } ,
    to : "shard0003" } )


    1. 建立索引
    use mydb
    db.location.ensureIndex({"hostid":1})
    db.location.ensureIndex({"posTime":1})
    db.location.ensureIndex({"hostno":1})
    db.location.createIndex( { loc : "2dsphere" } )

    db.location.ensureIndex({"posTime":1,"loc":"2dsphere"})
    db.location.ensureIndex({"posTime":1,"hostno":1,"loc":"2dsphere"})
    db.location.getIndexes()

  • 相关阅读:
    A1044. Shopping in Mars (25)
    A1019. General Palindromic Number (20)
    A1018. Public Bike Management (30)
    26850: 收集数码晶体 有40%错误
    A1016. Phone Bills (25)
    A1014. Waiting in Line (30)
    A1011. World Cup Betting (20)
    A1010. Radix (25)
    A1009. Product of Polynomials (25)
    A1008. Elevator (20)
  • 原文地址:https://www.cnblogs.com/myibm/p/5939382.html
Copyright © 2011-2022 走看看