注意:
1、mongodb必须为分片集群,且每个分片集群的从节点不能读写。
2、另外需要知道mongo分片集群已经创建好的集合的名称
3、需要提前将需要回收的数据库写到列表中
4、因为从节点禁止读写,所以读取数据库列表是从主节点去执行,在去从节点执行回收命令
#!/usr/bin/python3
from pymongo import MongoClient
from multiprocessing import Process
m_client = MongoClient(host="172.20.100.124", port=27001, connect=False)
m_dblist = m_client.list_database_names()
out_list = ["admin","config","local","test"]
for a in out_list:
if a in m_dblist:
m_dblist.remove(a):
def get_db_stats(l):
new_m_client = MongoClient(host="172.20.100.105", port=27001, connect=False)
a = new_m_client[l].command("compact", 'user')
b = new_m_client[l].command("compact", 'event')
return a,b
if __name__ == '__main__':
for i in range(0, len(m_dblist), 10):
m = m_dblist[i:i + 10]
for q in range(len(m)):
p = Process(target=get_db_stats, args=(m[q],))
p.start()
p.join()