[TOC]
Slow query
// 开启2000毫秒以上的慢查询记录db.setProfilingLevel(1,2000)// 查看最近5条慢查询show profile// 按照慢查询执行时间倒序查询db.system.profile.find().sort({‘millis‘:-1}).pretty// 查看查询计划db.Sync_Log.find({"$and":[{"mbid" :{$eq 7542163}},{‘t‘:{$gte:2333}}]}).explain()// 关闭慢查询记录db.setProfilingLevel(0)
Task management stops queries that exceed a certain time
db.currentOp().inprog.forEach(function(item){if (item.secs_running > 1000 ) db.killOp(item.opid);})
Stop a query on a table
db.currentOp().inprog.forEach(function(item){if (item.ns == "dbA.tbA")db.killOp(item.opid)})
Find some kind of task
db.currentOp().inprog.forEach(function(item){if (item.op=="query"){print(item.opid,item.op);}})
Another way of thinking about the task of viewing
db.currentOp( {$and :[ {ns:{$ne:‘local.oplog.rs‘}}, {ns:{$ne:‘local.replset.minvalid‘}}, {ns:{$ne:‘admin.$cmd‘}}, {ns:{$ne:‘‘}}] })
Replica set management modifies host names
cfg = rs.conf()cfg.members[0].host = "xxxhost: 20000"cfg.members[1].host = "yyyhost: 20001"cfg.members[2].host = "zzzhost: 20002"rs.reconfig(cfg)
Shard Management Modify Primary Shard
When a MongoDB shard cluster creates a database, the system picks a node with the largest number of available empty nodes as its default node.
db.runCommand({"movePrimary": "test", "to": "shard0000"})
Manually migrating a collection's data blocks
Purpose: Pre-migrating data when a node is required to be offline
sh.moveChunk("dbA.collectionB", { collC: "53187" }, "shard0019")
If an error occurs
"MongoDB" Management Command collection