1 MongoDB 监控概述
MongoDB 自带2个监控工具: mongostat 和 mongotop。 但是从MongoDB 4.4 开始,这2个工具需要单独安装,在之前版本是集成的。
具体安装步骤参考之前的博客:
MongoDB 4.4 以后版本安装Database Tools 工具
https://www.cndba.cn/dave/article/107952
除了这2个监控工具之外,也可以使用其他几个函数来查看DB的信息。
2 Mongostat 工具
该工具的帮助如下:
[dave@www.cndba.cn_1 bin]# mongostat --help
Usage:
mongostat <options> <connection-string> <polling interval in seconds>
Monitor basic MongoDB server statistics.
Connection strings must begin with mongodb:// or mongodb+srv://.
See http://docs.mongodb.com/database-tools/mongostat/ for more information.
general options:
--help print usage
--version print the tool version and exit
--config= path to a configuration file
verbosity options:
-v, --verbose=<level> more detailed log output (include multiple times for more verbosity, e.g. -vvvvv, or specify a
numeric value, e.g. --verbose=N)
--quiet hide all log output
connection options:
-h, --host=<hostname> mongodb host(s) to connect to (use commas to delimit hosts)
--port=<port> server port (can also use --host hostname:port)
ssl options:
--ssl connect to a mongod or mongos that has ssl enabled
--sslCAFile=<filename> the .pem file containing the root certificate chain from the certificate authority
--sslPEMKeyFile=<filename> the .pem file containing the certificate and key
--sslPEMKeyPassword=<password> the password to decrypt the sslPEMKeyFile, if necessary
--sslCRLFile=<filename> the .pem file containing the certificate revocation list
--sslFIPSMode use FIPS mode of the installed openssl library
--tlsInsecure bypass the validation for server's certificate chain and host name
authentication options:
-u, --username=<username> username for authentication
-p, --password=<password> password for authentication
--authenticationDatabase=<database-name> database that holds the user's credentials
--authenticationMechanism=<mechanism> authentication mechanism to use
--awsSessionToken=<aws-session-token> session token to authenticate via AWS IAM
kerberos options:
--gssapiServiceName=<service-name> service name to use when authenticating using GSSAPI/Kerberos (default: mongodb)
--gssapiHostName=<host-name> hostname to use when authenticating using GSSAPI/Kerberos (default: <remote server's address>)
uri options:
--uri=mongodb-uri mongodb uri connection string
stat options:
-o=<field>[,<field>]* fields to show. For custom fields, use dot-syntax to index into serverStatus output, and
optional methods .diff() and .rate() e.g. metrics.record.moves.diff()
-O=<field>[,<field>]* like -o, but preloaded with default fields. Specified fields inserted after default output
--humanReadable= print sizes and time in human readable format (e.g. 1K 234M 2G). To use the more precise machine
readable format, use --humanReadable=false (default: true)
--noheaders don't output column names
-n, --rowcount=<count> number of stats lines to print (0 for indefinite)
--discover discover nodes and display stats for all
--http use HTTP instead of raw db connection
--all all optional fields
--json output as JSON rather than a formatted table
--useDeprecatedJsonKeys use old key names; only valid with the json output option.
-i, --interactive display stats in a non-scrolling interface
[dave@www.cndba.cn_1 bin]#
[dave@www.cndba.cn_1 bin]# mongostat --port=27018 -u "root" --authenticationDatabase "admin" -p "root"
2022-05-04T08:32:20.817+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 7|0 1.1% 3.7% 0 1.96G 112M 0|0 1|0 618b 51.9k 43 shard1 PRI May 4 08:32:21.844
*0 *0 *0 *0 0 15|0 1.1% 3.7% 0 1.96G 112M 0|0 1|0 3.27k 157k 43 shard1 PRI May 4 08:32:22.842
*0 *0 *0 *0 0 2|0 1.1% 3.7% 0 1.96G 112M 0|0 1|0 364b 50.3k 43 shard1 PRI May 4 08:32:23.843
*0 *0 *0 *0 0 6|0 1.1% 3.7% 0 1.96G 112M 0|0 1|0 1.68k 55.4k 43 shard1 PRI May 4 08:32:24.844
*0 *0 *0 *0 2 11|0 1.1% 3.7% 0 1.96G 112M 0|0 1|0 6.13k 60.0k 43 shard1 PRI May 4 08:32:25.843
*0 *0 *0 *0 0 7|0 1.1% 3.7% 0 1.96G 112M 0|0 1|0 1.53k 54.9k 43 shard1 PRI May 4 08:32:26.841
*0 *0 *0 *0 0 0|0 1.1% 3.7% 0 1.96G 112M 0|0 1|0 262b 49.7k 43 shard1 PRI May 4 08:32:27.843
*0 *0 *0 *0 0 3|0 1.1% 3.7% 0 1.96G 112M 0|0 1|0 870b 51.5k 43 shard1 PRI May 4 08:32:28.841
*0 *0 *0 *0 0 2|0 1.1% 3.7% 0 1.96G 112M 0|0 1|0 515b 51.0k 43 shard1 PRI May 4 08:32:29.843
*0 *0 *0 *0 0 7|0 1.1% 3.7% 0 1.96G 112M 0|0 1|0 3.64k 55.6k 43 shard1 PRI May 4 08:32:30.843
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 8|0 1.1% 3.7% 0 1.96G 112M 0|0 1|0 620b 52.0k 43 shard1 PRI May 4 08:32:31.843
*0 *0 *0 *0 0 10|0 1.1% 3.7% 0 1.96G 112M 0|0 1|0 1.91k 154k 43 shard1 PRI May 4 08:32:32.842
*0 *0 *0 *0 0 2|0 1.1% 3.7% 0 1.96G 112M 0|0 1|0 364b 50.4k 43 shard1 PRI May 4 08:32:33.843
*0 *0 *0 *0 0 7|0 1.1% 3.7% 0 1.96G 112M 0|0 1|0 1.68k 55.5k 43 shard1 PRI May 4 08:32:34.842
*0 *0 *0 *0 1 10|0 1.1% 3.7% 0 1.96G 112M 0|0 1|0 6.12k 60.0k 43 shard1 PRI May 4 08:32:35.842
*0 *0 *0 *0 0 6|0 1.1% 3.7% 0 1.96G 112M 0|0 1|0 1.53k 54.8k 43 shard1 PRI May 4 08:32:36.842
*0 *0 *0 *0 0 0|0 1.1% 3.7% 0 1.96G 112M 0|0 1|0 262b 49.8k 43 shard1 PRI May 4 08:32:37.842
*0 *0 *0 *0 0 2|0 1.1% 3.7% 0 1.96G 112M 0|0 1|0 867b 51.4k 43 shard1 PRI May 4 08:32:38.844
^C2022-05-04T08:32:39.638+0800 signal 'interrupt' received; forcefully terminating
[dave@www.cndba.cn_1 bin]#
因为我们这里是密码是使用明文的,所以在开始之前还给了一个告警信息。
关键列说明:
- insert、query、update、delete 分别代表每秒的操作次数
- getmore代表当前批量查询得到的文档个数,如果查询的文档多,mongodb会自动批量查询
- command代表primary和secondary的节点指令个数,如果是在从库上执行,则代表从库执行的命令数据以及复制从库的其他实例的命令执行情况,二者通过|分割。
- dirty代表wiretiger存储引擎的缓冲中脏字节的百分比。
- used代表已经使用的wiretiger存储引擎缓存比例
- flushed对于wireTiger存储引擎,表示触发检查点的次数;对于MMAPv1存储引擎,表示当前将数据写入磁盘的次数
- vsize:程序应用的虚拟内存大小
- res:当前已经使用的物理内存量,单位为Mb
- qrw:等待读取的文档个数与等待写入的文档个数
- arw:正在执行的读取文档个数与正在执行的写入文档个数
- net_in|net_out 进出的网络流量
- conn:当前连接数
3 mongotop 工具
工具帮助:
[dave@www.cndba.cn_1 bin]# mongotop --help
Usage:
mongotop <options> <connection-string> <polling interval in seconds>
Monitor basic usage statistics for each collection.
Connection strings must begin with mongodb:// or mongodb+srv://.
See http://docs.mongodb.com/database-tools/mongotop/ for more information.
general options:
--help print usage
--version print the tool version and exit
--config= path to a configuration file
verbosity options:
-v, --verbose=<level> more detailed log output (include multiple times for more verbosity, e.g. -vvvvv, or specify a
numeric value, e.g. --verbose=N)
--quiet hide all log output
connection options:
-h, --host=<hostname> mongodb host(s) to connect to (use commas to delimit hosts)
--port=<port> server port (can also use --host hostname:port)
ssl options:
--ssl connect to a mongod or mongos that has ssl enabled
--sslCAFile=<filename> the .pem file containing the root certificate chain from the certificate authority
--sslPEMKeyFile=<filename> the .pem file containing the certificate and key
--sslPEMKeyPassword=<password> the password to decrypt the sslPEMKeyFile, if necessary
--sslCRLFile=<filename> the .pem file containing the certificate revocation list
--sslFIPSMode use FIPS mode of the installed openssl library
--tlsInsecure bypass the validation for server's certificate chain and host name
authentication options:
-u, --username=<username> username for authentication
-p, --password=<password> password for authentication
--authenticationDatabase=<database-name> database that holds the user's credentials
--authenticationMechanism=<mechanism> authentication mechanism to use
--awsSessionToken=<aws-session-token> session token to authenticate via AWS IAM
kerberos options:
--gssapiServiceName=<service-name> service name to use when authenticating using GSSAPI/Kerberos (default: mongodb)
--gssapiHostName=<host-name> hostname to use when authenticating using GSSAPI/Kerberos (default: <remote server's address>)
uri options:
--uri=mongodb-uri mongodb uri connection string
output options:
--locks report on use of per-database locks
-n, --rowcount=<count> number of stats lines to print (0 for indefinite)
--json format output as JSON
[dave@www.cndba.cn_1 bin]#
[dave@www.cndba.cn_1 bin]# mongotop --port=27018 -u "root" --authenticationDatabase "admin" -p "root"
2022-05-04T08:45:24.556+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-05-04T08:45:24.572+0800 connected to: mongodb://localhost:27018/
ns total read write 2022-05-04T08:45:25+08:00
local.oplog.rs 1ms 1ms 0ms
admin.system.roles 0ms 0ms 0ms
admin.system.users 0ms 0ms 0ms
admin.system.version 0ms 0ms 0ms
cndba.user 0ms 0ms 0ms
config.cache.chunks.cndba.user 0ms 0ms 0ms
config.cache.chunks.config.system.sessions 0ms 0ms 0ms
config.cache.collections 0ms 0ms 0ms
config.cache.databases 0ms 0ms 0ms
config.migrationCoordinators 0ms 0ms 0ms
相关列说明:
- Ns:集合名字
- total:读写花费时间(单位是ms)
- read:读花费时间
- write:写花费时间
mongotop输出的内容表示每个集合的每个表读写情况,它打印了每个库里面读写花费的时长,单位是ms,可以帮助快速定位读写瓶颈。
4 其他DB 函数
这里涉及的相关命令有:
- db.serverStatus()
- db.stats()
- db.collStats()
- rs.status()
4.1 db.serverStatus() 函数
这条命令会列出MongoDB的整体情况,包含主机名字、版本、进程、连续运行时间、连接状态以及操作状态。
shard1:PRIMARY> use admin
switched to db admin
shard1:PRIMARY> db.auth('root','root')
1
shard1:PRIMARY> db.serverStatus()
{
"host" : "mongodb1.novalocal:27018",
"version" : "4.4.13",
"process" : "mongod",
"pid" : NumberLong(19910),
"uptime" : 35418,
"uptimeMillis" : NumberLong(35417192),
"uptimeEstimate" : NumberLong(35417),
"localTime" : ISODate("2022-05-04T00:54:19.010Z"),
"asserts" : {
"regular" : 0,
"warning" : 0,
"msg" : 0,
"user" : 644,
"rollovers" : 0
},
"connections" : {
"current" : 41,
"available" : 81879,
"totalCreated" : 165,
"active" : 17,
"exhaustIsMaster" : 15,
"exhaustHello" : 0,
"awaitingTopologyChanges" : 17
},
……
因为它显示的结果比较长,这里我们只说说常用的几个信息。
- host:主机名字
- version:MongoDB版本
- process:PID进程号
- uptime:主机的运行时间
- asserts:MongoDB启动后报警的统计数量
- connections:MongoDB的连接统计信息
- network:MongoDB的网路情况
- storageEngine:存储引擎信息
- mem:当前使用的内存信息
4.2 db.stats()函数
该命令显示的是db的信息,没有server层面的信息:
shard1:PRIMARY> db.stats()
{
"db" : "admin",
"collections" : 2,
"views" : 0,
"objects" : 6,
"avgObjSize" : 179.5,
"dataSize" : 1077,
"storageSize" : 57344,
"indexes" : 3,
"indexSize" : 77824,
"totalSize" : 135168,
"scaleFactor" : 1,
"fsUsedSize" : 4775596032,
"fsTotalSize" : 52601794560,
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(0, 0),
"electionId" : ObjectId("7fffffff0000000000000001")
},
"lastCommittedOpTime" : Timestamp(1651625885, 1),
"$configServerState" : {
"opTime" : {
"ts" : Timestamp(1651625884, 1),
"t" : NumberLong(1)
}
},
"$clusterTime" : {
"clusterTime" : Timestamp(1651625885, 1),
"signature" : {
"hash" : BinData(0,"pZwsDVWHOMdNYcpX3JSnVmCcN/c="),
"keyId" : NumberLong("7093529851058978835")
}
},
"operationTime" : Timestamp(1651625885, 1)
}
shard1:PRIMARY>
相关值说明:
- db:代表当前数据库的名称
- collections:当前数据库中集合的数量
- view:当前数据库中视图的数量
- objects:当前数据库中所有文档的数量
- avgObjSize:数据库中文档的平均大小
- dataSize:当前数据库的数据大小,单位是byte
- storageSize:当前数据库占用硬盘空间的大小,单位是byte
- numExtents:当前数据库中所有集合Extents扩展的数量统计
- indexes:当前数据库中的索引数量
- indexSize:当前数据库中的索引代销,单位是byte
- fsUsedSize:当前MongoDb所在的硬盘已经使用的空间大小
- fsTotalSize:当前MongoDb所在的硬盘总共的空间大小
- ok:1表示成功,0表示失败
4.3 db.coll.stats()函数
这个函数返回的是集合的状态信息,由于输出的内容很多,这里我们仅说明重要的几个部分:
> db.yeyz.stats()
{
"ns" : "test.yeyz",
"size" : 115,
"count" : 3,
"avgObjSize" : 38,
"storageSize" : 36864,
"capped" : false,
"wiredTiger" : {
"metadata" : {
"formatVersion" : 1
},
……
相关说明:
- ns:当前集合的名称
- size:当前集合的大小,单位是byte
- count:当前集合的文档数量
- nindexes:当前集合中索引的数量
- totalIndexSize:当前集合中所有索引的大小,单位是byte
4.4 rs.status() 函数
这个函数是用来查看副本集中的成员的状态信息,如下:
shard1:PRIMARY> rs.status()
{
"set" : "shard1",
"date" : ISODate("2022-05-04T01:01:05.855Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"votingMembersCount" : 3,
"writableVotingMembersCount" : 3,
……
常用的字段说明:
- set:副本集的名称
- date:当前的时间
- myState:当前副本集节点的状态
- term:副本集的选举数
- syncingTo:从哪个副本集同步数据,如果这里是空值,则代表当前副本是primary
- syncSourceHost、syncSourceId分别代表同步目标节点的host和id值
- heartbeatIntervalMills:副本集心跳频率,单位为ms
- optimes:副本集同步的进度信息
- lastStableCheckPointTimestamp:列出最近的检查点的时间。
- members:列出副本集中每个成员的信息数据。
版权声明:本文为博主原创文章,未经博主允许不得转载。