Rotating logs in MongoDB can be done by issuing a logrotate command or alternatively by sending a SIGUSR1 signal to the mongod instance. Here’s how you can do it:
Method 1: Using logrotate command
1. Create a logrotate configuration file, for example `/etc/logrotate.d/mongod` with the following content:
\`\`\` /var/log/mongodb/mongod.log { daily rotate 7 compress dateext missingok notifempty create 640 mongodb mongodb sharedscripts postrotate /bin/kill -SIGUSR1 `cat /var/lib/mongodb/mongod.lock 2> /dev/null` 2> /dev/null || true endscript } \`\`\` This configuration will rotate the logs daily and keep 7 days of logs. Adjust the configuration according to your needs.1. Then you can manually run logrotate with `sudo logrotate /etc/logrotate.d/mongod`.
Method 2: Sending SIGUSR1 signal to the mongod process
1. Find the process ID of your mongod instance, you can do this with the command `pgrep mongod` or `cat /var/lib/mongodb/mongod.lock`.
1. Using the process ID found, for example if process ID is 12345, send the SIGUSR1 signal with `kill -SIGUSR1 12345` command.
Remember, rotating logs will cause MongoDB to close the current log file and start a new one. So, you need to ensure the file system has enough space for the new file.