mongodb MongoDB持续高CPU使用率

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34951683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:36:46  来源:igfitidea点击:

MongoDB constantly high cpu usage

mongodb

提问by alsdkjasdlkja

According to docker container statistics, my mongo database consumes constantly between 250 and 350% cpu. That's pretty impressive since it's a single core system :P

根据 docker 容器统计,我的 mongo 数据库持续消耗 250% 到 350% 的 CPU。这是非常令人印象深刻的,因为它是一个单核系统:P

The sad part is that this is my production instance and much more sad is that it has to live until the next prod-backup and thats 3.5 more hours to go.

可悲的是,这是我的生产实例,更可悲的是它必须持续到下一次生产备份,而且还有 3.5 个小时。

I tried to do a mongotop but it tells me 0ms stats for all shown collections. Can I do anything else to figure out what's going on?

我试图做一个 mongotop,但它告诉我所有显示的集合的 0ms 统计数据。我能做些什么来弄清楚发生了什么吗?

PS: The db is up for 9 weeks and didn't cause problems.

PS:数据库已经运行了 9 周并且没有引起问题。

回答by Daniel F

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).

有一个名为db.currentOp()的函数,它列出了当前正在运行的查询以及非常详细的信息,它还包括它们运行的​​持续时间 ( secs_running)。

You can then use the currentOp.opidwith db.killOp()to kill that query/operation.

然后您可以使用currentOp.opidwith db.killOp()终止该查询/操作。

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.

如果db.currentOp()没有返回任何结果,因为没有查询造成严重破坏,那么还有db.setProfilingLevel()它将通过将查询存储到“本地”数据库来启用分析。是来自“M102:MongoDB for DBA”课程的“教程” 。

Further information can also be found in this detailed article "Troubleshooting MongoDB 100% CPU load and slow queries"from Igor Khomenko.

还可以在Igor Khomenko 的这篇详细文章“对 MongoDB 100% CPU 负载和慢查询进行故障排除”中找到更多信息。

回答by Paulo Victor

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%.

您需要做的第一件事也是更重要的事情是检查您的查询类型,例如,在我的情况下,我遇到了同样的问题,当我检查我的日志tail -f /var/log/mongodb/mongod.log(您可以在 中配置此日志/etc/mongod.conf)时,我只看到了简单的查询,例如db.brands.find({"field":"value"}) 但我检查了我在“brands”集合中的索引,并且查询中的这个字段没有被索引(db.brands.getIndexes())。我所做的唯一一件事就是索引这个字段db.brands.ensureIndex({name:1},{unique:true})。当然要确保你的领域是独一无二的,因为在这个例子中我把它作为唯一的。之后,我的 CPU 从 100% 变为 20%。

So I am not saying that is your problem but could be, check your queries before doing some bigger things.

所以我并不是说这是你的问题,但可能是,在做一些更大的事情之前检查你的查询。