Quantcast
Channel: CodeSection,代码区,数据库(综合) - CodeSec
Viewing all articles
Browse latest Browse all 6262

MongoDB CPU飙升至99%解决

$
0
0

公司业务调整,把一部分数据由Redis转至MongoDB,业务在测试环境正常,生产环境上线后发现压力一上来MongoDB的服务直接把CPU占满了,和开发的同学分析了一下也参考了一下百度上类似的问题,最后定位原因:未使用索引。

解决方法很简单:对应的关键字段创建索引即可。

参考命令如下:

>use Data; #进入数据库Data,注意区分大小写;

>db.stats(); #查看数据库的状态;

>db.getCollectionNames(); #查看所有表名;

>db.dynamic.find().limit(5); #查看dynamic表的前5条数据;

>db.dynamic.find().count(); #统计dynamic表总共有多少数据;

>db.dynamic.getIndexes(); #查询表dynamic的索引

#新建索引:对dynamic的score字段以backgroud方式建立索引

>db.dynamic.ensureIndex({score:1} , {backgroud:true});

#状态查看:20秒,每秒更新一次

mongostat -h 80.81.2.3 -p 27017 --rowcount 20 1

开始创建索引的字段没找准,效果不明显,后来对关键字段创建索引后CPU使用率断崖式下跌。

更多 MongoDB 相关教程见以下内容 :

CentOS 编译安装 MongoDB与mongoDB的php扩展 http://www.linuxidc.com/Linux/2012-02/53833.htm

CentOS 6 使用 yum 安装MongoDB及服务器端配置 http://www.linuxidc.com/Linux/2012-08/68196.htm

Ubuntu 13.04下安装MongoDB2.4.3 http://www.linuxidc.com/Linux/2013-05/84227.htm

MongoDB入门必读(概念与实战并重) http://www.linuxidc.com/Linux/2013-07/87105.htm

Ubunu 14.04下MongoDB的安装指南 http://www.linuxidc.com/Linux/2014-08/105364.htm

《MongoDB 权威指南》(MongoDB: The Definitive Guide)英文文字版[PDF] http://www.linuxidc.com/Linux/2012-07/66735.htm

Nagios监控MongoDB分片集群服务实战 http://www.linuxidc.com/Linux/2014-10/107826.htm

基于CentOS 6.5操作系统搭建MongoDB服务http://www.lin uxidc.com/Linux/2014-11/108900.htm

MongoDB 的详细介绍 : 请点这里

MongoDB 的下载地址 : 请点这里

本文永久更新链接地址 : http://www.linuxidc.com/Linux/2017-03/141626.htm


Viewing all articles
Browse latest Browse all 6262

Trending Articles