Connection keep being closed

Hey guys. I am using mongodb with my nodejs application, and I am keeping receiving this connection closed error:

“connection x to IP:27017 closed”

Every once in a while I am getting this error. I dont know if this is something happening on my code-setup (using nestjs with mongoose), or if is something configurable at atlas side.

Someone already had this error?

Edit: I already whitelisted the ips. I can connected but my app keep getting disconnected.

1 Like

Hello @JulioW ,

If you have already whitelisted your current IP address and still getting the issue then I would recommend you to check a few things such as:

  • Check if any other process is trying to use the same 27017 port and creating conflict.
  • Check if there is any firewall blocking access or frequent IP address changes.
  • Ensure your network is stable and doesn’t have intermittent disruptions as it can cause connections to drop.
  • Check the TCP keepalive value for the affected systems.
  • You can also implement error handling and connection retry logic in your Node.js application. This will help your application gracefully handle dropped connections and attempt reconnection.
  • Lastly, ensure that you are using a recent version of Mongoose, as newer versions often have improvements.

If you’ve checked all of the above and the issue still persists, it might be helpful to provide more information about your specific setup, how frequently you’re experiencing disconnections and code so that a more detailed analysis can be conducted. Additionally, check MongoDB’s logs for any related error messages that might provide more insight into the issue.


I have the exact same problem. Connection closes after a few minutes both in Node and MongoDB Compass. I discovered that PIs associated with node.exe and MongoDbCompass.exe are using the port 27017. Is that a problem?