Debugging local MongoDB connectivity issue on macOS

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 🙌

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 the Bootstrap failed: 5: Input/output error and mongodb-community error root /Library/LaunchDaemons/homebrew.mxcl.mongodb-community.plist error.
  • 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 the Unable to unlink socket file error message.

  • The socket file, /tmp/mongodb-27017.sock was owned by root instead of my current user. This results in brew being 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.

Did you find this article valuable?

Support Bernice Choy by becoming a sponsor. Any amount is appreciated!