1. 查看数据库
默认返回单位是bytes
> uset testDB
>db.stats()
{
"db" : "testDB", //当前数据库
"collections" : 27, //当前数据库多少表
"objects" : 18738550, //当前数据库所有表多少条数据
"avgObjSize" : 1153.54876188392, //每条数据的平均大小
"dataSize" : 21615831152.0, //所有数据的总大小
"storageSize" : 23223312272.0, //所有数据占的磁盘大小
"numExtents" : 121,
"indexes" : 26, //索引数
"indexSize" : 821082976, //索引大小
"fileSize" : 25691160576.0, //预分配给数据库的文件大小
"nsSizeMB" : 16,
"dataFileVersion" : {
"major" : 4,
"minor" : 5
},
"extentFreeList" : {
"num" : 1,
"totalSize" : 65536
},
"ok" : 1.0
}
可用通过 scale传参,改变输出单位
得到KB单位
db.stats(1024)
得到G单位
db.stats(1073741824);
{
"db" : "testDB",
"collections" : 27,
"objects" : 18736680,
"avgObjSize" : 1153.53257375373,
"dataSize" : 20, //所有数据的总大小
"storageSize" : 21, //所有数据占的磁盘大小
"numExtents" : 121,
"indexes" : 26,
"indexSize" : 0,
"fileSize" : 23, //预分配给数据库的文件大小
"nsSizeMB" : 16,
"dataFileVersion" : {
"major" : 4,
"minor" : 5
},
"extentFreeList" : {
"num" : 1,
"totalSize" : 0
},
"ok" : 1.0
}
注意:objects以及avgObjSize还是bytes为单位的,不受参数影响。
分片集群
$ mongo --host xx.xx.xx.xx --port 20221 -u username -p 'XXX' --authenticationDatabase admin
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5d6794b8f0b6dc3438d42be7")
}
shards:
{ "_id" : "fileShard01", "host" : "fileShard01/192.168.1.101:20002,192.168.1.102:20002", "state" : 1 }
{ "_id" : "fileShard02", "host" : "fileShard02/192.168.1.103:30003,192.168.1.104:30003", "state" : 1 }
active mongoses:
"3.4.17" : 3
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
NaN
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
207 : Success
databases:
{ "_id" : "fileDB", "primary" : "fileShard02", "partitioned" : true }
fileDB.fs.chunks
shard key: { "files_id" : 1 }
unique: false
balancing: true
chunks:
fileShard01 771038
fileShard02 771036
too many chunks to print, use verbose if you want to force print
mongos> db.stats()
{
"raw" : {
"fileShard01/192.168.1.101:20002,192.168.1.102:20002" : {
"db" : "test",
"collections" : 0,
"views" : 0,
"objects" : 0,
"avgObjSize" : 0,
"dataSize" : 0,
"storageSize" : 0,
"numExtents" : 0,
"indexes" : 0,
"indexSize" : 0,
"fileSize" : 0,
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(0, 0),
"electionId" : ObjectId("7fffffff0000000000000009")
}
},
"fileShard02/192.168.1.103:30003,192.168.1.104:30003" : {
"db" : "test",
"collections" : 0,
"views" : 0,
"objects" : 0,
"avgObjSize" : 0,
"dataSize" : 0,
"storageSize" : 0,
"numExtents" : 0,
"indexes" : 0,
"indexSize" : 0,
"fileSize" : 0,
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(0, 0),
"electionId" : ObjectId("7fffffff0000000000000005")
}
}
},
"objects" : 0,
"avgObjSize" : 0,
"dataSize" : 0,
"storageSize" : 0,
"numExtents" : 0,
"indexes" : 0,
"indexSize" : 0,
"fileSize" : 0,
"extentFreeList" : {
"num" : 0,
"totalSize" : 0
},
"ok" : 1
}
mongos> use fileDB
switched to db fileDB
mongos> db.stats()
{
"raw" : {
"fileShard01/192.168.1.101:20002,192.168.1.102:20002" : {
"db" : "fileDB",
"collections" : 1,
"views" : 0,
"objects" : 138695651,
"avgObjSize" : 125096.51585447333,
"dataSize" : 17350342704268,
"storageSize" : 17626939813888,
"numExtents" : 0,
"indexes" : 2,
"indexSize" : 4597989376,
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(0, 0),
"electionId" : ObjectId("7fffffff0000000000000009")
}
},
"fileShard02/192.168.1.103:30003,192.168.1.104:30003" : {
"db" : "fileDB",
"collections" : 14,
"views" : 0,
"objects" : 243453474,
"avgObjSize" : 62849.72022024627,
"dataSize" : 15300982727547,
"storageSize" : 15543496740864,
"numExtents" : 0,
"indexes" : 52,
"indexSize" : 25772535808,
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(0, 0),
"electionId" : ObjectId("7fffffff0000000000000005")
}
}
},
"objects" : 382149125,
"avgObjSize" : 85440.67331024767,
"dataSize" : NumberLong("32651325431815"),
"storageSize" : NumberLong("33170436554752"),
"numExtents" : 0,
"indexes" : 54,
"indexSize" : 30370525184,
"fileSize" : 0,
"extentFreeList" : {
"num" : 0,
"totalSize" : 0
},
"ok" : 1
}
mongos> db.stats(1073741824)
{
"raw" : {
"fileShard01/192.168.1.101:20002,192.168.1.102:20002" : {
"db" : "fileDB",
"collections" : 1,
"views" : 0,
"objects" : 138695809,
"avgObjSize" : 125096.38716546944,
"dataSize" : 16158.767622794956,
"storageSize" : 16416.366970062256,
"numExtents" : 0,
"indexes" : 2,
"indexSize" : 4.2822113037109375,
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(0, 0),
"electionId" : ObjectId("7fffffff0000000000000009")
}
},
"fileShard02/192.168.1.103:30003,192.168.1.104:30003" : {
"db" : "fileDB",
"collections" : 14,
"views" : 0,
"objects" : 243454007,
"avgObjSize" : 62849.64420397895,
"dataSize" : 14250.164590760134,
"storageSize" : 14476.00940322876,
"numExtents" : 0,
"indexes" : 52,
"indexSize" : 24.002544403076172,
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(0, 0),
"electionId" : ObjectId("7fffffff0000000000000005")
}
}
},
"objects" : 382149816,
"avgObjSize" : 85440.6581962269,
"dataSize" : 30408,
"storageSize" : 30892,
"numExtents" : 0,
"indexes" : 54,
"indexSize" : 28,
"fileSize" : 0,
"extentFreeList" : {
"num" : 0,
"totalSize" : 0
},
"ok" : 1
}
2. 看数据库表
> use testDB
> db.testColl.stats();
{
"ns" : "testDB.testColl",
"count" : 1,
"size" : 56,
"avgObjSize" : 56,
"storageSize" : 8192,
"numExtents" : 1,
"nindexes" : 1,
"lastExtentSize" : 8192,
"paddingFactor" : 1,
"systemFlags" : 1,
"userFlags" : 0,
"totalIndexSize" : 8176,
"indexSizes" : {
"_id_" : 8176
},
"ok" : 1
}
3. 查看当前数据库状态概要
>db.serverStatus()
{
"host":"localhost.localdomain",
"version":"1.8.1",
--服务器版本
"process":"mongod",
"uptime":3184,
--启动时间(单位:秒)
"uptimeEstimate":3174,
"localTime":ISODate("2012-05-28T11:20:22.819Z"),
"globalLock":{
"totalTime":3183918151,
"lockTime":10979,
"ratio":0.000003448267034299149,
"currentQueue":{
"total":0,
--当前全部队列量
"readers":0,
--读请求队列量
"writers":0
--写请求队列量
},
"activeClients":{
"total":0,
--当前全部客户端连接量
"readers":0,
--客户端读请求量
"writers":0
--客户端写请求量
}
},
"mem":{
"bits":32,
--32位系统
"resident":20,
--占用物量内存量
"virtual":126,
--虚拟内存量
"supported":true,
--是否支持扩展内存
"mapped":32
},
"connections":{
"current":1,
--当前活动连接量
"available":818
--剩余空闲连接量
},
...
"indexCounters":{
"btree":{
"accesses":0,
--索引被访问量
"hits":0,
--索引命中量
"misses":0,
--索引偏差量
"resets":0,
"missRatio":0
--索引偏差率(未命中率)
}
},
...
"network":{
"bytesIn":1953,
--发给此服务器的数据量(单位:byte)
"bytesOut":25744,
--此服务器发出的数据量(单位:byte)
"numRequests":30
--发给此服务器的请求量
},
"opcounters":{
"insert":0,
--插入操作的量
"query":1,
--查询操作的量
"update":0,
--更新操作的量
"delete":0,
--删除操作的量
"getmore":0,
"command":31
--其他操作的量
},
...
"ok":1
}
db.serverStatus命令与mongostat类似,不过它显示的数据更为具体和全面。
db.serverStatus也有一个缺陷,就是数据是静态的,而不是实时的。
参考
mongoDB 查看数据库和表大小
https://www.jb51.net/article/52517.htm
db.serverStatus() - 返回当前数据库状态的概要
https://www.mongodb.org.cn/manual/78.html
db.stats()
https://www.mongodb.com/docs/manual/reference/method/db.stats
网友评论