How to kill a process in
If you are unfamiliar with your current database schema and indexes, or run a heavy query by mistake on production database things go nasty! To find the query and kill it run mongo client as below and run
to run
#mongodb #mongo #killOp #currentOp #query
mongo
shell?If you are unfamiliar with your current database schema and indexes, or run a heavy query by mistake on production database things go nasty! To find the query and kill it run mongo client as below and run
currentOp
command:> mongo
> db.currentOp()
currentOp()
database method will display the ongoing queries in json format, find the query which is related to you and get its opid
. Now you needto run
killOp()
database method to kill that process as below:db.killOp(229)
229
is the operation id we have got from the first command.NOTE:
terminate running operations with extreme caution. Only use db.killOp() to terminate operations initiated by clients and do not terminate internal database operations.#mongodb #mongo #killOp #currentOp #query
Today we encountered slowness on MongoDB that caused all the infrastructure to get affected. The problem was that slowness on some specific mongo queries caused all the other queries to wait. YES we use index and YES we used explained on those queries and saw that those queries are using index. Now to mitigate the issue we had to kill very slow
The function below kills slow queries:
We need to save this query in mongo itself and run it directly. To save the above function in mongoDB use
I will explain the above function parts in a different post. Now you need to load server scripts and then run it:
The above query kills queries that has taken longer than 20s.
#mongodb #mongo #function #kill_slow_queries #currentOp
find
queries until we fix the issue.The function below kills slow queries:
function (sec) {db.currentOp()['inprog'].forEach(function (query) { if (query.op !== 'query') { return; } if (query.secs_running < sec) { return; } print(['Killing query:', query.opid, 'which was running:', query.secs_running, 'sec.'].join(' ')); db.killOp(query.opid); })}
We need to save this query in mongo itself and run it directly. To save the above function in mongoDB use
db.system.js.save
:db.system.js.save({_id:"kill_slow_queries", value:function (sec) {db.currentOp()['inprog'].forEach(function (query) { if (query.op !== 'query') { return; } if (query.secs_running < sec) { return; } print(['Killing query:', query.opid, 'which was running:', query.secs_running, 'sec.'].join(' ')); db.killOp(query.opid); })} })
I will explain the above function parts in a different post. Now you need to load server scripts and then run it:
db.loadServerScripts()
kill_slow_queries(20)
The above query kills queries that has taken longer than 20s.
NOTE:
you can create a shell script and run it periodically using crontab until you fix the slowness on your server.#mongodb #mongo #function #kill_slow_queries #currentOp
On previous posts we explained about query slowness. Here we try to explain different parts of the function.
can use command like
The part we are interested in is
This is all we've done in previous posts, to kill slow queries in mongoDB.
#mongodb #mongo #currentOp #killOp #opid
db.currentOp
: in progress operations in mongoDB is displayed by this command. The response of the command is in json format, so youcan use command like
db.currentOp()['inprog']
. The response has many useful informations like lock status
, numYields
and so on.The part we are interested in is
opid
part. opid
is the pid number of the query operation. op
section of each operation shows the type of the query. It can be an internal database command, insert command and or query. secs_running
of the operation is the part that we can check whether a query has taken a long time or not. It is in second.db.killOp
: killing an operation is just as simple as giving the opid
number to killOp
as below:db.killOp(6123213)
This is all we've done in previous posts, to kill slow queries in mongoDB.
#mongodb #mongo #currentOp #killOp #opid