Managing MongoDB Logs with Logrotate ( Logs Delete )

2023. 3. 15. 09:34mongodb

반응형

In this article, we delve into the common pitfalls that MongoDB beginners encounter when it comes to log management.
One such mistake is failing to realize that logs constantly accumulate and take up precious disk space. Compounding this issue is the fact that the MongoDB installation manual and guide do not offer any guidance on log management, which can leave novice administrators struggling to make sense of the available commands. To help mitigate this problem, we provide a comprehensive overview of MongoDB log management settings that beginners are prone to overlooking. By taking the time to read and absorb this information, MongoDB beginners will gain a greater appreciation for the importance of log management and will be better equipped to handle logs through the use of appropriate settings.

1.Characteristics of MongoDB Logs:

• MongoDB runs fine without any log management settings.
• Log management needs to be done manually.
• Logs can accumulate without limits, causing disk space issues.


*Q: Is there any way to automate log management?
*A: Yes, by using logrotate.

반응형

2.Logrotate Location:

cd /etc/logrotate.d

3.Logrotate Configuration:

sudo vi mongod


Refer to /etc/mongo.conf when writing the configuration.
- Required information: location of log files.
- User and user group for execution.

For example:

/var/log/mongodb/mongod.log is the location of the log file set in CONFIG. 
create 664 user_group user_id to grant permissions and account access 
(user permissions). 
(If MongoDB was installed using apt-get, only the user_id needs to be changed.)

728x90



*Use the following command and modify the bolded sections:

cd /etc/logrotate.d
sudo vi mongod

*Write file

/var/log/mongodb/mongod.log
{
su root root
daily
size 300M
rotate 7
missingok
compress
delaycompress
notifempty
create 664 ubuntu ubuntu
sharedscripts
postrotate
sudo /bin/kill -SIGUSR1 ps -ef | grep mongod | grep -v grep | awk '{print $2}'
endscript
}


3. Before Logrotate runs, state of the logs

 cd /var/log/mongodb
ls


4. State of the logs after Logrotate runs (manual execution command):

sudo /usr/sbin/logrotate -f /etc/logrotate.conf



You can confirm that the file is being split as shown above, and that the file size does not exceed a certain size limit.

Scheduling through a cron tab makes it convenient to manage.

반응형