How does MongoDB free up free space?

Source: Internet
Author: User
Tags mongodb mongodb free

When we delete a document or collection from MongoDB, MongoDB does not release the disk space that is already occupied, and it maintains a data file that has occupied disk space, although there may be a large and small list of empty records in the data file. When the client program inserts the document again, MongoDB allocates the storage space from the empty list of records to the new document. So for more efficient use of disk space, we need to defragment the data files of MongoDB and reclaim the unused space. There are two types of thought:
1. Reorganization of the original data
2. Only copy the data to form a full backup of the data only

Here are a few common implementation methods:
1、compact2、db.repairDatabase()3、secondary节点重同步2、db.copyDatabase()
First, Compat

The official website defines the command: rewrite and defragment all data and indexes in the collection.

How to use
use yourdatabase;db.runCommand({ compact : ‘yourCollection‘ });
Precautions
1、在执行命令前请保证你有比较新的备份2、在使用MMAPv1存储引擎的MongoDB上compact需要数据文件所在分区至少有2G的空闲空间3、在使用WiredTiger存储引擎的MongoDB上,compact命令将重写集合和索引,且释放未使用的空间,但使用MMAPv1存储引擎的MongoDB上,该命令只对集合的数据文件进行碎片整理并重新创建其索引。不会释放空间,在使用MMAPv1存储引擎的MongoDB上回收空间,建议使用第三种方法“secondary节点重同步”4、使用MMAPv1存储引擎的MongoDB中的Capped Collections,是无法被压缩的,但使用WiredTiger存储引擎的MongoDB在执行compact时会进行压缩。5、在副本集上运行该命令时,要分别在每个节点执行6、该命令只能在mongod实例上执行,不能再mongos实例上运行。也就是说针对分片集群的compact操作要分别在每个分片节点上执行。7、一般该命令运行在secondary节点上,在执行时,会强制节点进入RECOVERING状态,RECOVERING状态的实例读写操作将被阻塞8、再碰到特殊情况要停止运行该命令时,可通过db.currentOp()查询进程信息,然后通过db.killOp()干掉进程9、compact可能会增加数据文件的总大小和数量,尤其是第一次运行时。但这不会增加总集合使用的磁盘空间,因为存储大小是数据库文件中分配的数据量,而不是文件系统上文件的大小/数量10、使用MMAPv1存储引擎的MongoDB中的Capped Collections,是无法被压缩的,但使用WiredTiger存储引擎的MongoDB在执行compact时会进行压缩。
Second, Db.repairdatabase ()

Official website The definition of this command: Rebuild the database and index by throwing invalid or corrupted data. Similar to file system repair command fsck. So this command is primarily used to repair data.

How to use
use yourdatabase;db.repairDatabase();
Precautions
1、db.repairDatabase()主要用于修复数据。若你拥有数据的完整副本,且有权限访问,请使用第三种方法“secondary节点重同步”2、在执行命令前请保证你有比较新的备份3、此命令会完全阻塞数据库的读写,谨慎操作4、此命令执行需要数据文件所在位置有等同于所有数据文件大小总和的空闲空间再加上2G5、在使用MMAPv1存储引擎的secondary节点上执行该命令可以压缩集合数据6、在使用WiredTiger存储引擎的MongoDB库上执行不会有压缩的效果7、再碰到特殊情况要停止运行该命令时,可通过db.currentOp()查询进程信息,然后通过db.killOp()干掉进程8、非常消耗时间
Three, secondary node re-synchronization

The main idea is to delete the specified data in the secondary node to restart the data synchronization with the primary. Resynchronization can also be used when the replica set member data is too old. The resynchronization of the data is different from the direct copy data file, and MongoDB synchronizes the data only, so the data file after the resynchronization is complete does not have an empty collection, thus realizing the recovery of disk space.

How to use

You must first ensure that the data has a full backup.

1、若是primary节点,先强制将之变为secondary节点,否则跳过此步骤:    rs.stepdown(120);2、然后在primary上删除secondary节点:    rs.remove("IP:port");3、删除secondary节点dbpath下的所有文件。4、将节点重新加入集群,然后使之自动进行数据的同步:    rs.add("IP:port");5、等数据同步完成后,循环1-4的步骤可以将集群中所有节点的磁盘空间释放针对一些特殊情况,不能下线secondary节点的,可以新增一个节点到副本集中,然后secondary就自动开始数据的同步了。总的来说,重同步的方法是比较好的,第一基本不会阻塞副本集的读写,第二消耗的时间相对前两种比较短
Iv. Db.copydatabase ()
mongodb还支持在线复制数据:db.copyDatabase("from","to","IP:port"),此种方法也能释放空间,因为db.copyDatabase复制的数据,而不是表示在磁盘中的数据文件。但,该命令在4.0版本起被弃用;3.x版本还能继续使用如:    db.copyDatabase("sourceDB","DistDB");    将源库sourceDB。拷贝为DistDB。当然,该命令支持远程复制。该命令的完整语法为:db.copyDatabase(<源数据库名称>, <目标数据库名称>, <源mongodb的IP:port>, <源数据库连接需要的账户>,<密码>, <mechanism>)以上:命令必须在目标数据库服务器上执行。若源数据库与目标数据库存在于一个MongoDB服务器,<源mongodb的IP:port>, <源数据库连接需要的账户>,<密码>都可省略。<mechanism>是身份验证类型,可选的。
Precautions
1、db.copyDatabase()不会阻塞源数据库和目标数据库数据的读写,因此可能会出现两份数据不一致的情况2、db.copyDatabase()复制索引数据会锁定数据库,此操作也会对其他数据库产生影响3、db.copyDatabase()不要在mongos实例中使用4、db.copyDatabase()不要用于复制包含分片集合的数据库5、在4.0版中更改:db.copyDatabase()仅支持SCRAM进行身份验证fromhost,<mechanism>选项。6、某些不同版本的MongoDB间不支持此种复制方法,详见链接:https://docs.mongodb.com/manual/reference/method/db.copyDatabase/

In addition, there are some methods, such as using the Import/Export method (Mongodump/mongorestore), this method in the data volume is very large situation is not applicable, because the import and export method uses the full amount of form, to ensure that there is enough free space to hold the imported data.

How does MongoDB free up free space?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.