$ cat get_table_size.sh
#!/bin/bash
db="test"
function mongo_login() {
/path/to/bin/mongo --quiet --host xx.xx.xx.xx --port=27017 -u username --password='XXX' --authenticationDatabase=admin
}
tables=`echo -e "use ${db};\n show tables;" | mongo_login | grep -v "switched to"`
for table in ${tables}
do
echo "${table}"
size=`echo -e "use ${db};\n db.${table}.totalSize()" | mongo_login | grep -v "switched to"`
echo ${size} | awk '{size=$1/1024;if(size<1024){printf("%10.3f KB\t%s\n",size,$2);}else{size=size/1024;if(size<1024){printf("\033[36m%10.3f MB\t%s\n\033[0m",size,$2);}else{size=size/1024;if(size<1024){printf("\033[35m%10.3f GB\t%s\n\033[0m",size,$2);}else{size=size/1024;printf("\033[31m%10.3f TB\t%s\n\033[0m",size,$2);}}}}'
echo ""
done
#!/bin/bash
function mongo_login() {
/path/to/bin/mongo --quiet --host xx.xx.xx.xx --port=27017 -u username --password='XXX' --authenticationDatabase=admin
}
dbs=`echo -e "use admin;\n show dbs;" | mongo_login`
for db in ${dbs}
echo "############ ${db} ###########"
tables=`echo -e "use ${db};\n show tables;" | mongo_login | grep -v "switched to"`
for table in ${tables}
do
echo "${table}"
size=`echo -e "use ${db};\n db.${table}.totalSize()" | mongo_login | grep -v "switched to"`
echo ${size} | awk '{size=$1/1024;if(size<1024){printf("%10.3f KB\t%s\n",size,$2);}else{size=size/1024;if(size<1024){printf("\033[36m%10.3f MB\t%s\n\033[0m",size,$2);}else{size=size/1024;if(size<1024){printf("\033[35m%10.3f GB\t%s\n\033[0m",size,$2);}else{size=size/1024;printf("\033[31m%10.3f TB\t%s\n\033[0m",size,$2);}}}}'
echo ""
done
注:此处mongo_login 如果定义成变量 ,用 ${mongo_login} 引用,会语法报错
image.png image.png参考
mongoDB统计库、表大小
https://www.jianshu.com/p/e4ab13dded03
Shell脚本获取MongoDB数据量大小
https://www.yangxinghui.com/1450.html
MongoDB查看数据库和表大小
http://www.zhouqishan.cn/2015/07/29/mongodb%E6%9F%A5%E7%9C%8B%E6%95%B0%E6%8D%AE%E5%BA%93%E5%92%8C%E8%A1%A8%E5%A4%A7%E5%B0%8F/
Mongo库表占用空间统计
https://www.cnblogs.com/mengrennwpu/p/14722867.html
MongoDB-查看数据库和集合大小
https://cloud.tencent.com/developer/article/1876450
mongo 查看某个库下面每个集合的大小
https://blog.csdn.net/qq_36470475/article/details/104928526
网友评论