Linux 删除mongodb的日志文件是否安全?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19533019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:09:24  来源:igfitidea点击:

Is it safe to delete the journal file of mongodb?

linuxmongodbdisk

提问by user94602

If I delete the 3.1G journal file, sudo service mongodb restartwill fail. However, this file is taking too much space. How can I solve this problem? How can I remove it?

如果我删除3.1G的日志文件,sudo service mongodb restart会失败。但是,此文件占用了太多空间。我怎么解决这个问题?我怎样才能删除它?

bash$ du -sh /var/lib/mongodb/*
4.0K    _tmp
65M auction_development.0
128M    auction_development.1
17M auction_development.ns
3.1G    journal
4.0K    mongod.lock

采纳答案by mnemosyn

TL;DR: You have two options. Use the --smallfilesstartup option when starting MongoDB to limit the size of the journal filesto 128MB, or turn off journalling using the --nojournaloption. Using --nojournalin production is usually a bad idea, and it often makes sense to use different write concerns also in development so you don't have different code in dev and prod.

TL;DR:你有两个选择。--smallfiles启动 MongoDB 时使用启动选项将日志文件的大小限制为 128MB,或使用该--nojournal选项关闭日志记录。--nojournal在生产中使用通常是一个坏主意,在开发中使用不同的写关注点通常是有意义的,这样你在开发和生产中就没有不同的代码。

The long answer: No, deleting the journal file isn't safe. The idea of journalling is this:

长答案:不,删除日志文件不安全。日记的想法是这样的:

A write comes in. Now, to make that write persistent (and the database durable), the write must somehow go to the disk.

写入进来了。现在,要使写入持久化(并使数据库持久化),写入必须以某种方式转到磁盘。

Unfortunately, writes to the disk take eons compared to writes to the RAM, so the database is in a dilemma: not writing to the disk is risky, because an unexpected shutdown would cause data loss. But writing to the disk for every single write operation will decrease the database's performance so badly that it becomes unusable for practical purposes.

不幸的是,与写入 RAM 相比,写入磁盘需要花费大量时间,因此数据库处于两难境地:不写入磁盘是有风险的,因为意外关闭会导致数据丢失。但是每次写入操作都写入磁盘会严重降低数据库的性能,以至于无法用于实际用途。

Now instead of writing to the data files themselves, and instead of doing it for every request, the database will simply append to a journal file where it stores all the operations that haven't been committed to the actual data files yet. This is a lot faster, because the file is already 'hot' since it's read and written to all the time, and it's only one file, not a bunch of files, and lastly, because it writes all pending operations in a batch every 100ms by default. Deleting this file in the middle of something wreaks havoc.

现在,不是写入数据文件本身,也不是为每个请求都执行此操作,数据库将简单地附加到日志文件中,在该文件中存储尚未提交到实际数据文件的所有操作。这要快得多,因为文件已经“热”了,因为它一直在读取和写入,而且它只是一个文件,而不是一堆文件,最后,因为它每 100 毫秒批量写入所有挂起的操作默认情况下。在某些事情中删除此文件会造成严重破坏。

回答by Mitja

As explained in mnemosyn's answer, journaling is essential to the storage engines. Luckily, it can be controlled to some extent. The following was written for the MMAPv1storage engine, which was the default until MongoDB 3.2. Then, WiredTigerbecame the engine of choice, to which more information can be found at the bottom of this answer.

正如 mnemosyn 的回答中所解释的那样,日志对于存储引擎来说是必不可少的。幸运的是,它可以在一定程度上得到控制。以下是为MMAPv1存储引擎编写的,这是 MongoDB 3.2 之前的默认值。然后,WiredTiger成为首选引擎,更多信息可以在此答案的底部找到。

MMAPv1

MMAPv1

MongoDB < 2.6 (Non-YAML Config)

MongoDB < 2.6(非 YAML 配置)

For our development server, we used the following procedure:

对于我们的开发服务器,我们使用了以下过程:

cp -p /etc/mongodb.conf /etc/mongodb.conf.orig
vi /etc/mongodb.conf

Now, insert

现在,插入

smallfiles=true

into the mongodb.conf, then save. smallfileslimits the journal file to 128MB.

进入mongodb.conf,然后保存。smallfiles将日志文件限制为 128MB。

service mongodb stop
rm -rf /var/lib/mongodb/journal/*
service mongodb start

MongoDB >= 2.6 (YAML Config)

MongoDB >= 2.6(YAML 配置)

If you're using MMAPv1 with the YAML config style, use the same step to backup the config as above, but into the

如果您将 MMAPv1 与YAML 配置样式一起使用,请使用与上述相同的步骤备份配置,但进入

  mmapv1:

config block, insert

配置块,插入

    smallFiles: true 

. Afterwards, proceed as above, restarting the server whilst removing the journals.

. 之后,按上述操作,在删除日志的同时重新启动服务器。

WiredTiger (MongoDB >=3.0, default since 3.2)

WiredTiger(MongoDB >=3.0,自 3.2 起默认)

On development machines, journal files under WiredTiger should be somewhat smaller by default than under MMAPv1, as journal compressionis enabled by default. According to the documentation, "WiredTiger journal files for MongoDB have a maximum size limit of approximately 100 MB". It will "create checkpoints (i.e. write the snapshot data to disk) at intervals of 60 seconds or 2 gigabytes of journal data."

在开发机器上,默认情况下 WiredTiger 下的日志文件应该比 MMAPv1 下的要小一些,因为默认情况下启用了日志压缩。根据文档,“MongoDB 的 WiredTiger 日志文件的最大大小限制约为 100 MB”。它将“以 60 秒或 2 GB 的日志数据为间隔创建检查点(即,将快照数据写入磁盘)”。

Thus, if you're only running a low amount of requests (with little data to change) on your database, the journal files using WiredTiger should not exceed a low multiple of 100 MB. The size of the journal files seems not to be configurable, however.

因此,如果您只在数据库上运行少量请求(几乎没有数据可更改),则使用 WiredTiger 的日志文件不应超过 100 MB 的低倍数。然而,日志文件的大小似乎不可配置。

回答by Jadeye

mongodbhas evolved since. Now its v3.4.1 stable.
I am on v3.2here's how:
uncomment # mmapv1:so it looks like:

mongodb从那以后就进化了。现在它的v3.4.1 稳定版
我在v3.2 上是这样的:
取消注释# mmapv1:所以它看起来像:

  mmapv1:
    smallFiles: true 

if you have a different version look for storage Optionson the reference/configuration-optionspage.

如果您有不同的版本,请storage Optionsreference/configuration-options页面上查找。

don't forget to empty the journal

不要忘记清空 journal

sudo service mongodb stop
sudo rm -rf /var/lib/mongodb/journal/*
sudo service mongodb start