MongoDB中提供了mongostat 和 mongotop 两个命令来监控MongoDB的运行情况。
1.mongostat 命令
mongostat是mongodb自带的状态检测工具,在命令行下使用。它会间隔固定时间获取mongodb的当前运行状态,并输出。如果你发现数据库突然变慢或者有其他问题的话,你第一手的操作就考虑采用mongostat来查看mongo的状态。
[root@10-0-42-211 bin]# ./mongostat -h 10.0.42.211:27017 --username=admin --password=123456 --authenticationDatabase admin
2022-11-26T14:46:51.679+0800 WARNING: On some systems, a password provided directly using --password may be visible to system status programs such as `ps` that may be invoked by other users. Consider omitting the password to provide it via stdin, or using the --config option to specify a configuration file with the password.
insert query update delete getmore command dirty used flushes vsize res qrw arw net_in net_out conn set repl time
*0 *0 *0 *0 0 6|0 0.0% 0.1% 0 2.01G 139M 0|0 0|0 1.43k 60.6k 22 rs SLV Nov 26 14:46:52.737
*0 *0 *0 *0 0 2|0 0.0% 0.1% 0 2.01G 139M 0|0 0|0 498b 60.5k 22 rs SLV Nov 26 14:46:53.704
*0 *0 *0 *0 0 1|0 0.0% 0.1% 0 2.01G 139M 0|0 0|0 583b 58.5k 22 rs SLV Nov 26 14:46:54.704
*0 *0 *0 *0 0 1|0 0.0% 0.1% 0 2.01G 139M 0|0 0|0 480b 58.4k 22 rs SLV Nov 26 14:46:55.708
*0 *0 *0 *0 2 4|0 0.0% 0.1% 0 2.01G 139M 0|0 0|0 3.11k 60.9k 22 rs SLV Nov 26 14:46:56.706
*0 *0 *0 *0 0 2|0 0.0% 0.1% 0 2.01G 139M 0|0 0|0 483b 58.7k 22 rs SLV Nov 26 14:46:57.703
*0 *0 *0 *0 0 1|0 0.0% 0.1% 0 2.01G 139M 0|0 0|0 581b 58.4k 22 rs SLV Nov 26 14:46:58.706
*0 *0 *0 *0 0 4|0 0.0% 0.1% 0 2.01G 139M 0|0 0|0 635b 59.1k 22 rs SLV Nov 26 14:46:59.703
*0 *0 *0 *0 0 2|0 0.0% 0.1% 0 2.01G 139M 0|0 0|0 659b 58.7k 22 rs SLV Nov 26 14:47:00.704
*0 *0 *0 *0 4 8|0 0.0% 0.1% 0 2.01G 139M 0|0 0|0 5.92k 64.8k 22 rs SLV Nov 26 14:47:01.704
2.mongotop 命令
mongotop也是mongodb下的一个内置工具,mongotop提供了一个方法,用来跟踪一个MongoDB的实例,查看哪些大量的时间花费在读取和写入数据。 mongotop提供每个集合的水平的统计数据。默认情况下,mongotop返回值的每一秒。
启动你的Mongod服务,进入到你安装的MongoDB目录下的bin目录, 然后输入mongotop命令,如下所示:
[root@10-0-42-211 bin]# ./mongotop -h 10.0.42.211:27017 --username=admin --password=123456 --authenticationDatabase admin
2022-11-26T14:48:11.737+0800 WARNING: On some systems, a password provided directly using --password may be visible to system status programs such as `ps` that may be invoked by other users. Consider omitting the password to provide it via stdin, or using the --config option to specify a configuration file with the password.
2022-11-26T14:48:11.757+0800 connected to: mongodb://10.0.42.211:27017/
ns total read write 2022-11-26T14:48:12+08:00
local.oplog.rs 1ms 1ms 0ms
admin.$cmd.aggregate 0ms 0ms 0ms
admin.system.keys 0ms 0ms 0ms
admin.system.users 0ms 0ms 0ms
admin.system.version 0ms 0ms 0ms
config.external_validation_keys 0ms 0ms 0ms
config.image_collection 0ms 0ms 0ms
config.settings 0ms 0ms 0ms
config.system.indexBuilds 0ms 0ms 0ms
config.system.sessions 0ms 0ms 0ms