首页 > 编程语言 > 详细

python脚本回收mongo集群磁盘碎片

时间:2020-10-12 17:30:39      阅读:21      评论:0      收藏:0      [点我收藏+]

注意:
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()
if ‘admin‘ in m_dblist:
    m_dblist.remove(‘admin‘)
if ‘config‘ in m_dblist:
    m_dblist.remove(‘config‘)
if ‘local‘ in m_dblist:
    m_dblist.remove(‘local‘)
if ‘test‘ in m_dblist:
    m_dblist.remove(‘test‘)
if ‘test‘ in m_dblist:
    m_dblist.remove(‘test‘)
if ‘test_label_1‘ in m_dblist:
    m_dblist.remove(‘test_label_1‘)
if ‘test_label_2‘ in m_dblist:
    m_dblist.remove(‘test_label_2‘)
if ‘ycsb‘ in m_dblist:
    m_dblist.remove(‘ycsb‘)
if ‘ycsbtes‘ in m_dblist:
    m_dblist.remove(‘ycsbtes‘)

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()


python脚本回收mongo集群磁盘碎片

原文:https://www.cnblogs.com/dzc-/p/13803624.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!