How to Fix MongoDB Broken Pipe on Ubuntu 20.04
Troubleshooting “MongoDB Broken Pipe” on Ubuntu 20.04
The “Broken Pipe” error in MongoDB, particularly on Ubuntu 20.04, often signals an underlying resource exhaustion or system limit issue rather than a direct network problem. This guide will walk you through diagnosing and resolving this common issue.
1. The Root Cause: Why this happens on Ubuntu 20.04
The “Broken Pipe” error occurs when a process attempts to write to a communication channel (like a network socket or named pipe) that has been unexpectedly closed by the other end, or by the operating system itself due to resource constraints. For MongoDB on Ubuntu 20.04, this most frequently stems from insufficient system resource limits, specifically:
- File Descriptors (
LimitNOFILE/ulimit -n): MongoDB, especially under load, requires a large number of file descriptors. It uses them for data files, journal files, replication logs, and crucially, for every incoming client connection. Ubuntu 20.04’s defaultsystemdunit file formongod.serviceoften setsLimitNOFILEto a value that is too low (e.g., 1024). When MongoDB hits this limit, it can fail to open new connections, manage existing ones, or even access its own data files, leading to connection drops and the “Broken Pipe” error being reported to clients or internally. - Processes/Threads (
LimitNPROC/ulimit -u): While less common than file descriptor limits, insufficient process or thread limits can also contribute, particularly in very high-concurrency environments, as MongoDB uses threads for various internal operations.
When these limits are too low, the mongod process can become unstable, drop client connections prematurely, or fail to start new operations, manifesting as “Broken Pipe” errors.
2. Quick Fix (CLI)
To quickly apply a persistent fix to the systemd service limits, we’ll use systemctl edit to create an override file for the mongod.service unit. This is the recommended approach as it preserves your changes even if the main mongod.service file is updated during package upgrades.
-
Open the systemd override editor:
sudo systemctl edit mongod.serviceThis command will open a temporary file in your default editor (usually
nanoorvi). -
Add/Modify resource limits: Paste or type the following configuration into the editor. We’ll set the file descriptor limit (
LimitNOFILE) to a robust value like 64000 and the processes limit (LimitNPROC) to match. These are generally good starting points for production MongoDB instances. Adjust based on your specific workload and server capacity if needed.[Service] LimitNOFILE=64000 LimitNPROC=64000 -
Save and exit:
- If using
nano: PressCtrl+X, thenYto confirm saving, thenEnter. - If using
vi/vim: PressEsc, then type:wqand pressEnter.
This action will create a file named
/etc/systemd/system/mongod.service.d/override.confwith your specified settings and automatically runsystemctl daemon-reload. - If using
-
Restart MongoDB: Apply the new limits by restarting the MongoDB service:
sudo systemctl restart mongod
3. Configuration Check
While the systemctl edit command creates the necessary override, it’s beneficial to understand where these configurations reside and other potential areas to check.
Primary Configuration Location (systemd Service Limits):
-
Override File: The
systemctl edit mongod.servicecommand creates or modifies:/etc/systemd/system/mongod.service.d/override.confThis file should contain the[Service]block withLimitNOFILEandLimitNPROCas specified in the Quick Fix. This is the preferred method for setting service-specific limits. -
Original Service File: The default limits are defined in the main MongoDB systemd unit file, typically:
/lib/systemd/system/mongod.serviceDo not edit this file directly, as your changes will be overwritten during package updates. Use the override method instead.
System-Wide Limits (Less Impactful for systemd Services):
-
/etc/security/limits.conf: This file sets system-wideulimitvalues for users and groups. While important for processes started directly by users (e.g., via SSH session),systemdservice limits (like those inoverride.conf) take precedence for services it manages. You can still set limits here for themongodbuser for completeness, though it’s often not the direct fix for the “Broken Pipe” service issue.Example entry (for the
mongodbuser):mongodb soft nofile 64000 mongodb hard nofile 64000 mongodb soft nproc 64000 mongodb hard nproc 64000For these changes to take effect for
limits.conf, a reboot might be required, or for specific users to log out and back in. Forsystemdservices, theoverride.confmethod is more direct and effective.
4. Verification
After applying the configuration changes, it’s crucial to verify that the new limits are active and that MongoDB is running without issues.
-
Check MongoDB Service Status: Ensure the MongoDB service is running and healthy.
sudo systemctl status mongodLook for
Active: active (running)and check the latest log entries for any errors. -
Inspect MongoDB Logs: Review the MongoDB logs for any
BrokenPipeErrormessages or other critical issues. Thejournalctlcommand is ideal for this.sudo journalctl -u mongod --since "10 minutes ago" -fLet this run for a few minutes, especially during periods of expected load, to ensure no new
BrokenPipeErrororToo many open filesmessages appear. -
Verify Running Process Limits: Confirm that the
mongodprocess is actually operating with the new, higher limits.a. Find the MongoDB process ID (PID):
pgrep mongod(This will usually return one or more PIDs. Pick the main
mongodserver process PID.)b. Check the limits for that PID: Replace
<PID>with the actual PID you found.cat /proc/<PID>/limits | grep -E "Max open files|Max processes"You should see
Max open filesandMax processes(orMax user processes) reflecting your configured limits (e.g.,64000). -
Connect and Test Functionality: Connect to your MongoDB instance using the
mongoshell or your application and perform various operations (read, write, aggregation) to confirm full functionality and stability under load.
By systematically addressing the system resource limits, you should effectively resolve “MongoDB Broken Pipe” errors on Ubuntu 20.04. If the issue persists, further investigation into network connectivity, client-side configuration, or MongoDB’s internal logging for other error indicators might be necessary.