• Mongodb的分片集群基本操作


    查看数据插入分片后分布情况

    第一种方式:
    mongos> use articledb switched to db articledb
    mongos> db.comment.getShardDistribution() Shard hqmongodb2 at hqmongodb2/192.168.1.103:27017,192.168.1.105:27017 data : 19KiB docs : 493 chunks : 2 estimated data per chunk : 9KiB estimated docs per chunk : 246 Shard hqmongodb at hqmongodb/192.168.1.100:27017,192.168.1.101:27017 data : 19KiB docs : 507 chunks : 2 estimated data per chunk : 9KiB estimated docs per chunk : 253 Totals data : 38KiB docs : 1000 chunks : 4 Shard hqmongodb2 contains 49.28% data, 49.3% docs in cluster, avg obj size on shard : 39B Shard hqmongodb contains 50.71% data, 50.7% docs in cluster, avg obj size on shard : 39B

    第二种方式:
    mongos> use articledb
    switched to db articledb

    mongos> db.comment.stats().sharded
    true #简单的返回true或者false

    第三种方式:去config库查询
    这种方法可以查到分片键信息
    mongos> use config
    switched to db config
    mongos> db.collections.find()

     分片集群操作

    1、不同分片片健的配置

    范围片健

    admin> sh.shardCollection("数据库名称.集合名称",key : {分片键: 1}  )
    或
    admin> db.runCommand( { shardcollection : "数据库名称.集合名称",key : {分片键: 1} } )

    eg:

    admin > sh.shardCollection("test.vast",key : {id: 1}  )
    或
    admin> db.runCommand( { shardcollection : "test.vast",key : {id: 1} } )

    哈希片键

    admin > sh.shardCollection( "数据库名.集合名", { 片键: "hashed" } )

    创建哈希索引

    admin> db.vast.ensureIndex( { a: "hashed" } )
    admin > sh.shardCollection( "test.vast", { a: "hashed" } )

    2、分片集群的操作

    判断是否Shard集群

    admin> db.runCommand({ isdbgrid : 1})

    列出所有分片信息

    admin> db.runCommand({ listshards : 1})

    列出开启分片的数据库

    admin> use config
    config> db.databases.find( { "partitioned": true } )
    config> db.databases.find() //列出所有数据库分片情况

    查看分片的片键

    config> db.collections.find()
    {
        "_id" : "test.vast",
        "lastmodEpoch" : ObjectId("58a599f19c898bbfb818b63c"),
        "lastmod" : ISODate("1970-02-19T17:02:47.296Z"),
        "dropped" : false,
        "key" : {
            "id" : 1
        },
        "unique" : false
    }

    查看分片的详细信息

    admin> db.printShardingStatus()
    或
    admin> sh.status()

    删除分片节点

    sh.getBalancerState()
    mongos> db.runCommand( { removeShard: "shard2" } )

    balance操作

    查看mongo集群是否开启了 balance 状态

    mongos> sh.getBalancerState()
    true

    当然你也可以通过在路由节点mongos上执行sh.status() 查看balance状态。

      如果balance开启,查看是否正在有数据的迁移

    连接mongo集群的路由节点

    mongos> sh.isBalancerRunning()
    false

    设置balance 窗口

    #切换配置节点
    use config
    #确定balance开启中 sh.getBalancerState()
    如果未开启执行命令 sh.setBalancerState(
    true )
    #修改balance 窗口的时间 db.settings.update( { _id:
    "balancer" }, { $set: { activeWindow : { start : "<start-time>", stop : "<stop-time>" } } }, { upsert: true } )

    eg:

    db.settings.update({ _id : "balancer" }, { $set : { activeWindow : { start : "00:00", stop : "5:00" } } }, true )

    注:当你设置了activeWindow,就不能用sh.startBalancer() 启动balance

    #删除balance 窗口
    use config db.settings.update({ _id :
    "balancer" }, { $unset : { activeWindow : true } })

    开启balance

    sh.enableBalancing("user")

    关闭balance

    默认balance 的运行可以在任何时间,只迁移需要迁移的chunk,如果要关闭balance运行,停止一段时间可以用下列方法:

    #停止balance
    sh.stopBalancer()
    #查看balance状态
    sh.getBalancerState()
    #停止balance 后,没有迁移进程正在迁移,可以执行下列命令
    use config
    while( sh.isBalancerRunning() ) {
              print("waiting...");
              sleep(1000);
    }

    关于集合的balance

    #关闭某个集合的balance
    sh.disableBalancing("students.grades")
    #打开某个集合的balance
    sh.enableBalancing("students.grades")
    #确定某个集合的balance是开启或者关闭
    db.getSiblingDB("config").collections.findOne({_id : "students.grades"}).noBalance;

    问题解决

    mongodb在做自动分片平衡的时候,或引起数据库响应的缓慢,可以通过禁用自动平衡以及设置自动平衡进行的时间来解决这一问题。

    禁用分片的自动平衡
    // connect to mongos
    > use config
    > db.settings.update( { _id: "balancer" }, { $set : { stopped: true } } , true );

    自定义 自动平衡进行的时间段

    // connect to mongos
    > use config
    > db.settings.update({ _id : "balancer" }, { $set : { activeWindow : { start : "21:00", stop : "9:00" } } }, true )
  • 相关阅读:
    oob中程序的监视
    使用内联 XAML
    silverlight 导航注意点
    动画入门,用actionscript实现A*寻路算法【游戏自动寻路】 转
    Remoting 转
    XSD文件 转
    Security 转
    游戏开发(程序)职位招聘的一些感受和经验 转
    WCF 转
    WPF 转
  • 原文地址:https://www.cnblogs.com/fat-girl-spring/p/13613636.html
Copyright © 2020-2023  润新知