0

背景:设置了 1 个成员和 1 个仲裁器的 MongoDB ReplicaSet。释放磁盘空间是必要的,因为不再需要 MongoDB 中的数据。所有文件都被db.collection.remove({})命令删除。之后db.runCommand ( { compact: 'collection', force:true} )db.collection.reIndex()db.repairDatabase()运行,db.stats()看起来像:

> db.stats()
{
    "db" : "database",
    "collections" : 2,
    "objects" : 6,
    "avgObjSize" : 167.83333333333334,
    "dataSize" : 1007,
    "storageSize" : 153042944,
    "numExtents" : 0,
    "indexes" : 5,
    "indexSize" : 45056,
    "ok" : 1
}

如您所见,DB 中只有 6 个文档(在第二个集合中),总存储大小为 153MB。但是,如果我们查看磁盘空间使用情况:

# du -h --max-depth=1 | grep mongodb
9.8G    ./mongodb

在 mongodb 目录中:

ls -l
total 9365696
-rw-r----- 1 username usergroup      16384 Aug 19 14:19 collection-0-2472884588219438804.wt
-rw-r----- 1 username usergroup      36864 Aug 19 14:19 collection-0-4266045208498277842.wt
-rw-r----- 1 username usergroup      12288 Aug 24 15:55 collection-0--7009505821458556818.wt
-rw-r----- 1 username usergroup      49152 Aug 19 14:20 collection-0--7439052959034576211.wt
-rw-r----- 1 username usergroup      36864 Aug 19 14:20 collection-0-8676027872146699793.wt
-rw-r----- 1 username usergroup      36864 Aug 19 14:19 collection-2-2472884588219438804.wt
-rw-r----- 1 username usergroup      32768 Aug 23 13:54 collection-2-2911328926458913167.wt
-rw-r----- 1 username usergroup 9589645312 Aug 24 15:55 collection-4-2472884588219438804.wt
-rw-r----- 1 username usergroup      36864 Aug 19 14:19 collection-7--7439052959034576211.wt
-rw-r----- 1 username usergroup      36864 Aug 19 14:19 collection-9--7439052959034576211.wt
drwxr-x--- 2 username usergroup       4096 Aug 24 16:03 diagnostic.data
-rw-r----- 1 username usergroup       4096 Aug 24 15:54 index-10-561247476684508201.wt
-rw-r----- 1 username usergroup      36864 Jul 20 15:09 index-10--7439052959034576211.wt
-rw-r----- 1 username usergroup      36864 Aug 19 14:19 index-11--7439052959034576211.wt
-rw-r----- 1 username usergroup      16384 Mar  4 09:20 index-1-2472884588219438804.wt
-rw-r----- 1 username usergroup      36864 Jul 20 15:09 index-1-4266045208498277842.wt
-rw-r----- 1 username usergroup      36864 Aug 19 14:20 index-1--7439052959034576211.wt
-rw-r----- 1 username usergroup      16384 Jul  5 13:25 index-1-8676027872146699793.wt
-rw-r----- 1 username usergroup      36864 Jul 20 15:09 index-2-4266045208498277842.wt
-rw-r----- 1 username usergroup      16384 Mar  4 09:20 index-3-2472884588219438804.wt
-rw-r----- 1 username usergroup      16384 Aug 23 14:00 index-6-561247476684508201.wt
-rw-r----- 1 username usergroup      16384 Aug 23 14:00 index-7-561247476684508201.wt
-rw-r----- 1 username usergroup       4096 Aug 24 15:54 index-8-561247476684508201.wt
-rw-r----- 1 username usergroup      16384 Aug 19 14:19 index-8--7439052959034576211.wt
-rw-r----- 1 username usergroup       4096 Aug 24 15:54 index-9-561247476684508201.wt
drwxr-x--- 2 username usergroup       4096 Aug 24 15:55 journal
-rw-r----- 1 username usergroup      36864 Aug 24 15:55 _mdb_catalog.wt
-rwxr-x--- 1 username usergroup          6 Aug 19 14:19 mongod.lock
-rw-r----- 1 username usergroup      36864 Aug 24 15:56 sizeStorer.wt
-rw-r----- 1 username usergroup         95 Dec 30  2015 storage.bson
-rw-r----- 1 username usergroup         46 Dec 30  2015 WiredTiger
-rw-r----- 1 username usergroup        534 Dec 30  2015 WiredTiger.basecfg
-rw-r----- 1 username usergroup       4096 Aug 19 14:19 WiredTigerLAS.wt
-rw-r----- 1 username usergroup         21 Dec 30  2015 WiredTiger.lock
-rw-r----- 1 username usergroup        903 Aug 24 15:57 WiredTiger.turtle
-rw-r----- 1 username usergroup      94208 Aug 24 15:57 WiredTiger.wt

这个字符串对我来说似乎很奇怪:

    -rw-r----- 1 username usergroup 9589645312 Aug 24 15:55 collection-4-2472884588219438804.wt

问题:神秘 collection-4-2472884588219438804.wt 文件中有什么?为什么它不受紧凑、repairDatabase 命令的影响?有没有办法强制 MongoDB 清空这个文件或以某种方式回收它的空间?

更新:在@james-wahlin 的帮助下,我们发现 9.8Gb 是 Replica Set Oplog 的大小。但是,尽管其他副本集成员可能会丢失数据,但我如何才能强制 MongoDB 释放空间?

4

1 回答 1

0

基于您删除了所有文档并且不再需要数据的事实:

  1. 停止 mongod 进程
  2. 手动从 ./mongodb 目录中删除所有文件

    > use admin
    switched to db admin
    > db.shutdownServer()
    2016-08-24T08:50:29.085-0500 I NETWORK  DBClientCursor::init call() failed server should be down...
    2016-08-24T08:50:29.096-0500 I NETWORK  trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
    2016-08-24T08:50:30.103-0500 W NETWORK  Failed to connect to 127.0.0.1:27017, reason: errno:10061 No connection could be made because the target machine actively refused it.
    2016-08-24T08:50:30.104-0500 I NETWORK  reconnect 127.0.0.1:27017 (127.0.0.1) failed failed couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed
    >
    
    # cd ./mongodb
    # rm -rf *
    

当您重新启动 mongo 时,它将根据需要初始化 mongodb 目录。

于 2016-08-24T13:55:29.630 回答