Why Did My MongoDB Logs Turn Into PCX Images? Investigation and Fix
A MongoDB shard node’s logs unexpectedly turned into PCX image files due to improper log rotation using echo, causing CPU spikes and slow‑query analysis failures; the investigation reproduces the issue, explains the header corruption, and presents a SIGUSR1‑based solution to restore proper ASCII logs and retrieve the needed time‑range data.
Background
MongoDB cluster with four shard nodes, each running on a 16‑core/32 GB CentOS 7.4 server. Log files are stored under
/dataand each day generates about 23‑24 GB of logs.
Incident
A night‑time alert reported high CPU on a shard node. The team assumed the cause was a slow query and attempted to extract the log segment for the high‑CPU period (15:00–16:00), but the log files were identified as “PCX ver. 2.5 image data” instead of the expected ASCII text.
Root Cause Analysis
Log rotation was performed with
echo > log, which inserts a newline character (0x0a) at the beginning of the file.
The inserted newline matches the header of a PCX image, causing the file type detection to report an image format.
Repeated use of
echo > logon a locked MongoDB log file overwrote the original content with zeros, further corrupting the file.
Reproduction
Creating an empty file and running
echo > logshows the file header becomes a newline (0x0a) in hex dump, which is the same header as a PCX image. The same behavior was observed on the production server.
Solution
Replace the manual
echo > logrotation with the standard MongoDB signal‑based rotation:
<code>kill -SIGUSR1 $(pidof mongod)</code>A sample init script (
/etc/init.d/mongo_logspit.sh) is provided to automate the SIGUSR1 rotation and clean logs older than seven days.
Recovery Procedure
Split the corrupted 23 GB log file into 6 GB chunks using
split -b 6G.
Identify chunks that are still ASCII text and extract the 15:00–16:00 window with
mlogfilterand
mloginfo.
Generate slow‑query scatter plots with
mplotqueries.
Conclusion
Using
echo > logfor log rotation corrupts MongoDB logs by inserting a newline that mimics a PCX header; the proper method is to send
SIGUSR1to the mongod process. After fixing the rotation, logs return to normal ASCII format and the required time‑range data can be retrieved.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.