通过对数据库的性能监控,能够更好的了解数据库的工作状态,从而进而优化。
介绍几个性能监控的工具:
mongosniff
此工具可以从底层监控到底有哪些命令发送给MongoDB取执行。
./mongosniff --source NET lo
它是实时动态监视的,需要打开另一个客户端进行命令操作。可以将这些数据输出到一个日志文件中,那么就可以保留下所有数据库操作的历史记录,对于后期的性能分析和安全审计等工作键是一个巨大的贡献。
mongostat
此工具可以快速的查看某组运行中的MongoDB实例的统计信息,也可以需要在打开一个客户端进行命令操作:
这里注意一下两个命令的区别:
mongosniff监控的是客户端与数据库服务的通信
mongostat监控的是增、删、改、查等操作。
现在我们就来试一试。
第一个命令,哎呀!我的bin里面没有mongosniff,貌似需要build和第三方通信库的。
方法如下:http://docs.mongodb.org/manual/reference/program/mongosniff/
这是官方文档,文章后面提到了build方法。
再见!!
我们直接开始mongostat。
打开两个bash。
第一个运行mongostat。
[neil@neilhost ~]$ sudo /usr/local/mongodb/bin/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 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 1 20:31:49 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 1 20:31:50 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 1 20:31:51 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 1 20:31:52 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 1 20:31:53 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 1 20:31:54 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 1 20:31:55 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 1 20:31:56 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 1 20:31:57 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 1 20:31:58 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 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 1 20:31:59 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 1 20:32:00 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 1 20:32:01 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 1 20:32:02 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 1 20:32:03 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 1 20:32:04 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 dt1:0.0% 0 0|0 0|0 62b 4k 1 20:32:05 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 1 20:32:06 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 1 20:32:07 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 1 20:32:08 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 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 1 20:32:09 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 1 20:32:10 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 1 20:32:11 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 1 20:32:12 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 1 20:32:13 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 1 20:32:14 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 1 20:32:15 *0 *0 *0 *0 0 5|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 361b 4k 2 20:32:16 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 2 20:32:17 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 .:0.0% 0 0|0 0|0 62b 4k 2 20:32:18 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 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 2 20:32:19 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 2 20:32:20 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 2 20:32:21 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 2 20:32:22 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 2 20:32:23 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 2 20:32:24 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 2 20:32:25 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 2 20:32:26 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 2 20:32:27 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 2 20:32:28 insert query update delete getmore command flushes mapped vsize res faults locked db idx miss % qr|qw ar|aw netIn netOut conn time *0 9 *0 *0 0 2|0 1 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 140b 4k 2 20:32:29 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 2 20:32:30 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 2 20:32:31 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 2 20:32:32 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 2 20:32:33 *0 1 *0 *0 0 2|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 180b 5k 2 20:32:34 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 2 20:32:35 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 2 20:32:36 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 2 20:32:37 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 2 20:32:38 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 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 2 20:32:39 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 local:0.0% 0 0|0 0|0 62b 4k 2 20:32:40 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 2 20:32:48 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 2 20:32:49 *0 *0 *0 *0 0 1|0 0 640m 1.53g 149m 0 test:0.0% 0 0|0 0|0 62b 4k 2 20:32:50 ^C[neil@neilhost ~]$
另一边的客户端进行操作:
[neil@neilhost ~]$ sudo /usr/local/mongodb/bin/mongo [sudo] password for neil: MongoDB shell version: 2.6.8 connecting to: test > use dt6 switched to db dt6 > db.c1.find() { "_id" : ObjectId("550aac29f237dcbf6bee2cb2"), "name" : "s0", "age" : 20, "parents" : { "father" : "f0", "mother" : "m0" } } { "_id" : ObjectId("550aac2af237dcbf6bee2cb3"), "name" : "s1", "age" : 21, "parents" : { "father" : "f1", "mother" : "m1" } } { "_id" : ObjectId("550aac2af237dcbf6bee2cb4"), "name" : "s2", "age" : 22, "parents" : { "father" : "f2", "mother" : "m2" } } { "_id" : ObjectId("550aac2af237dcbf6bee2cb5"), "name" : "s3", "age" : 23, "parents" : { "father" : "f3", "mother" : "m3" } } { "_id" : ObjectId("550aac2af237dcbf6bee2cb6"), "name" : "s4", "age" : 24, "parents" : { "father" : "f4", "mother" : "m4" } } { "_id" : ObjectId("550aac2af237dcbf6bee2cb7"), "name" : "s5", "age" : 25, "parents" : { "father" : "f5", "mother" : "m5" } } { "_id" : ObjectId("550aac2af237dcbf6bee2cb8"), "name" : "s6", "age" : 26, "parents" : { "father" : "f6", "mother" : "m6" } } { "_id" : ObjectId("550aac2af237dcbf6bee2cb9"), "name" : "s7", "age" : 27, "parents" : { "father" : "f7", "mother" : "m7" } } { "_id" : ObjectId("550aac2af237dcbf6bee2cba"), "name" : "s8", "age" : 28, "parents" : { "father" : "f8", "mother" : "m8" } } { "_id" : ObjectId("550aac2af237dcbf6bee2cbb"), "name" : "s9", "age" : 29, "parents" : { "father" : "f9", "mother" : "m9" } } >