In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article is about how to release free space in MongoDB. Xiaobian thinks it is quite practical, so share it with everyone to learn. I hope you can gain something after reading this article. Let's not say much. Let's take a look at it together with Xiaobian.
I. COMPAT
The official website defines this command: rewrite and defragment all data and indexes in the collection.
use method
use yourdatabase;db.runCommand({ compact : 'yourCollection' });
precautions
1. Make sure you have a newer backup before executing the command
2. Compact requires at least 2G of free space in the partition where the data file is located on MongoDB using the MMAPv1 storage engine.
On MongoDB with WiredTiger storage engine, the compact command overwrites collections and indexes and frees unused space, but on MongoDB with MMAPv1 storage engine, the command defragments and recreates indexes only for collections 'data files. No free space, reclaim space on MongoDB using MMAPv1 storage engine, recommend using the third method "secondary node resynchronization"
Capped Collections in MongoDB using the MMAPv1 storage engine cannot be compressed, but MongoDB using the WiredTiger storage engine compresses when performing compact.
5. When running this command on a replica set, execute it separately on each node
This command can only be executed on mongod instances and cannot be run on mongos instances. This means that compact operations for a sharded cluster are performed on each sharded node separately.
7. Generally, this command runs on the secondary node. When executed, it will force the node to enter the RECOVERING state, and the instance reading and writing operations in the RECOVERING state will be blocked.
8. When you encounter special circumstances to stop running the command, you can query the process information through db.currentOp(), and then kill the process through db.killOp().
Compact may increase the total size and number of data files, especially the first time it runs. However, this does not increase the disk space used by the total collection because storage size is the amount of data allocated in the database file, not the size/number of files on the file system
Capped Collections in MongoDB using the MMAPv1 storage engine cannot be compressed, but MongoDB using the WiredTiger storage engine compresses when performing compact.
db.repairDatabase()
Definition of this command: rebuild database and index by losing invalid or corrupted data. Similar to file system repair command fsck. So this command is mainly used to repair data.
use method
use yourdatabase;db.repairDatabase();
precautions
db.repairDatabase() is mainly used to repair data. If you have a complete copy of the data and have access, use the third method,"secondary node resynchronization."
2. Make sure you have a newer backup before executing the command
3. This command will completely block the reading and writing of the database. Be careful.
4. The execution of this command requires that the location of the data file has free space equivalent to the sum of all data file sizes plus 2G
Executing this command on a secondary node using the MMAPv1 storage engine compresses aggregate data
6. There is no compression effect when executing on MongoDB library using WiredTiger storage engine
7. When you encounter special circumstances to stop running the command, you can query the process information through db.currentOp(), and then kill the process through db.killOp().
8, very time-consuming
Third, secondary node resynchronization
The idea is to delete the specified data from the secondary node and restart synchronization with the primary. Resync can also be used when replica set member data is too stale. Data resynchronization is different from direct replication of data files. MongoDB only synchronizes data, so the data files after resynchronization are not empty sets, thus realizing disk space reclamation.
use method
First of all, you must ensure that your data is fully backed up.
1. If it is a primary node, force it to become a secondary node first, otherwise skip this step:
rs.stepdown(120);
2. Then delete the secondary node on the primary:
rs.remove("IP:port");
Delete all files under the dbpath secondary node.
4. Rejoin the node to the cluster and make it automatically synchronize data:
rs.add("IP:port");
5. After the data synchronization is completed, the steps of loop 1-4 can free the disk space of all nodes in the cluster.
For some special cases, secondary nodes cannot be offline, you can add a node to the replica set, and then the secondary automatically starts data synchronization.
In general, resynchronization methods are relatively good, the first basically does not block the copy set read and write, the second consumes less time than the first two
db.copyDatabase()
mongodb also supports online replication of data: db.copyDatabase("from","to","IP:port"), which also frees up space because db.copyDatabase replicates data rather than representing data files on disk. However, this command has been deprecated since version 4.0; version 3.x can still be used.
For example:
db.copyDatabase("sourceDB","DistDB");
sourceDB. Copy to DistDB.
Of course, this command supports remote replication.
The complete syntax for this command is:
db.copyDatabase(, , , ,, )
Above: The command must be executed on the target database server. If the source database and target database exist on a single MongoDB server, , can be omitted. is the authentication type, optional.
precautions
db.copyDatabase() does not block the reading and writing of data in the source database and the target database, so there may be two cases of inconsistent data.
db.copyDatabase() Copying index data locks the database, which also affects other databases
db.copyDatabase() Do not use in mongos instances
db.copyDatabase() Do not use to copy databases that contain sharded collections
Changed in version 4.0: db.copyDatabase() only supports SCRAM authentication fromhost, option.
The above is how to release free space in MongoDB. Xiaobian believes that some knowledge points may be seen or used in our daily work. I hope you can learn more from this article. For more details, please 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.