MongoDB constantly high cpu usage
There is a function called db.currentOp() which lists the currently running queries with very detailed information, it also includes the duration they have been running (secs_running
).
You can then use the currentOp.opid
with db.killOp() to kill that query/operation.
If db.currentOp()
doesn't return any results, because there is no query which went havoc, then there's also db.setProfilingLevel() which will enable profiling by storing queries into the "local" database. Here's a "Tutorial" which is from the "M102: MongoDB for DBAs" Course.
Further information can also be found in this detailed article "Troubleshooting MongoDB 100% CPU load and slow queries" from Igor Khomenko.
The first and more important thing that you need to do is check your kind of queries, for example, in my case I had the same problem and when I checked my logs tail -f /var/log/mongodb/mongod.log
(You can configure this logs in /etc/mongod.conf
) I only saw the simple queries like db.brands.find({"field":"value"}) but I checked my indexes in "brands" collections and this field on queries was not indexed (db.brands.getIndexes()
). The only thing that I did is index this field db.brands.ensureIndex({name:1},{unique:true})
. Of course make sure if your field is unique because in this example I put as a unique. After that my CPU changed from 100% to 20%.
So I am not saying that is your problem but could be, check your queries before doing some bigger things.