MongoDB log Rotation

Source: Internet
Author: User
Tags install mongodb mongo shell

MongoDB log Rotation

MongoDB can use a standard method to rotate logs, that is, to obtain the current log and enable a new one. To achieve this, the mongod instance renames the current log file by adding a UTC (GMT) timestamp at the end of the file name, and then opens the new log file, close the old log file and send all new log information to the new log file.

Rotation Setps

The MongoDB standard log rotation method is through the log rotation command, or let the mongod process receive a SIGUSER1 signal, the following implementation method

1. Enable the mongod process to ensure appending is enabled.

Mongod-v -- logpath/var/log/mongodb/server1.log -- logappend

2. Check whether the mongo log file exists on the terminal.

Ls/var/log/mongodb/server1.log *

3. In Windows, open mongo shell, enter the admin database, and enter the rotation command, that is

Use admin

Db. runCommand ({logRotate: 1 })

In Linux, run kill-SIGUSR1 <mongod process id> on the terminal.

4. check whether a new mongo log file is generated.

Ls/var/log/mongodb/server1.log *

My auto script and crontab

The following is a scheduled script written in python. A new log is generated every day. logs that have been written for more than 10 days are automatically deleted.

#! /Bin/env python
Import sys
Import OS
Import commands
Import datetime, time

# Get mongo pid
Performance_pid = commands. getoutput ("/sbin/pidof mongod ")
Print pai_pid

# Send Sig to mongo
If pai_pid! = '':
Cmd = "/bin/kill-USR1% s" % (pai_pid)
Print cmd
Export _rotate = commands. getoutput (cmd)
Else:
Print "specified D is not running ..."

# Clean log which> 10 days
Str_now = time. strftime ("% Y-% m-% d ")
Dat_now = time. strptime (str_now, "% Y-% m-% d ")
Array_dat_now = datetime. datetime (dat_now [0], dat_now [1], dat_now [2])
Lns = commands. getoutput ("/bin/ls -- full-time/var/log/mongodb/| awk '{print $6, $9 }'")
For ln in lns. split ('\ n '):
Ws = ln. split ()
If len (ws )! = 2:
Continue
Ws1 = time. strptime (ws [0], "% Y-% m-% d ")
Ws2 = datetime. datetime (ws1 [0], ws1 [1], ws1 [2])
If (array_dat_now-ws2). days> 10:
V_del = commands. getoutput ("/bin/rm-rf/var/log/mongodb // % s" % (ws [1])

View scheduled tasks in crontab-e under root

0 2 ***/home/jiangjianjian/pai_log_rotate.py>/home/jiangjianjian/null 2> & 1

CentOS compilation and installation of MongoDB

CentOS compilation and installation of php extensions for MongoDB and mongoDB

CentOS 6 install MongoDB and server configuration using yum

Install MongoDB2.4.3 in Ubuntu 13.04

MongoDB beginners must read (both concepts and practices)

MongoDB Installation Guide for Ubunu 14.04

MongoDB authoritative Guide (The Definitive Guide) in English [PDF]

Nagios monitoring MongoDB sharded cluster service practice

Build MongoDB Service Based on CentOS 6.5 Operating System

MongoDB details: click here
MongoDB: click here

This article permanently updates the link address:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.