背景:

      通过上一篇的 MongoDB 分片的原理、搭建、应用 大致了解了MongoDB分片的安装和一些基本的使用情况,现在来说明下如何管理和优化MongoDB分片的使用。

知识点:

1) 分片的配置和查看

① 添加分片:sh.addShard("IP:Port") 

mongos> sh.addShard("192.168.200.A:40000") #添加分片
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> sh.addShard("192.168.200.B:40000") #添加分片
{ "shardAdded" : "shard0001", "ok" : 1 }
mongos> sh.addShard("192.168.200.C:40000") #添加分片
{ "shardAdded" : "shard0002", "ok" : 1 }

② 开启分片功能:sh.enableSharding("库名")、sh.shardCollection("库名.集合名",{"key":1})

mongos> sh.enableSharding("dba")
{ "ok" : 1 }

mongos> sh.shardCollection("dba.account",{"name":1})
{ "collectionsharded" : "dba.account", "ok" : 1 }

③ 查看分片状态:sh.status()

mongos> sh.status()
--- Sharding Status ---...
  shards:
    {  "_id" : "shard0000",  "host" : "192.168.200.A:40000" }
    {  "_id" : "shard0001",  "host" : "192.168.200.B:40000" }
    {  "_id" : "shard0002",  "host" : "192.168.200.C:40000" }
...
  databases:  #库
    {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
    {  "_id" : "test",  "partitioned" : false,  "primary" : "shard0000" }
    {  "_id" : "dba",  "partitioned" : true,  "primary" : "shard0000" } #允许分片
        dba.account   #dba下的分片集合
            shard key: { "name" : 1 }                             
            chunks:   #块信息
                shard0000    1
            { "name" : { "$minKey" : 1 } } -->> { "name" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0)  #块里数据的范围

2) 配置信息说明,config库的介绍。

① 分片信息:config.shards  

mongos> db.shards.find()
{ "_id" : "shard0000", "host" : "192.168.200.51:40000" }
{ "_id" : "shard0001", "host" : "192.168.200.52:40000" }
{ "_id" : "shard0002", "host" : "192.168.200.53:40000" }
{ "_id" : "mablevi", "host" : "mablevi/192.168.200.53:50000,192.168.200.53:50001,192.168.200.53:50002" } #副本级分片,不需要把所有的节点写出

② 分片中所有数据库信息:config.databases

mongos> db.databases.find()
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : false, "primary" : "shard0001" }
{ "_id" : "dba", "partitioned" : true, "primary" : "shard0000" }  #分片
{ "_id" : "abc", "partitioned" : true, "primary" : "shard0000" }  #分片

③ 分片集合信息:config.collections

mongos> db.collections.findOne()
{
    "_id" : "dba.account",
    "lastmod" : ISODate("2015-07-14T15:12:29.706Z"),
    "dropped" : false,
    "key" : {          #片键
        "name" : 1
    },
    "unique" : false,  #片键是否唯一
    "lastmodEpoch" : ObjectId("55a526dd511d36716224fb77")
}

④ mongos路由的信息:config.mongs 。可以查看所有mongos的状态

mongos> db.mongos.findOne()
{
    "_id" : "mongo2:30000",
    "ping" : ISODate("2015-07-27T03:13:06.178Z"),
    "up" : 323671,      #活着时间
    "waiting" : true, 
    "mongoVersion" : "3.0.4" #版本
}

⑤ 均衡器锁的信息:config.locks,记录所有集群范围的锁,可得知哪个mongos是均衡器。

mongos> db.locks.findOne()
{
    "_id" : "balancer",   #均衡器
    "state" : 1,          #0非活动状态、1尝试得到锁,但还没得到,2表示正在进行均衡
    "who" : "mongo1:30000:1436888525:1804289383:Balancer:846930886", #哪个mongos当均衡器
    "ts" : ObjectId("55b5a2d5fdd9a605a039f951"),
    "process" : "mongo1:30000:1436888525:1804289383",
    "when" : ISODate("2015-07-27T03:17:41.159Z"),
    "why" : "doing balance round" #均衡导致锁
}

⑥ 记录所有块的信息:config.chunks,也可以通过sh.status()查看

mongos> db.chunks.find().pretty()
{
    "_id" : "dba.account-name_MinKey",   #标识符
    "lastmod" : Timestamp(2, 0),         #块的版本
    "lastmodEpoch" : ObjectId("55a526dd511d36716224fb77"), #块的版本
    "ns" : "dba.account",    #集合名
    "min" : {                #数据范围
        "name" : { "$minKey" : 1 }
    },
    "max" : {
        "name" : "9XXqCaBhfhPIXLq"
    },
    "shard" : "mablevi"      #所在分片
}
{
    "_id" : "dba.account-name_\"9XXqCaBhfhPIXLq\"",
    "lastmod" : Timestamp(4, 0),
    "lastmodEpoch" : ObjectId("55a526dd511d36716224fb77"),
    "ns" : "dba.account",
    "min" : {
        "name" : "9XXqCaBhfhPIXLq"
    },
    "max" : {
        "name" : "RWINvgjYYQmbZds"
    },
    "shard" : "shard0002"
}

⑦ 记录所有的分片操作:config.changelog,拆分、迁移

拆分:

{
    "_id" : "mongo1-2015-07-14T15:12:40-55a526e8f0432675a473009c",
    "server" : "mongo1",
    "clientAddr" : "192.168.200.52:53257",
    "time" : ISODate("2015-07-14T15:12:40.375Z"),
    "what" : "multi-split",  #拆分
    "ns" : "dba.account",
    "details" : {            #拆分先后的信息
        "before" : {
            "min" : {        #拆分前范围
                "name" : { "$minKey" : 1 }
            },
            "max" : {
                "name" : { "$maxKey" : 1 }
            }
        },
        "number" : 1,   #第一个块
        "of" : 3,       #拆分成3个块
        "chunk" : {
            "min" : {
                "name" : { "$minKey" : 1 }
            },
            "max" : {
                "name" : "9XXqCaBhfhPIXLq"
            },
            "lastmod" : Timestamp(1, 1), #版本号
            "lastmodEpoch" : ObjectId("55a526dd511d36716224fb77")
        }
    }
}
{
    "_id" : "mongo1-2015-07-14T15:12:40-55a526e8f0432675a473009d",
    "server" : "mongo1",
    "clientAddr" : "192.168.200.52:53257",
    "time" : ISODate("2015-07-14T15:12:40.378Z"),
    "what" : "multi-split",
    "ns" : "dba.account",
    "details" : {
        "before" : {
            "min" : {
                "name" : { "$minKey" : 1 }
            },
            "max" : {
                "name" : { "$maxKey" : 1 }
            }
        },
        "number" : 2,
        "of" : 3,
        "chunk" : {
            "min" : {
                "name" : "9XXqCaBhfhPIXLq"
            },
            "max" : {
                "name" : "okmjUUZuuKgftDC"
            },
            "lastmod" : Timestamp(1, 2), #版本号
            "lastmodEpoch" : ObjectId("55a526dd511d36716224fb77")
        }
    }
}
{
    "_id" : "mongo1-2015-07-14T15:12:40-55a526e8f0432675a473009e",
    "server" : "mongo1",
    "clientAddr" : "192.168.200.52:53257",
    "time" : ISODate("2015-07-14T15:12:40.381Z"),
    "what" : "multi-split",
    "ns" : "dba.account",
    "details" : {
        "before" : {
            "min" : {
                "name" : { "$minKey" : 1 }
            },
            "max" : {
                "name" : { "$maxKey" : 1 }
            }
        },
        "number" : 3,
        "of" : 3,
        "chunk" : {
            "min" : {
                "name" : "okmjUUZuuKgftDC"
            },
            "max" : {
                "name" : { "$maxKey" : 1 }
            },
            "lastmod" : Timestamp(1, 3),  #版本号
            "lastmodEpoch" : ObjectId("55a526dd511d36716224fb77")
        }
    }
}

迁移:4个文档

{
    "_id" : "mongo1-2015-07-14T15:12:41-55a526e9f0432675a47300a1",
    "server" : "mongo1",
    "clientAddr" : "192.168.200.52:53257",
    "time" : ISODate("2015-07-14T15:12:41.406Z"),
    "what" : "moveChunk.start",  #迁移开始
    "ns" : "dba.account",
    "details" : {
        "min" : {
            "name" : { "$minKey" : 1 }
        },
        "max" : {
            "name" : "9XXqCaBhfhPIXLq"
        },
        "from" : "shard0000",
        "to" : "mablevi"
    }
}
{
    "_id" : "mongo3-2015-07-14T15:12:42-55a526ead5bee637c12aadd4",
    "server" : "mongo3",
    "clientAddr" : ":27017",
    "time" : ISODate("2015-07-14T15:12:42.419Z"),
    "what" : "moveChunk.to",   #from分片
    "ns" : "dba.account",
    "details" : {
        "min" : {
            "name" : { "$minKey" : 1 }
        },
        "max" : {
            "name" : "9XXqCaBhfhPIXLq"
        },
        "step 1 of 5" : 327,  #耗时,单位毫秒。检查命令参数
        "step 2 of 5" : 301,  #申请分布锁
        "step 3 of 5" : 1,    #连接到to分片
        "step 4 of 5" : 0,    #数据复制,每个分片都是直接和另一个分片、配置服务器直接连接
        "step 5 of 5" : 407,  #和to分片、配置服务器确认是否完成
        "note" : "success"
    }
}
{
    "_id" : "mongo1-2015-07-14T15:12:42-55a526eaf0432675a47300a2",
    "server" : "mongo1",
    "clientAddr" : "192.168.200.52:53257",
    "time" : ISODate("2015-07-14T15:12:42.854Z"),
    "what" : "moveChunk.commit",  #提交
    "ns" : "dba.account",
    "details" : {
        "min" : {
            "name" : { "$minKey" : 1 }
        },
        "max" : {
            "name" : "9XXqCaBhfhPIXLq"
        },
        "from" : "shard0000",
        "to" : "mablevi",
        "cloned" : NumberLong(1),
        "clonedBytes" : NumberLong(94),
        "catchup" : NumberLong(0),
        "steady" : NumberLong(0)
    }
}
{
    "_id" : "mongo1-2015-07-14T15:12:43-55a526ebf0432675a47300a3",
    "server" : "mongo1",
    "clientAddr" : "192.168.200.52:53257",
    "time" : ISODate("2015-07-14T15:12:43.258Z"),
    "what" : "moveChunk.from",  #to分片
    "ns" : "dba.account",
    "details" : {
        "min" : {
            "name" : { "$minKey" : 1 }
        },
        "max" : {
            "name" : "9XXqCaBhfhPIXLq"
        },    #步骤的时间为毫秒
        "step 1 of 6" : 0,      #迁移索引 
        "step 2 of 6" : 613,    #删除块内的数据,清理数据残余
        "step 3 of 6" : 2,      #文档复制到to分片
        "step 4 of 6" : 1029,   #复制期间,在to分片上执行操作
        "step 5 of 6" : 415,    #等待to分片将新迁移过来的数据复制到集群
        "step 6 of 6" : 0,      #修改元数据完成迁移
"to" : "mablevi",
        "from" : "shard0000",
        "note" : "success"
    }
}

⑧ 分片标签:config.tags,sh.addShardTag

mongos> db.tags.findOne()
{
    "_id" : {
        "ns" : "abc.number",
        "min" : {
            "num" : 0
        }
    },
    "ns" : "abc.number",
    "min" : {
        "num" : 0
    },
    "max" : {
        "num" : 20
    },
    "tag" : "AAA"
}

⑨ 分片设置:config.settings,设置分片块的大小和开启关闭均衡器

mongos> db.settings.find()
{ "_id" : "chunksize", "value" : 32 }
{ "_id" : "balancer", "stopped" : false }

⑩ 网络连接数: db.adminCommand({"connPoolStats":1})

mongos> db.adminCommand({"connPoolStats":1})
{
    "hosts" : {
        "192.168.200.51:20000::0" : {
            "available" : 1,
            "created" : 1
        },
        "192.168.200.51:20000::30" : {
            "available" : 1,
            "created" : 1
        },
        "192.168.200.51:20000,192.168.200.51:21000,192.168.200.51:22000::0" : {
            "available" : 1,
            "created" : 1
        },
        "192.168.200.51:20000,192.168.200.51:21000,192.168.200.51:22000::30" : {
            "available" : 3,
            "created" : 422
        },
        "192.168.200.51:21000::0" : {
            "available" : 1,
            "created" : 1
        },
        "192.168.200.51:21000::30" : {
            "available" : 1,
            "created" : 1
        },
        "192.168.200.51:22000::0" : {
            "available" : 1,
            "created" : 1
        },
        "192.168.200.51:22000::30" : {
            "available" : 1,
            "created" : 1
        },
        "192.168.200.51:40000::0" : {
            "available" : 2,
            "created" : 2
        },
        "192.168.200.52:40000::0" : {
            "available" : 1,
            "created" : 4
        },
        "192.168.200.53:40000::0" : {
            "available" : 1,
            "created" : 2
        },
        "192.168.200.53:50000::5" : {
            "available" : 1,
            "created" : 2
        },
        "192.168.200.53:50001::0" : {
            "available" : 1,
            "created" : 1
        },
        "192.168.200.53:50001::5" : {
            "available" : 1,
            "created" : 2
        },
        "192.168.200.53:50002::0" : {
            "available" : 1,
            "created" : 1
        },
        "192.168.200.53:50002::5" : {
            "available" : 1,
            "created" : 2
        },
        "mablevi/192.168.200.53:50000,192.168.200.53:50001,192.168.200.53:50002::0" : {
            "available" : 2,
            "created" : 3
        }
    },
    "replicaSets" : {
        "mablevi" : {
            "hosts" : [
                {
                    "addr" : "192.168.200.53:50000",
                    "ok" : true,
                    "ismaster" : true,
                    "hidden" : false,
                    "secondary" : false,
                    "pingTimeMillis" : 1
                },
                {
                    "addr" : "192.168.200.53:50001",
                    "ok" : true,
                    "ismaster" : false,
                    "hidden" : false,
                    "secondary" : true,
                    "pingTimeMillis" : 1
                },
                {
                    "addr" : "192.168.200.53:50002",
                    "ok" : true,
                    "ismaster" : false,
                    "hidden" : false,
                    "secondary" : true,
                    "pingTimeMillis" : 1
                }
            ]
        }
    },
    "createdByType" : {
        "master" : 22,
        "set" : 3,
        "sync" : 423
    },
    "totalAvailable" : 21,   #总的可用连接
    "totalCreated" : 448,   #总创建的连接
    "numDBClientConnection" : 40,
    "numAScopedConnection" : 1,
    "ok" : 1
}
View Code

相关文章:

  • 2022-01-14
  • 2021-07-17
  • 2021-05-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-17
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-03-06
  • 2021-11-03
  • 2021-04-26
  • 2021-09-20
  • 2022-12-23
相关资源
相似解决方案