Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the way to clean up the excess disk space occupied by MongoDB

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

In this issue, the editor will bring you about the method of cleaning up the excess disk space occupied by MongoDB. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

The first point is that mongodb does not release the hard disk space that is already occupied, not even drop collection, unless drop database. If a db once had a large amount of data and then deleted after a period of time, hard disk space is a problem, how to recover the excess space occupied by mongdodb? There are two ways:

1. Dump & restore

Mongodump-d databasename-o / path/to/dump_dir

Echo 'db.dropDatabase ()' | mongo

Mongorestore-d / path/to/dump_dir

If the amount of data is small, dump does not take too long, or there are frequent backups of dump files, this method is simple.

2. Repair database

That is, run db.repairDatabase () in mongo shell, or

Db.runCommand ({repairDatabase: 1})

The second method can take several other parameters

{repairDatabase: 1

PreserveClonedFilesOnFailure:

BackupOriginalFiles:}

RepairDatabase is the only way to reclaim hard disk space in official documentation.

RepairDatabase is the appropriate and the only way to reclaim disk space.

When you have multiple shard and a large amount of data, the dump & restore method takes a lot of time, and the advantage of the second method is that running repairDatabase on each shard is much faster.

PS: formatting the output of mongo shell

If there is a large amount of data, the default output of mongo shell is so messy that it is almost impossible to read. We can solve this problem with .resume ():

Db.collection.find () .pretty ()

The output would be much more beautiful, one line per field.

{

"_ id": ObjectId ("5396cd3823e97923ba689ef3")

"batch": 66

"category": 4

"cover_imgs": [

"/ post_imgs/5396cd3823e97923ba689ef3/c_2.jpg"

"/ post_imgs/5396cd3823e97923ba689ef3/c_3.jpg"

"/ post_imgs/5396cd3823e97923ba689ef3/c_4.jpg"

]

"created_at": ISODate ("2014-06-10T09:18:06.383Z")

"fav_count": 0

Host_reply_count: 338

"last_reply_date": "2014-06-17 21:22:00"

"post_date": "2014-06-06 19:57:00"

"referer": "http://tieba.baidu.com/f?kw=%B9%C5%D7%B0%B5%E7%CA%D3%BE%E7","

Reply_count: 716

"reuse_type": 2

"section": "costume TV series"

"seq": 27180

"serial": false

"sort_index": 0.997

"source_site": "Post Bar"

"updated_at": ISODate ("2014-06-18T09:04:55.228Z")

"visible": true

}

{

"_ id": ObjectId ("5396c7ca23e97921fb7de8e4")

"batch": 74

"category": 4

}

Configure it to be Default:

Add the following configuration to $HOME/.mongorc.js, or create it if it doesn't exist.

DBQuery.prototype._prettyShell = true

This eliminates the need to use the pretty () method every time, but simply db.collection.find ().

The above is the method shared by Xiaobian to remove the excess disk space occupied by MongoDB. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report