MongoDB数据库数据清理
清理MongoDB集群数据:
1、登录MongoDB集群(mongos):
# mongo -u username -p password --authenticationDatabase admin 127.0.0.1:27017/admin
2、查询2018-01-31之前的数据(mongos):
mongos> db.tcache.find({"ft":{"$lt":new Date("2018-01-31T00:00:00.000Z")}}).count() # mongo-u username -p password --authenticationDatabase admin 127.0.0.1:27017/yqtrack_gather01 --eval 'db.tcache.find({"ft":{"$lt":new Date("2018-01-31T00:00:00.000Z")}}).count()'
3、删除2018-01-31之前的数据(mongos):
mongos> db.tcache.deleteMany({"ft":{"$lt":new Date("2018-01-31T00:00:00.000Z")}}) # mongo -u username -p password --authenticationDatabase admin 127.0.0.1:27017/yqtrack_gather01 --eval 'db.tcache.deleteMany({"ft":{"$lt":new Date("2018-01-31T00:00:00.000Z")}})'
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
收缩MongoDB节点数据库:
1、登录Mongod节点(mongod):
# mongo -u __system -p "$(tr -d '\011-\015\040' < /usr/local/mongodb/etc/mongodb.key )" --authenticationDatabase local 172.16.1.151:27018/yqtrack_gather07
2、确认节点为从数据库:
rs1:SECONDARY> rs.status()
3、设置从数据库节点可以读写:
rs1:SECONDARY> db.getMongo().setSlaveOk()
4、切换至对应数据库:
rs1:SECONDARY> use yqtrack_gather01;
5、强制收缩对应数据库:
rs1:SECONDARY> db.runCommand({compact:'tcache',force:true }); # mongo -u __system -p "$(tr -d '\011-\015\040' < /usr/local/mongodb/etc/mongodb.key )" --authenticationDatabase local 172.16.1.87:27018/yqtrack_gather01 --eval 'db.runCommand({compact:"tcache",force:true})'
6、将主节点降级为从节点:
rs1:PRIMARY> rs.stepDown()
---------------------------------------------------------------- shell ---------------------------------------------------------------------
查询指定节点,指定日期区间的数据量:
#!/bin/bash for((p=1;p<=10;p+=1)) do for((d=592;d>=506;d-=5)) do day=`date -d -"$d days" +%Y-%m-%d` echo "$day" day_utc=""$day"T00:00:00.000Z" date="'db.tcache.find({\"ft\":{\"\$lt\":new Date(\"$day_utc\")}}).count()'" if [ "$p" -lt '10' ] then mongo -u username -p password --authenticationDatabase admin 127.0.0.1:27017/yqtrack_gather0"$p" --eval "$date" RESULT=$? else mongo -u username -p password --authenticationDatabase admin 127.0.0.1:27017/yqtrack_gather"$p" --eval "$date" fi sleep 5 done done
收缩指定节点的数据:
#!/bin/bash for((d=1;d<=10;d+=1)) do if [ "$d" -lt '10' ] then mongo -u __system -p "$(tr -d '\011-\015\040' < /usr/local/mongodb/etc/mongodb.key )" --authenticationDatabase local 127.0.0.1:27018/yqtrack_gather0"$d" --eval 'db.runCommand({compact:"tcache",force:true})' else mongo -u __system -p "$(tr -d '\011-\015\040' < /usr/local/mongodb/etc/mongodb.key )" --authenticationDatabase local 127.0.0.1:27018/yqtrack_gather"$d" --eval 'db.runCommand({compact:"tcache",force:true})' fi done
[THE END]