Mongo分片基础命令

一、三节点作用
Shard:
用于存储实际的数据块,实际生产环境中一个shard server角色可由几台机器组个一个replica set承担,防止主机单点故障
Config Server:
mongod实例,存储了整个 ClusterMetadata,其中包括 chunk信息。
Query Routers:
前端路由,客户端由此接入,且让整个集群看上去像单一数据库,前端应用可以透明使用。

 

 
二、基础命令
1、为数据库启用分片(test数据库)
sh.enableSharding("test1")
2、使用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}) }
3、唯一索引 db.recharge_order.ensureIndex({ "order_id": 1},{"unique":true});
4、查看索引:db.recharge_order.getIndexes()
5、授权:use admin
db.createUser({ user: 'root', pwd:'P8O6!FLLsXmf', roles:['root']})会自动同步到其他节点
db.createUser({ user: 'qa_select', pwd:'Zlongame1234', roles:['read']})
6、查看分片中的db.users.stats() 
(1) 细致到collection的显示:sh.status()
(2)仅显示分片:
use config
db.shards.find()
(3)use admin
db.runCommand({listshards: 1})
列出所有的shard server
db.printShardingStatus();

7、查看mongo均衡器
use config
 db.locks.find(( { _id : "balancer" })).pretty()
state 只有 0 关闭 1 正在获取状态 2 正在均衡 
8、添加分片:sh.addShard("IP:Port") 
 sh.addShard(":") #添加分片
9、删除分片
db.runCommand({"removeshard":"mab"})   #删除mab分片
db.adminCommand({"movePrimary":"db","to":"shard0001"})  #移动dba的主分片到shard0001.
10、查看各分片的状态:mongostat --discover  
11、从节点开启查询功能rs.slaveOk();
12、 db.repairDatabase()  //执行这个命令后,Mongodb会把不需要的空间释放出来
13、设置慢查询
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库的介绍。
① 分片信息: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"
    }
}

 五、慢查询

1、通过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 }

'''

2、返回最近的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()
'''

3、通过配置文件
profile = 1
slowms = 300

  

一、三节点作用
Shard:
用于存储实际的数据块,实际生产环境中一个shard server角色可由几台机器组个一个replica set承担,防止主机单点故障
Config Server:
mongod实例,存储了整个 ClusterMetadata,其中包括 chunk信息。
Query Routers:
前端路由,客户端由此接入,且让整个集群看上去像单一数据库,前端应用可以透明使用。
 
 
 
 
二、基础命令
1、为数据库启用分片(test数据库)
sh.enableSharding("test1")
2、使用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}) }
3、唯一索引 db.recharge_order.ensureIndex({ "order_id": 1},{"unique":true});
4、查看索引:db.recharge_order.getIndexes()
5、授权:use admin
db.createUser({ user: 'root', pwd:'P8O6!FLLsXmf', roles:['root']})会自动同步到其他节点
db.createUser({ user: 'qa_select', pwd:'Zlongame1234', roles:['read']})
6、查看分片中的db.users.stats() 
(1) 细致到collection的显示:sh.status()
(2)仅显示分片:
use config
db.shards.find()
(3)use admin
db.runCommand({listshards: 1})
列出所有的shard server
db.printShardingStatus();
 
7、查看mongo均衡器
use config
db.locks.find(( { _id : "balancer" })).pretty()
state 只有 0 关闭 1 正在获取状态 2 正在均衡
8、添加分片:sh.addShard("IP:Port")
sh.addShard(":") #添加分片
9、删除分片
db.runCommand({"removeshard":"mab"}) #删除mab分片
db.adminCommand({"movePrimary":"db","to":"shard0001"}) #移动dba的主分片到shard0001.
10、查看各分片的状态:mongostat --discover
11、从节点开启查询功能rs.slaveOk();
12、 db.repairDatabase() //执行这个命令后,Mongodb会把不需要的空间释放出来
13、设置慢查询
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库的介绍。
① 分片信息: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"
}
}
posted @ 2017-11-22 11:46  Bourne.D  阅读(2335)  评论(0编辑  收藏  举报