ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

MongoDB集群分片管理之数据库分片

2021-04-16 11:56:54  阅读:189  来源: 互联网

标签:uuid MongoDB 数据库 分片 primary mongos 172.16 id UUID


分片整体信息

mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
    "_id" : 1,
    "minCompatibleVersion" : 5,
    "currentVersion" : 6,
    "clusterId" : ObjectId("60545017224c766911a9c440")
  }
  shards:
        {  "_id" : "hdshard1",  "host" : "hdshard1/172.16.254.136:40001,172.16.254.137:40001,172.16.254.138:40001",  "state" : 1 }
        {  "_id" : "hdshard2",  "host" : "hdshard2/172.16.254.136:40002,172.16.254.137:40002,172.16.254.138:40002",  "state" : 1 }
        {  "_id" : "hdshard3",  "host" : "hdshard3/172.16.254.136:40003,172.16.254.137:40003,172.16.254.138:40003",  "state" : 1 }
  active mongoses:
        "4.2.12" : 3
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
        Failed balancer rounds in last 5 attempts:  5
        Last reported error:  Could not find host matching read preference { mode: "primary" } for set hdshard2
        Time of Reported error:  Wed Apr 14 2021 19:16:07 GMT+0800 (CST)
        Migration Results for the last 24 hours: 
                No recent migrations
  databases:
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                config.system.sessions
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                hdshard1    342
                                hdshard2    341
                                hdshard3    341
                        too many chunks to print, use verbose if you want to force print
        {  "_id" : "db5",  "primary" : "hdshard2",  "partitioned" : true,  "version" : {  "uuid" : UUID("bc5d26af-5248-431b-8065-7143fdbffb97"),  "lastMod" : 1 } }
                db5.db5
                        shard key: { "id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                hdshard2    1
                        { "id" : { "$minKey" : 1 } } -->> { "id" : { "$maxKey" : 1 } } on : hdshard2 Timestamp(1, 0) 
        {  "_id" : "db6",  "primary" : "hdshard3",  "partitioned" : false,  "version" : {  "uuid" : UUID("6a1dd60f-a1fc-47be-9fad-d8ce01841d68"),  "lastMod" : 1 } }
        {  "_id" : "recommend",  "primary" : "hdshard1",  "partitioned" : false,  "version" : {  "uuid" : UUID("cb833b8e-cc4f-4c52-83c3-719aa383bac4"),  "lastMod" : 1 } }

列出分片信息

mongos> use admin
switched to db admin
mongos> db.runCommand( { listshards : 1 } )
{
    "shards" : [
        {
            "_id" : "hdshard1",
            "host" : "hdshard1/172.16.254.136:40001,172.16.254.137:40001,172.16.254.138:40001",
            "state" : 1
        },
        {
            "_id" : "hdshard2",
            "host" : "hdshard2/172.16.254.136:40002,172.16.254.137:40002,172.16.254.138:40002",
            "state" : 1
        },
        {
            "_id" : "hdshard3",
            "host" : "hdshard3/172.16.254.136:40003,172.16.254.137:40003,172.16.254.138:40003",
            "state" : 1
        }
    ],
    "ok" : 1,
    "operationTime" : Timestamp(1618479240, 1),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1618479240, 1),
        "signature" : {
            "hash" : BinData(0,"ou1dzkqck0XTgvqkH/xBlhXC0R4="),
            "keyId" : NumberLong("6941260985399246879")
        }
    }
}

或者

mongos> use config
switched to db config
mongos> db.shards.find()
{ "_id" : "hdshard1", "host" : "hdshard1/172.16.254.136:40001,172.16.254.137:40001,172.16.254.138:40001", "state" : 1 }
{ "_id" : "hdshard2", "host" : "hdshard2/172.16.254.136:40002,172.16.254.137:40002,172.16.254.138:40002", "state" : 1 }
{ "_id" : "hdshard3", "host" : "hdshard3/172.16.254.136:40003,172.16.254.137:40003,172.16.254.138:40003", "state" : 1 }

查看所有数据库分片情况

mongos> use config
switched to db config
mongos> db.databases.find() 
{ "_id" : "db5", "primary" : "hdshard2", "partitioned" : true, "version" : { "uuid" : UUID("bc5d26af-5248-431b-8065-7143fdbffb97"), "lastMod" : 1 } }
{ "_id" : "db6", "primary" : "hdshard3", "partitioned" : false, "version" : { "uuid" : UUID("6a1dd60f-a1fc-47be-9fad-d8ce01841d68"), "lastMod" : 1 } }
{ "_id" : "recommend", "primary" : "hdshard1", "partitioned" : false, "version" : { "uuid" : UUID("cb833b8e-cc4f-4c52-83c3-719aa383bac4"), "lastMod" : 1 } }

可以看到db5开启了分片,db6和recommend没有开启分片。

激活数据库分片功能

mongos> sh.enableSharding("db6")
{
    "ok" : 1,
    "operationTime" : Timestamp(1618480521, 3),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1618480521, 3),
        "signature" : {
            "hash" : BinData(0,"ReiWhtUTkbTud9SM+7N2+WyhBcs="),
            "keyId" : NumberLong("6941260985399246879")
        }
    }
}
mongos> 
mongos> sh.enableSharding("recommend")
{
    "ok" : 1,
    "operationTime" : Timestamp(1618480562, 4),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1618480562, 4),
        "signature" : {
            "hash" : BinData(0,"NJG21ltpt/C7h5SH7CKvnT95I+k="),
            "keyId" : NumberLong("6941260985399246879")
        }
    }
}

验证数据库分片情况

mongos> db.databases.find()
{ "_id" : "db5", "primary" : "hdshard2", "partitioned" : true, "version" : { "uuid" : UUID("bc5d26af-5248-431b-8065-7143fdbffb97"), "lastMod" : 1 } }
{ "_id" : "db6", "primary" : "hdshard3", "partitioned" : true, "version" : { "uuid" : UUID("6a1dd60f-a1fc-47be-9fad-d8ce01841d68"), "lastMod" : 1 } }
{ "_id" : "recommend", "primary" : "hdshard1", "partitioned" : true, "version" : { "uuid" : UUID("cb833b8e-cc4f-4c52-83c3-719aa383bac4"), "lastMod" : 1 } }

可以看到db6和recommend库都已活数据库分片。

判断是否为分片集群

mongos> db.runCommand({ isdbgrid : 1})
{
    "isdbgrid" : 1,
    "hostname" : "mongo7",
    "ok" : 1,
    "operationTime" : Timestamp(1618486042, 13),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1618486042, 13),
        "signature" : {
            "hash" : BinData(0,"3k9SyHGJqe/nbacFkPTxo59smNA="),
            "keyId" : NumberLong("6941260985399246879")
        }
    }
}

列出开启分片的数据库

mongos> use config
switched to db config
mongos> db.databases.find( { "partitioned": true } )
{ "_id" : "db5", "primary" : "hdshard2", "partitioned" : true, "version" : { "uuid" : UUID("bc5d26af-5248-431b-8065-7143fdbffb97"), "lastMod" : 1 } }
{ "_id" : "db6", "primary" : "hdshard3", "partitioned" : true, "version" : { "uuid" : UUID("6a1dd60f-a1fc-47be-9fad-d8ce01841d68"), "lastMod" : 1 } }
{ "_id" : "recommend", "primary" : "hdshard1", "partitioned" : true, "version" : { "uuid" : UUID("cb833b8e-cc4f-4c52-83c3-719aa383bac4"), "lastMod" : 1 } }

查看分片键

mongos> db.collections.find()
{ "_id" : "config.system.sessions", "lastmodEpoch" : ObjectId("60547131a531190b111e42a0"), "lastmod" : ISODate("1970-02-19T17:02:47.296Z"), "dropped" : false, "key" : { "_id" : 1 }, "unique" : false, "uuid" : UUID("f8a8e664-10d2-41fb-84ec-a165579ee86d") }
{ "_id" : "db5.db5", "lastmodEpoch" : ObjectId("6054804d6410651de69524d9"), "lastmod" : ISODate("1970-02-19T17:02:47.296Z"), "dropped" : false, "key" : { "id" : 1 }, "unique" : false, "uuid" : UUID("2122da21-7279-4b88-b60f-73dd3d0276fb") }

标签:uuid,MongoDB,数据库,分片,primary,mongos,172.16,id,UUID
来源: https://blog.51cto.com/u_12592884/2710493

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有