Shard:
用于存储实际的数据块,实际生产环境中一个shard server角色可由几台机器组个一个replica set承担,防止主机单点故障
Config Server:
mongod实例,存储了整个 ClusterMetadata,其中包括 chunk信息。
Query Routers:
前端路由,客户端由此接入,且让整个集群看上去像单一数据库,前端应用能够透明使用。
一、为数据库启用分片(test数据库) sh.enableSharding("test1") 二、使用hash分片某个集合(test数据库中的users集合,username是文档中的key) sh.shardCollection("test1.users", {username: "hashed"}) 插入1w数据 for (var i = 1; i <= 1000; i++) { db.users.insert({username: "name" + i}) } 三、惟一索引 db.recharge_order.ensureIndex({ "order_id": 1},{"unique":true}); 四、查看索引:db.recharge_order.getIndexes() 五、受权:use admin db.createUser({ user: 'root', pwd:'P8O6!FLLsXmf', roles:['root']})会自动同步到其余节点 db.createUser({ user: 'qa_select', pwd:'Zlongame1234', roles:['read']}) 六、查看分片中的db.users.stats() (1) 细致到collection的显示:sh.status() (2)仅显示分片: use config db.shards.find() (3)use admin db.runCommand({listshards: 1}) 列出全部的shard server db.printShardingStatus(); 七、查看mongo均衡器 use config db.locks.find(( { _id : "balancer" })).pretty() state 只有 0 关闭 1 正在获取状态 2 正在均衡 八、添加分片:sh.addShard("IP:Port") sh.addShard(":") #添加分片 九、删除分片 db.runCommand({"removeshard":"mab"}) #删除mab分片 db.adminCommand({"movePrimary":"db","to":"shard0001"}) #移动dba的主分片到shard0001. 十、查看各分片的状态:mongostat --discover 十一、从节点开启查询功能rs.slaveOk(); 十二、 db.repairDatabase() //执行这个命令后,Mongodb会把不须要的空间释放出来 1三、设置慢查询 db.getProfilingLevel() #查看状态 1 shardset1:PRIMARY> db.setProfilingLevel(1,100)#设置 { "was" : 1, "slowms" : 200, "ok" : 1 } shardset1:PRIMARY> db.getProfilingLevel()#查看级别 1 shardset1:PRIMARY> db.getProfilingStatus() # { "was" : 1, "slowms" : 100 } 查看db.system.profile.find().pretty()
① 分片信息:config.shards mongos> db.shards.find() ② 分片中全部数据库信息:config.databases mongos> db.databases.find() ③ 分片集合信息:config.collections mongos> db.collections.findOne() ④ mongos路由的信息:config.mongs 。能够查看全部mongos的状态 mongos> db.mongos.findOne() ⑤ 均衡器锁的信息:config.locks,记录全部集群范围的锁,可得知哪一个mongos是均衡器。 mongos> db.locks.findOne() ⑥ 记录全部块的信息:config.chunks,也能够经过sh.status()查看 mongos> db.chunks.find().pretty() ⑦ 记录全部的分片操做:config.changelog,拆分、迁移 db.changelog.find().pretty() ⑧ 分片标签:config.tags,sh.addShardTag mongos> db.tags.findOne() ⑨ 分片设置:config.settings,设置分片块的大小和开启关闭均衡器 mongos> db.settings.find() ⑩ 网络链接数: db.adminCommand({"connPoolStats":1}) mongos> db.adminCommand({"connPoolStats":1})
均衡器是使用块数量的多少,而不是数据的大小进行均衡 均衡器负责数据的迁移,会周期性的检查分片是否存在不均衡,若是存在则会进行块的迁移,不会影响到mongos正常的操做。均衡器进行均衡的条件是块数量的多少,而不是块大小,好比A片有几个较大的块,B片有不少较小的块,则均衡器会把B的分片迁移至A片。 ① 关闭/开启自动均衡器: mongos> sh.setBalancerState(false) #关闭均衡器 mongos> db.settings.find({"_id" : "balancer"}) { "_id" : "balancer", "stopped" : true} mongos> sh.setBalancerState(true) #开启均衡器 mongos> db.settings.find({"_id" : "balancer"}) { "_id" : "balancer", "stopped" : false} mongos> sh.stopBalancer() #关闭均衡器 Waiting for active hosts... Waiting for active host mongo2:30000 to recognize new settings... (ping : Mon Jul 27 2015 16:08:33 GMT+0800 (CST)) Waiting for the balancer lock... Waiting again for active hosts after balancer is off... mongos> db.settings.find({"_id" : "balancer"}) { "_id" : "balancer", "stopped" : true} mongos> sh.startBalancer() #开启均衡器 mongos> db.settings.find({"_id" : "balancer"}) { "_id" : "balancer", "stopped" : false} 查看均衡器的锁: mongos> use config mongos> db.locks.find( { _id : "balancer" } ).pretty() ② 指定均衡器的工做时间:activeWindow mongos> db.settings.update({"_id":"balancer"},{"$set":{"activeWindow":{"start":"07:00:00","stop":"03:00:00"}}},true) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) mongos> db.settings.findOne({"_id":"balancer"}) { "_id" : "balancer", "stopped" : false, "activeWindow" : { #凌晨7点到次日凌晨3点开启均衡器功能。 "start" : "07:00:00", "stop" : "03:00:00" } }
5、慢查询前端
一、经过mongo shell: ''' #查看状态:级别和时间 PRIMARY> db.getProfilingStatus() { "was" : 1, "slowms" : 200 } #查看级别 PRIMARY> db.getProfilingLevel() 1 #设置级别 PRIMARY> db.setProfilingLevel(2) { "was" : 1, "slowms" : 100, "ok" : 1 } #设置级别和时间 PRIMARY> db.setProfilingLevel(1,100) { "was" : 2, "slowms" : 100, "ok" : 1 } ''' 二、返回最近的10条记录 ''' db.system.profile.find().limit(10).sort({ ts : -1 }).pretty() #返回全部的操做,除command类型的 db.system.profile.find( { op: { $ne : ‘command‘ } }).pretty() #返回特定集合 db.system.profile.find( { ns : ‘mydb.test‘ } ).pretty() #返回大于5毫秒慢的操做 db.system.profile.find({ millis : { $gt : 5 } } ).pretty() ''' 三、经过配置文件
profile = 1
slowms = 300