mongodb会话相关

mongoDB会话相关操作

查看当前连接数和最大连接数

db.serverStatus().connections

maxTimeMS() 限制操作时间

db.location.find( { "town": { "$regex": "(Pine Lumber)",
"$options": 'i' } } ).maxTimeMS(30) ---限制30ms
db.runCommand( { distinct: "collection",
key: "city",
maxTimeMS: 45 } ) --限制45ms

返回报错的操作

db.getLastError() 
OR
db.getLastErrorObj() 

kill 会话

db.killOp(<opId>) 
db.currentOp(
{
"waitingForLock" : true,
$or: [
{ "op" : { "$in" : [ "insert", "update", "remove" ] } },
{ "query.findandmodify": { $exists: true } }
]
}
)


db.currentOp(
{
"waitingForLock" : true
}
)
db.currentOp(
{
"active" : true
}
)
use admin
db.aggregate( [
{ $currentOp : { allUsers: true } },
{$match:{host:"testdb:27107"}}
{$project:{_id:0,"host":1,"client":1,"active":1,"waitingForLock":1}}
] )

--显示当前的会话中,以客户端IP地址分组统计连接数

use admin
db.aggregate( [
{$currentOp : { allUsers: true ,idleConnections:true} },
{$project:{IP:{$split:["$client",":"]}}},
{$unwind:"$IP"},
{$match:{"IP":/^10\./}},
{$group:{_id:"$IP",count:{$sum:1}}}
]);

--查看当前活动会话正在执行的操作

use admin
db.aggregate( [
{$currentOp : { allUsers: true ,idleConnections:false} },
{$project:{opid:1,client:1,op:1,ns:1,microsecs_running:1,command:1}},
{$match:{client:{$exists:true}}},
{$sort:{microsecs_running:-1}}
]).pretty();

--查看所有活动会话,以执行时间排序

db.aggregate( [
{$currentOp : { allUsers: true ,idleConnections:false} },
{$project:{opid:1,client:1,op:1,ns:1,microsecs_running:1}},
{$match:{client:{$exists: true}}},
{$sort:{microsecs_running:-1}}
]);

---查看本机的会话,以执行时间排序

db.aggregate( [
{$currentOp : { allUsers: true ,idleConnections:false} },
{$project:{opid:1,client:1,op:1,ns:1,microsecs_running:1}},
{$match:{client:/^127.0.0.1/}},
{$sort:{microsecs_running:-1}}
]);

---根据连接编号查询会话信息

db.aggregate( [
{$currentOp : { allUsers: true ,idleConnections:true} },
{$match:{connectionId:6459}},
{$sort:{microsecs_running:-1}}
]);
db.aggregate( [
{$currentOp : { allUsers: true ,idleConnections:true} },
{$match:{opid:297834751}},
{$sort:{microsecs_running:-1}}
]);

---检查是否有会话在等待锁

db.aggregate( [
   {$currentOp : { allUsers: true ,idleConnections:false} },
{$project:{opid:1,client:1,op:1,ns:1,microsecs_running:1,waitingForLock:1}},
   {$match:{"waitingForLock":true}},
   {$count:"wait for locks"}
   ]);

如果存在大量锁等待,则首先检查是否存在表、库、全局锁

db.aggregate( [
   {$currentOp : { allUsers: true ,idleConnections:false} },  {$match:{$or:[{"locks.Global":"W"},{"locks.Database":"W"},{"locks.Collection":"W"}]}}
   ])

如果发现有会话持有表级、库级或者全局锁,则可根据其opid,执行db.killOp(opid)释放锁资源。如果未发现表级以上的锁,则检查不处于等待状态的会话在做些什么操作,再进行具体处理

db.aggregate( [
{$currentOp : { allUsers: true ,idleConnections:false} },
{$project:{opid:1,client:1,op:1,ns:1,microsecs_running:1,waitingForLock:1}},
{$match:{$and:[{"waitingForLock":false},{client:{$exists:true}}]}},
{$sort:{ microsecs_running:-1}}
]);

--查看执行时间超过1S的非后台会话

use admin
db.aggregate( [
{$currentOp : { allUsers: true ,idleConnections:false} },
{$project:{opid:1,client:1,op:1,ns:1,microsecs_running:1,command:1}},
{$match: {$and:[{microsecs_running : { "$gt" : 1000000 }},{client:{$exists: true}}]}},
{$sort:{microsecs_running:-1}}
]);

---查看是否有会话正在创建索引

use admin
db.aggregate( [
   {$currentOp : { allUsers: true ,idleConnections:false} },
   {$project:{opid:1,client:1,op:1,command:1,ns:1,microsecs_running:1}},
   {$match: { op: "command", "command.createIndexes": { $exists: true }  }},
   {$sort:{microsecs_running:-1}}
   ])
db.currentOp(
{
"active" : true,
"numYields" : 0,
"waitingForLock" : false
}
)
db.currentOp(true).inprog.forEach(
function(opDoc){
if (opDoc.client)
printjson(opDoc.client)
}
)
db.currentOp(true).inprog.forEach(
function(opDoc){
if(opDoc.active && opDoc.op=='query')
printjson(opDoc)
}
)
posted @ 2024-12-27 11:31  数据库小白(专注)  阅读(6)  评论(0编辑  收藏  举报