MongoDB数据库数据清理
<p><strong>清理MongoDB集群数据:</strong></p><p>1、登录MongoDB集群(mongos):</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># mongo -u username -p password --authenticationDatabase admin 127.0.0.1:27017/admin
</pre>
</div>
<p>2、查询2018-01-31之前的数据(mongos):</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">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()'
</pre>
</div>
<p>3、删除2018-01-31之前的数据(mongos):</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">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")}})'
</pre>
</div>
<p>---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------</p>
<p><strong>收缩MongoDB节点数据库:</strong></p>
<p>1、登录Mongod节点(mongod):</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># mongo -u __system -p "$(tr -d '\011-\015\040' < /usr/local/mongodb/etc/mongodb.key )" --authenticationDatabase local 172.16.1.151:27018/yqtrack_gather07
</pre>
</div>
<p>2、确认节点为从数据库:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">rs1:SECONDARY> rs.status()
</pre>
</div>
<p>3、设置从数据库节点可以读写:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">rs1:SECONDARY> db.getMongo().setSlaveOk()
</pre>
</div>
<p>4、切换至对应数据库:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">rs1:SECONDARY> use yqtrack_gather01;
</pre>
</div>
<p>5、强制收缩对应数据库:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">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})'
</pre>
</div>
<p>6、将主节点降级为从节点:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">rs1:PRIMARY> rs.stepDown()
</pre>
</div>
<p>---------------------------------------------------------------- shell ---------------------------------------------------------------------</p>
<p><strong>查询指定节点,指定日期区间的数据量:</strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">#!/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
</pre>
</div>
<p><strong>收缩指定节点的数据:</strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">#!/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
</pre>
</div>
<p><strong> </strong> </p><br><br>
来源:https://www.cnblogs.com/configure/p/11896498.html
頁:
[1]