1.借助工具 mongostat 分析mongodb运行状况
C:UsersAdministrator>mongostat --help //查看帮助 View live MongoDB performance statistics. usage: mongostat [options] [sleep time] sleep time: time to wait (in seconds) between calls Options: --help produce help message -v [ --verbose ] be more verbose (include multiple times for more verbosity e.g. -vvvvv) --quiet silence all non error diagnostic messages --version print the program's version and exit -h [ --host ] arg mongo host to connect to ( <set name>/s1,s2 for sets) --port arg server port. Can also use --host hostname:port --ipv6 enable IPv6 support (disabled by default) -u [ --username ] arg username -p [ --password ] arg password --authenticationDatabase arg user source (defaults to dbname) --authenticationMechanism arg (=MONGODB-CR) authentication mechanism --gssapiServiceName arg (=mongodb) Service name to use when authenticating using GSSAPI/Kerberos --gssapiHostName arg Remote host name to use for purpose of GSSAPI/Kerberos authentication --noheaders don't output column names -n [ --rowcount ] arg (=0) number of stats lines to print (0 for indefinite) --http use http instead of raw db connection --discover discover nodes and display stats for all --all all optional fields Fields inserts - # of inserts per second (* means replicated op) query - # of queries per second update - # of updates per second delete - # of deletes per second getmore - # of get mores (cursor batch) per second command - # of commands per second, on a slave its local|replicated flushes - # of fsync flushes per second mapped - amount of data mmaped (total data size) megabytes vsize - virtual size of process in megabytes res - resident size of process in megabytes non-mapped - amount virtual memeory less mapped memory (only with --all) faults - # of pages faults per sec locked - name of and percent time for most locked database idx miss - percent of btree page misses (sampled) qr|qw - queue lengths for clients waiting (read|write) ar|aw - active clients (read|write) netIn - network traffic in - bytes netOut - network traffic out - bytes conn - number of open connections set - replica set name repl - replication type PRI - primary (master) SEC - secondary REC - recovering UNK - unknown SLV - slave b RTR - mongos process ("router") C:UsersAdministrator>mongostat connected to: 127.0.0.1 insert query update delete getmore command flushes mapped vsize res faults locked db idx miss % qr|qw ar|aw netIn netOut conn time *0 *0 *0 *0 0 1|0 0 160m 446m 40m 0 test:0.0% 0 0|0 0|0 62b 3k 2 13:54:19
2.mongodb慢操作日志 profile
> db.getProfilingLevel() //查看慢日志记录级别 0=关闭 1=所有超过slowms的操作 2=记录所有查找 0 > db.setProfilingLevel(2){ "was" : 0, "slowms" : 100, "ok" : 1 } //设置日志记录级别2 > show tablessystem.profile //这个就是日志集合 > db.system.profile.find().sort({$natural:-1}).limit(1) //自然排序 倒序查看一条日志记录 { "op": "query", "ns": "test.system.namespaces", "query": { }, "ntoreturn": 0, "ntoskip": 0, "nscanned": 11, "nscannedObjects": 11, "keyUpdates": 0, "numYield": 0, "lockStats": { "timeLockedMicros": { "r": NumberLong(177), "w": NumberLong(0) }, "timeAcquiringMicros": { "r": NumberLong(4), "w": NumberLong(4) } }, "nreturned": 11, "responseLength": 449, "millis": 0, "execStats": { "type": "COLLSCAN", "works": 13, "yields": 0, "unyields": 0, "invalidates": 0, "advanced": 11, "needTime": 1, "needFetch": 0, "isEOF": 1, "docsTested": 11, "children": [ ] }, "ts": ISODate("2015-09-16T06:14:39.604Z"), "client": "127.0.0.1", "allUsers": [ ], "user": "" }
3.explain 方法显示查询语句的详细信息
> db.system.local.find().explain() { "cursor" : "BasicCursor", "isMultiKey" : false, "n" : 0, "nscannedObjects" : 0, "nscanned" : 0, "nscannedObjectsAllPlans" : 0, "nscannedAllPlans" : 0, "scanAndOrder" : false, "indexOnly" : false, "nYields" : 0, "nChunkSkips" : 0, "millis" : 0, "server" : "buexplain:27017" }