设为首页 收藏本站
查看: 662|回复: 0

[经验分享] mongoDB文件太大查错纪录

[复制链接]
YunVN网友  发表于 2015-7-9 08:00:34 |阅读模式
  

日志系统,突然从24号之后的都断层了,交易看不见。查了一下问题是MongoDB把硬盘撑爆了,看了下情况:



-bash-3.2$ du -h
82M     ./log
3.1G    ./db/journal
4.0K    ./db/ciflogs/_tmp
4.0G    ./db/ciflogs
4.0K    ./db/local/_tmp
1.1G    ./db/local
4.0K    ./db/_tmp
8.1G    ./db
8.2G    .
  去google了两把,又去官网看了下,发现官网FAQ中有一段回答:(注意加粗部分)


Why are the files in my data directory larger than the data in my database?



  • Preallocated data files.
    In the data directory, MongoDB preallocates data files to a particular size, in part to prevent file system fragmentation. MongoDB names the first data file .0, the next .1, etc. The first file mongodallocates is 64 megabytes, the next 128 megabytes, and so on, up to 2 gigabytes, at which point all subsequent files are 2 gigabytes. The data files include files with allocated space but that hold no data. mongod may allocate a 1 gigabyte data file that may be 90% empty. For most larger databases, unused allocated space is small compared to the database.
      On Unix-like systems, mongod preallocates an additional data file and initializes the disk space to 0. Preallocating data files in the background prevents significant delays when a new database file is next allocated.
      You can disable preallocation with the noprealloc run time option. However noprealloc is not intended for use in production environments: only use noprealloc for testing and with small data sets where you frequently drop databases.
      On Linux systems you can use hdparm to get an idea of how costly allocation might be:





    time hdparm --fallocate $((1024*1024)) testfile

  • The oplog.
    If this mongod is a member of a replica set, the data directory includes the oplog.rs file, which is a preallocated capped collection in the local database. The default allocation is approximately 5% of disk space on 64-bit installations, seeOplog Sizing for more information. In most cases, you should not need to resize the oplog. However, if you do, seeChange the Size of the Oplog.


  • The journal.
    The data directory contains the journal files, which store write operations on disk prior to MongoDB applying them to databases. See Journaling Mechanics.


  • Empty records.
    MongoDB maintains lists of empty records in data files when deleting documents and collections. MongoDB can reuse this space, but will never return this space to the operating system.
    To de-fragment allocated storage, use compact, which de-fragments allocated space. By de-fragmenting storage, MongoDB can effectively use the allocated space. compact requires up to 2 gigabytes of extra disk space to run. Do not use compact if you are critically low on disk space.



    Important
    compact only removes fragmentation from MongoDB data files and does not return any disk space to the operating system.
    http://docs.mongodb.org/manual/faq/storage/


  然后在Journaling Mechanics页面又有详细对于Journa的介绍:


Journal Files

With journaling enabled, MongoDB creates a journal directory within the directory defined by dbpath, which is /data/db by default. The journal directory holds journal files, which contain write-ahead redo logs. The directory also holds a last-sequence-number file. A clean shutdown removes all the files in the journal directory.
Journal files are append-only files and have file names prefixed with j._. When a journal file holds 1 gigabyte of data, MongoDB creates a new journal file. Once MongoDB applies all the write operations in the journal files, it deletes these files. Unless you write many bytes of data per-second, the journal directory should contain only two or three journal files.
To limit the size of each journal file to 128 megabytes, use the smallfiles run time option when starting mongod.
To speed the frequent sequential writes that occur to the current journal file, you can ensure that the journal directory is on a different filesystem.



Important

If you place the journal on a different filesystem from your data files you cannot use a filesystem snapshot alone to capture valid backups of a dbpath directory. In this case, use fsyncLock() to ensure that database files are consistent before the snapshot and fsyncUnlock() once the snapshot is complete.


Note

Depending on your filesystem, you might experience a preallocation lag the first time you start a mongod instance with journaling enabled.
MongoDB may preallocate journal files if the mongod process determines that it is more efficient to preallocate journal files than create new journal files as needed. The amount of time required to pre-allocate lag might last several minutes, during which you will not be able to connect to the database. This is a one-time preallocation and does not occur with future invocations.
http://docs.mongodb.org/manual/core/journaling/

  
  文中可以看出,journa最多只有3个文件。也就是最大只会占用3G硬盘,而且停止之后会自动删除。启动时使用-smallfiles则会让mongo的journa最大128M。
  另外删除的纪录不会立刻释放硬盘,但会在下次写入的时候重新利用。
  OK,停一下Mongo,删掉journa,用smallfiles参数。另外删掉一些太早的日志纪录。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-84504-1-1.html 上篇帖子: C#代码 利用MongoDB中Group聚合函数查询 下篇帖子: mongodb指南(翻译)(十九)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表