LoginSignup
3
6

More than 5 years have passed since last update.

MongoDBログローテーション

Last updated at Posted at 2017-04-07

環境・条件

CentOS7で実施した。

$ cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)

MongoDBのバージョンは3.4.2。

$ mongod --version
db version v3.4.2
git version: 3f76e40c105fc223b3e5aac3e20dcd026b83b38b
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
    distmod: rhel70
    distarch: x86_64
    target_arch: x86_64

手順

mongod.confを編集する。
ログ関連の設定については以下が参考になる。

systemlog-options

$ sudo /etc/mongod.conf

以下のように記述を編集する。

# logAppend: true #コメントアウトする
logRotate: rename

logAppendはデフォルトではfalseのはずだが、MongoDBを2.6から使っていたためか、trueになっていたのでコメントアウトした。

logAppendをfalseにしてもログがローテートされない?ため、注意。

systemLog.logAppendには、以下のような記載があり、

When true, mongos or mongod appends new entries to the end of the existing log file when the mongos or mongod instance restarts. Without this option, mongod will back up the existing log and create a new file.

When trueに対して、When falseではなく、Without this optionとあるため、falseに設定するのではなく設定自体をコメントアウトした方がよさそうである。

logRotateはデフォルトでrenameだが、明示的に指定する。

mongodを再起動する。

$ sudo systemctl restart mongod

mongoシェルを起動し、以下のようにコマンドを発行する。

$ mongo

> use admin;

> db.runCommand('logRotate', 1);

> exit

ログファイルの出力先(mongod.confのsystemLog:pathで設定がある)を確認する。
以下のようにログファイルが分割されていればOK。

$ ls /var/log/mongodb -l
-rw-r--r-- 1 mongod mongod    7445330  4月 7 13:36 mongod.log
-rw-r--r-- 1 mongod mongod 2143691041  4月 7 12:05 mongod.log.2017-04-07T03-05-51
3
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
6