今天收到监控警告,说服务器磁盘快满了.
登上去看了一下,发现 MongoDB 的日志没做切分,大小也达到了13G,那就给他做个切分吧.
查看 MongoDB 配置
登录服务器,查看 MongoDB 的配置文件,看下有没有配置日志切分的参数.
cat /PATH/TO/MONGODB/CONFIG
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| processManagement: fork: true net: bindIp: IP1,IP2 port: 27017 storage: dbPath: /PATH/TO/DB/mongodb systemLog: destination: file path: "/PATH/TO/LOG/mongod.log" logAppend: true storage: journal: enabled: true replication: replSetName: rs0 security: keyFile: "/PATH/TO/DB/mongod.key"
|
查看 systemLog 段有没有加 logAppend 和 logRotate,没有就加上:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| processManagement: fork: true net: bindIp: IP1,IP2 port: 27017 storage: dbPath: /PATH/TO/DB/mongodb systemLog: destination: file path: "/PATH/TO/LOG/mongod.log" logAppend: true logRotate: reopen storage: journal: enabled: true replication: replSetName: rs0 security: keyFile: "/PATH/TO/DB/mongod.key"
|
MongoDb 配置到这里就完成了,接下来搞切割配置.
新建 logrotate 配置
新建个文件,这里就叫 mongod 了
touch mongod
把配置写进去:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| /PATH/TO/LOG/*log { create 0644 root root daily rotate 60 dateext missingok notifempty sharedscripts postrotate /bin/kill -USR1 `cat /PATH/TO/DB/mongod.lock 2>/dev/null` 2>/dev/null || true endscript }
|
这里要把 postrotate 路径换成你 pid 文件的路径, 一般会在 /var/run 目录下.
测试一下配置是否正常
logrotate -df ./mongod
测试通过后就把它移动到 logrotate 的配置目录.
重启 MongoDB, 应用设置
这里我用的是 supervisor ,所以直接用 supervisorctl 重启 MongoDB.
supervisorctl restart mongodb
直接启动的话就在 MongoDB 终端中关闭:
1 2 3 4
| mongod
use admin db.shutdownServer()
|
关闭后重新启动即可.
参考文档:
MongoDB - Rotate Log Files