清理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]
