Mongodb监控命令


一、监控工具

1、mongostat工具

默认为显示每秒的统计信息

# mongostat -uroot -ppassword --authenticationDatabase admin -h192.168.x.xx  --rowcount 10 1
connected to: 192.168.x.xx
insert  query update delete getmore command flushes mapped  vsize    res faults  locked db idx miss %     qr|qw   ar|aw  netIn netOut  conn set repl       time 
    *0     *0     *0     *0       0     3|0       0  1.19g  3.07g    85m      0  test:0.0%          0       0|0     0|0   178b     4k     6 rs0  SEC   21:02:36 
    *0     *0     *0     *0       0     3|0       0  1.19g  3.07g    85m      0  test:0.0%          0       0|0     0|0   342b     4k     6 rs0  SEC   21:02:37 
    *0     *0     *0     *0       0     1|0       0  1.19g  3.07g    85m      0  test:0.0%          0       0|0     0|0    62b     3k     6 rs0  SEC   21:02:38 
    *0     *0     *0     *0       0     3|0       1  1.19g  3.07g    85m      0  test:0.0%          0       0|0     0|0   342b     4k     6 rs0  SEC   21:02:39 
    *0      6     *0     *0       0     1|0       0  1.19g  3.07g    85m      0  test:0.0%          0       0|0     0|0    62b     3k     6 rs0  SEC   21:02:40 
    *0     *0     *0     *0       0     3|0       0  1.19g  3.07g    85m      0  test:0.0%          0       0|0     0|0   342b     4k     6 rs0  SEC   21:02:41 
    *0     *0     *0     *0       0     1|0       0  1.19g  3.07g    85m      0  test:0.0%          0       0|0     0|0    62b     3k     6 rs0  SEC   21:02:42 
    *0     *0     *0     *0       0     3|0       0  1.19g  3.07g    85m      0  test:0.0%          0       0|0     0|0   342b     4k     6 rs0  SEC   21:02:43 
    *0     *0     *0     *0       0     1|0       0  1.19g  3.07g    85m      0  test:0.0%          0       0|0     0|0    62b     3k     6 rs0  SEC   21:02:44 
    *0     *0     *0     *0       0     3|0       0  1.19g  3.07g    85m      0   db4:0.0%          0       0|0     0|0   342b     4k     6 rs0  SEC   21:02:45

可以添加 --discover显示shard集群(连接mongos)和副本集的所有成员的统计信息。


输出含义,参考:http://docs.mongodb.org/v2.6/reference/program/mongostat/


2、mongotop工具

显示每个数据库的集合的读写时间,或每个数据库的读写锁时间(--locks),默认为1秒。

# mongotop -uroot -ppassword --authenticationDatabase admin -h192.168.x.xx  5
connected to: 192.168.x.xx

                            ns       total        read       write		2015-06-10T13:23:51
            db4.system.indexes         0ms         0ms         0ms
                      db4.$cmd         0ms         0ms         0ms
         db2.system.namespaces         0ms         0ms         0ms
            db2.system.indexes         0ms         0ms         0ms
                      db2.post         0ms         0ms         0ms
                    db1.userX2         0ms         0ms         0ms
                    db1.userX1         0ms         0ms         0ms

                            ns       total        read       write		2015-06-10T13:23:56
            db4.system.indexes         0ms         0ms         0ms
                      db4.$cmd         0ms         0ms         0ms
         db2.system.namespaces         0ms         0ms         0ms
            db2.system.indexes         0ms         0ms         0ms
                      db2.post         0ms         0ms         0ms
                    db1.userX2         0ms         0ms         0ms
                    db1.userX1         0ms         0ms         0ms

					
# mongotop -uroot -ppassword --authenticationDatabase admin -h192.168.x.xx  5 --locks
connected to: 192.168.x.xx

                            db       total        read       write		2015-06-10T13:24:42
                             .         1ms         1ms         0ms
                          test         0ms         0ms         0ms
                         local         0ms         0ms         0ms
                           db4         0ms         0ms         0ms
                           db2         0ms         0ms         0ms
                           db1         0ms         0ms         0ms
                         admin         0ms         0ms         0ms

                            db       total        read       write		2015-06-10T13:24:47
                          test         0ms         0ms         0ms
                         local         0ms         0ms         0ms
                           db4         0ms         0ms         0ms
                           db2         0ms         0ms         0ms
                           db1         0ms         0ms         0ms
                         admin         0ms         0ms         0ms
                             .         0ms         0ms         0ms	

输出含义,参考:http://docs.mongodb.org/v2.6/reference/program/mongotop/


3、db.serverStatus()

参考:http://docs.mongodb.org/v2.6/reference/command/serverStatus/


4、db.stats() 

> db.stats()
{
	"db" : "db4",
	"collections" : 3,
	"objects" : 13,
	"avgObjSize" : 87.38461538461539,
	"dataSize" : 1136,
	"storageSize" : 24576,
	"numExtents" : 3,
	"indexes" : 3,
	"indexSize" : 24528,
	"fileSize" : 16777216,
	"nsSizeMB" : 16,
	"dataFileVersion" : {
		"major" : 4,
		"minor" : 5
	},
	"extentFreeList" : {
		"num" : 0,
		"totalSize" : 0
	},
	"ok" : 1
}

参考:http://docs.mongodb.org/v2.6/reference/command/dbStats/#dbcmd.dbStats


5、db.collect_name.stats()

{
	"ns" : "db4.userInfo",
	"count" : 5,
	"size" : 560,
	"avgObjSize" : 112,
	"storageSize" : 8192,
	"numExtents" : 1,
	"nindexes" : 3,
	"lastExtentSize" : 8192,
	"paddingFactor" : 1,
	"systemFlags" : 1,
	"userFlags" : 1,
	"totalIndexSize" : 24528,
	"indexSizes" : {
		"_id_" : 8176,
		"name_hashed" : 8176,
		"age_1" : 8176
	},
	"ok" : 1
}

参考:http://docs.mongodb.org/v2.6/reference/command/collStats/#dbcmd.collStats


6、rs.status()

查询副本集信息

> rs.status()
{
	"set" : "rs0",
	"date" : ISODate("2015-06-11T04:02:55Z"),
	"myState" : 2,
	"syncingTo" : "192.168.x.xx:27018",
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.x.xx:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 84674,
			"optime" : Timestamp(1435989328, 1),
			"optimeDate" : ISODate("2015-07-04T05:55:28Z"),
			"lastHeartbeat" : ISODate("2015-06-11T04:02:54Z"),
			"lastHeartbeatRecv" : ISODate("2015-06-11T04:02:54Z"),
			"pingMs" : 8,
			"syncingTo" : "192.168.x.xx:27018"
		},
		{
			"_id" : 3,
			"name" : "192.168.x.xx:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 84676,
			"optime" : Timestamp(1435989328, 1),
			"optimeDate" : ISODate("2015-07-04T05:55:28Z"),
			"self" : true
		},
		{
			"_id" : 4,
			"name" : "192.168.x.xx:27018",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 84674,
			"optime" : Timestamp(1435989328, 1),
			"optimeDate" : ISODate("2015-07-04T05:55:28Z"),
			"lastHeartbeat" : ISODate("2015-06-11T04:02:54Z"),
			"lastHeartbeatRecv" : ISODate("2015-06-11T04:02:55Z"),
			"pingMs" : 2,
			"electionTime" : Timestamp(1435803779, 1),
			"electionDate" : ISODate("2015-07-02T02:22:59Z")
		}
	],
	"ok" : 1
}

参考:http://docs.mongodb.org/v2.6/reference/command/replSetGetStatus


二、诊断性能问题

1、Locks

分析 db.serverStatus() 输出的 globalLock 部分:

"globalLock" : {
		"totalTime" : NumberLong("240481182000"),
		"lockTime" : NumberLong(11670085),
		"currentQueue" : {
			"total" : 0,
			"readers" : 0,
			"writers" : 0
		},
		"activeClients" : {
			"total" : 0,
			"readers" : 0,
			"writers" : 0
		}
	}

If globalLock.currentQueue.total is consistently high, then there is a chance that a large number of requests are waiting for a lock.

This indicates a possible concurrency issue that may be affecting performance.


globalLock.totalTime : The value of totalTime represents the time, in microseconds, since the database last started and creation of the globalLock. 

   This is roughly equivalent to total server uptime.


lockTime/totalTime很大,表示锁花费的时间长,导致性能下降。


2、内存

分析 db.serverStatus() 输出的 mem 部分:

"mem" : {
		"bits" : 64,
		"resident" : 39,
		"virtual" : 2993,
		"supported" : true,
		"mapped" : 1119,
		"mappedWithJournal" : 2238
	}

单位为M.

resident 表示mongodb进程占用的内存,如果是专用服务器,随着mongodb使用内存的增加,这个参数的值会渐渐接近于系统内存。

如果这个参数的值,超过系统内存并且大量的数据存放在磁盘而不在内存中,我们需要增加硬件能力。


mapped 文件映射的内存量,这个值大致与mongodb数据库的大小相同。

If this value is greater than the amount of system memory, some operations will require disk access page faults to read data from virtual memory and negatively affect performance.


3、Page Faults

分析 db.serverStatus() 输出的 extra_info 部分:

"extra_info" : {
		"note" : "fields vary by platform",
		"heap_usage_bytes" : 62941696,
		"page_faults" : 40
	}

通常对大量数据的读取会产生大量的page faults 会使mongodb 性能下降。解决办法是优化sql或扩充内存。


4、连接数

分析 db.serverStatus() 输出的 connections 部分:

"connections" : {
		"current" : 12,
		"available" : 1626,
		"totalCreated" : NumberLong(8014)
	}
	
"globalLock" : {
		"totalTime" : NumberLong("240481182000"),
		"lockTime" : NumberLong(11670085),
		"currentQueue" : {
			"total" : 0,
			"readers" : 0,
			"writers" : 0
		},
		"activeClients" : {
			"total" : 0,
			"readers" : 0,
			"writers" : 0
		}
	}	

说明 :

globalLock.activeClients contains a counter of the total number of clients with active operations in progress or queued.


connections is a container for the following two fields:

  current the total number of current clients that connect to the database instance.

  available the total number of unused collections available for new clients.