Debugging local MongoDB connectivity issue on macOS
A fledgling engineer dabbling into areas of DevOps, AWS and automation. I enjoy tinkering with technology frameworks and tools to understand and gain visibility in the underlying mechanisms of the "magic" in them.
In the progress of accumulating nuggets of wisdom in the different software engineering disciplines!
Context
I was following the Fastify "Getting Started" guide to learn about Fastify, a low overhead web framework. I faced some difficulties starting my Fastify app due to the following error with my local MongoDB setup when I got back on working with it subsequently.
# PROJECT_NAME is a placeholder to replace my actual filepath
{"level":50,"time":"2022-01-08T16:07:37.330Z","reason":{"type":"Unknown","servers":{},"stale":false,"compatible":true,"heartbeatFrequencyMS":10000,"localThresholdMS":15},"stack":"MongoServerSelectionError: connect ECONNREFUSED ::1:27017\n at Timeout._onTimeout (~/<PROJECT_NAME>/node_modules/mongodb/lib/sdam/topology.js:330:38)\n at listOnTimeout (node:internal/timers:557:17)\n at processTimers (node:internal/timers:500:7)","type":"Error","msg":"connect ECONNREFUSED ::1:27017"}
Possible Causes
After searching up Stack Overflow and referring to the official documentation of Fastify and MongoDB, the issues I faced are as listed below
The bindIp address is configured to 127.0.0.1.
The mongodb service has not been started. OR The mongodb service was not shutdown properly.
Unable to unlink socket file
Resolution
While it was an unexpected cause, I eventually realized the issue was mainly due to the MongoDB service not running. 🤦🏻♀️ This was not obvious due to the unintentional blockers caused by myself while debugging🥲. Nonetheless, let's step through the issues 🙌
Unable to unlink socket file
Rationale
In hindsight, this issue was caused by running the commands with
sudo. This was in an attempt to repair the mongodb service when I faced theBootstrap failed: 5: Input/output errorandmongodb-community error root /Library/LaunchDaemons/homebrew.mxcl.mongodb-community.plisterror.Eventually, by reading through the logs in
/opt/homebrew/var/log/mongodb/mongo.log, I realize it was due to a permission issue upon finding theUnable to unlink socket fileerror message.The socket file,
/tmp/mongodb-27017.sockwas owned byrootinstead of my current user. This results inbrewbeing unable to stop or start the service as it only has the privilege of the current user.
Remediation
I resolved this issue with the following commands posted in this Stack Overflow posst to set the socket file to be owned by the current user and restart the service to ensure that the mongodb service can be started.
sudo chown `whoami` /tmp/mongodb-27017.sock
brew services restart mongodb-community
Local MongoDB configuration
Rationale
As mentioned in Fastify "Get Started" guide's note, the default bind IP points to 127.0.0.1. So if you want the project to point to all IPv4 addresses, you should set it as 0.0.0.0.
Action Taken
Upon resolving the permission file with the socket file, this may not be the main cause of connectivity. Nonetheless, I decided to update to ensure that the MongoDB is able to accommodate all IPv4 addresses in /opt/homebrew/etc/mongod.conf.
Conclusion
I went on a wild-goose chase in trying to resolve the deceivingly simple connectivity issue by Googling based on the generic error messages🙃. It can be daunting to look at logs when you are unfamiliar with reading logs. Nonetheless, that is key in providing you with a clearer direction in finding the root cause.
As Mac v10.13.6 and above (i.e. Mac High Sierra onwards) does not allow HomeBrew to use the root user to install. It is best to avoid running sudo with the brew command for the services, as you may not know how it would affect underlying system files such as the socket file in my case.
That's all in documenting how I resolve the connect ECONNREFUSED ::1:27017 connectivity issue with local MongoDB on macOS.
![[DH] Keeping an organised inventory of annotated screenshots](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1731688657857%2Fcaeeeb6f-1530-4c88-a820-d22deaf0391e.png&w=3840&q=75)



