Background

因為一直以來都沒有對Mongo的log檔做過優化, 所以他就一直越來越大XD. 所以特地查了一下. 那其實Mongo有內建的Log Rotate功能, 但我看了老半天 好像跟我預期的目標有一段差距. (沒有壓縮, 不穩定). 所以查了一下網路上有人利用Linux內建Logrotate的機制來達成. 我這邊也記錄一下.

Logrotate

Logrotate可以幫助我們管理日誌文件, 比如像是週期性的讀取日誌,壓縮日誌,備份日誌,創建新的日誌文件等. 通常Logrotate會每天執行一次(/etc/cron.daily/)

Setting

# add pid file
$ sudo mkdir /tmp/mongodb/
$ sudo touch /tmp/mongodb/pid
$ sudo chown -R mongodb:mongodb /tmp/mongodb/
$ sudo vim /etc/mongod.conf

mongod.conf

# mongod.conf
# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb-rs
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:
# where to write logging data.
systemLog:
  verbosity: 2
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log
# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0
processManagement:
  pidFilePath: /tmp/mongodb/pid
security:
  keyFile: /var/lib/mongodb/mongodb-keyfile
#operationProfiling:
replication:
  replSetName: rs0
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
  • 新增processManagement欄位
  • verbosity看你的需求. 這部分可以去看官網的介紹

接著再新增一個 mongo rotate 的設定檔在/etc/logrotate.d/

$ sudo vim /etc/logrotate.d/mongod

/etc/logrotate.d/mongod

/var/log/mongodb/mongod.log  {
    daily
    missingok
    rotate 30
    copytruncate
    dateext
    compress
    notifempty
    create 644 root root
    sharedscripts
    postrotate
        /bin/kill -SIGUSR1 'cat /tmp/mongodb/pid 2> /dev/null' 2> /dev/null || true
    endscript
}
  • /var/log/mongodb/mongod.log -> 對應到你mongod.conf內的log路徑
  • /tmp/mongodb/pid -> 對應到你mongod.conf內的pidFilePath路徑
  • daily:每天輪換日誌. Other options: daily,weekly,monthly, yearly.
  • rotate 30: 保留30個日誌文件,刪掉過時老舊的日誌文件(配合daily使用則表示保留30天的日誌文件)
  • compress:壓縮日誌文件(默認gzip格式)
  • notifempty:不輪轉空文件
  • create 640 nginx adm:創建一個新的日誌文件,並設置權限permissions/owner/group.
  • sharedscripts:在所有的日誌輪換完畢後執行postrotate腳本。如果該項沒有設置,則會在每個匹配的文件輪換後執行postrotate腳本。
  • postrotate:輪換日誌完成後運行的腳本。

最後執行下面的命令來看是否有順利設定完成

$ sudo logrotate -f -v /etc/logrotate.d/mongod
reading config file /etc/logrotate.d/mongod
Handling 1 logs
rotating pattern: /var/log/mongodb/mongod.log   forced from command line (30 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/mongodb/mongod.log
  log needs rotating
rotating log /var/log/mongodb/mongod.log, log->rotateCount is 30
dateext suffix '-20170211'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding old rotated logs failed
copying /var/log/mongodb/mongod.log to /var/log/mongodb/mongod.log-20170211
truncating /var/log/mongodb/mongod.log
running postrotate script
compressing log with: /bin/gzip

到mongodb的log目錄下則會看到變化.

$ cd /var/log/mongodb
$ ls
mongod.log mongod.log-20170211.gz

Reference