Skip to content

Issues connecting with MongoDB

0

My application from ECS was connecting fine to MongoDB and recently we started encoutering issues with MongoDB connection. I have attached logs for your reference. Tried everything and running out of options.

2025-11-27T19:00:52.632Z app listening on port 3000 2025-11-27T19:00:52.633Z (node:40) NOTE: The AWS SDK for JavaScript (v2) is in maintenance mode. 2025-11-27T19:00:52.633Z SDK releases are limited to address critical bug fixes and security issues only. 2025-11-27T19:00:52.633Z Please migrate your code to use AWS SDK for JavaScript (v3). 2025-11-27T19:00:52.633Z For more information, check the blog post at https://a.co/cUPnyil 2025-11-27T19:00:52.633Z (Use node --trace-warnings ... to show where the warning was created) 2025-11-27T19:00:56.000Z /user/node_modules/mongodb/lib/cmap/connect.js:387 2025-11-27T19:00:56.000Z return new error_1.MongoNetworkError(err); 2025-11-27T19:00:56.000Z ^ 2025-11-27T19:00:56.000Z MongoNetworkError: Client network socket disconnected before secure TLS connection was established 2025-11-27T19:00:56.000Z at connectionFailureError (/user/node_modules/mongodb/lib/cmap/connect.js:387:20) 2025-11-27T19:00:56.000Z at TLSSocket.<anonymous> (/user/node_modules/mongodb/lib/cmap/connect.js:310:22) 2025-11-27T19:00:56.000Z at Object.onceWrapper (node:events:639:26) 2025-11-27T19:00:56.000Z at TLSSocket.emit (node:events:524:28) 2025-11-27T19:00:56.000Z at TLSSocket.emit (node:domain:489:12) 2025-11-27T19:00:56.000Z at emitErrorNT (node:internal/streams/destroy:169:8) 2025-11-27T19:00:56.000Z at emitErrorCloseNT (node:internal/streams/destroy:128:3) 2025-11-27T19:00:56.000Z at process.processTicksAndRejections (node:internal/process/task_queues:82:21) { 2025-11-27T19:00:56.000Z cause: Error: Client network socket disconnected before secure TLS connection was established 2025-11-27T19:00:56.000Z at TLSSocket.onConnectEnd (node:_tls_wrap:1730:19) 2025-11-27T19:00:56.000Z at TLSSocket.emit (node:events:536:35) 2025-11-27T19:00:56.000Z at TLSSocket.emit (node:domain:489:12) 2025-11-27T19:00:56.000Z at endReadableNT (node:internal/streams/readable:1698:12) 2025-11-27T19:00:56.000Z at process.processTicksAndRejections (node:internal/process/task_queues:82:21) { 2025-11-27T19:00:56.000Z code: 'ECONNRESET', 2025-11-27T19:00:56.000Z path: undefined, 2025-11-27T19:00:56.000Z host: 'ac-4dtjvxt-shard-00-02.hcr3a.mongodb.net', 2025-11-27T19:00:56.000Z port: 27017, 2025-11-27T19:00:56.000Z localAddress: undefined 2025-11-27T19:00:56.000Z }, 2025-11-27T19:00:56.000Z connectionGeneration: 0, 2025-11-27T19:00:56.000Z [Symbol(errorLabels)]: Set(1) { 'ResetPool' } 2025-11-27T19:00:56.000Z } 2025-11-27T19:00:56.000Z Node.js v20.19.5 2025-11-27T19:00:56.012Z [nodemon] app crashed - waiting for file changes before starting...

asked a year ago272 views

2 Answers
0

Based on your logs, you're experiencing a MongoDB connection issue from your ECS container with the error "MongoNetworkError: Client network socket disconnected before secure TLS connection was established" and "ECONNRESET". This indicates a network connectivity problem between your ECS container and the MongoDB Atlas cluster.

Here are several potential causes and solutions:

  1. Network Configuration Issues:
  • Check if your ECS container is running in a private subnet. If so, ensure you have a properly configured NAT Gateway in a public subnet to route outbound traffic to the internet.
  • Verify that the route tables for your private subnets include a route to the NAT Gateway for internet-bound traffic (0.0.0.0/0).
  1. Security Group Configuration:
  • Ensure your security group allows outbound traffic to all destinations (0.0.0.0/0) on the necessary ports. MongoDB Atlas typically requires outbound access on ports 27015-27017.
  • Double-check that any inbound rules are properly configured if needed.
  1. MongoDB Atlas Whitelist:
  • Verify that the IP address of your NAT Gateway (if using one) or your ECS environment's public IP is whitelisted in MongoDB Atlas.
  • You could temporarily whitelist all IPs (0.0.0.0/0) for testing purposes.
  1. TLS Configuration:
  • The error specifically mentions a TLS connection issue. Ensure you're using the correct CA certificate and that TLS is properly configured in your connection string.
  • For MongoDB Atlas, make sure your connection string includes the necessary TLS options.
  1. Connection String:
  • Double-check that you're using the correct MongoDB Atlas connection string format in your application's environment variables.
  • The format should be something like: mongodb+srv://username:password@cluster.mongodb.net/dbname
  1. DNS Resolution:
  • Ensure that your ECS environment can resolve the DNS name of the MongoDB Atlas cluster.
  1. Network ACLs:
  • Check if there are any Network ACLs in your VPC that might be blocking outbound traffic to MongoDB Atlas.

Since this was working previously and recently started failing, consider what changes might have occurred:

  • Has your MongoDB Atlas configuration changed?
  • Have there been any recent updates to your VPC, security groups, or network settings?
  • Has your application code or connection string been modified?

To further diagnose the issue, you could enable VPC Flow Logs to analyze the network traffic and identify any potential blockages.
Sources
Connection Error With MongoDB Atlas Via ECS | AWS re:Post
Next.js App Deployed with Elastic Beanstalk Unable to Connect to MongoDB Atlas | AWS re:Post

answered a year ago

EXPERT

reviewed a year ago

0

Thanks for the suggestions. I checked all the above points most of it is validated except for point 6. So I think the issue is with DNS resolution. When checked from within the ECS task using curl -v https://stagedatabase.*******6au.mongodb.net → I get "Could not resolve host" and ping also reports “Name or service not known”.

answered 10 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.