getaddrinfo ENOTFOUND mongodb.railway.internal

ragsntatters
HOBBY

2 months ago

I've confirmed that my backend service is attempting to connect to mongodb.railway.internal:27017 using the MONGO_URL environment variable, which appears correctly set in my backend's environment variables. Despite this, I'm consistently receiving getaddrinfo ENOTFOUND mongodb.railway.internal errors

Logs:
Starting Container [DEBUG] URI Source: Using MONGO_URL from environment. [DEBUG] Final MONGODB_URI being used for connection: mongodb://[user]:[pass]@mongodb.railway.internal:27017 Attempting to connect to MongoDB (5 retries left)... Starting backend service... Environment: { NODE_ENV: 'production', PORT: '8080', DOMAIN: undefined, FRONTEND_URL: undefined, MONGODB_URI: '***missing***', CLERK_SECRET_KEY: '***exists***' } (node:1) [DEP0040] DeprecationWarning: The punycode module is deprecated. Please use a userland alternative instead. (Use node --trace-deprecation ... to show where the warning was created) Backend running on port 8080 Server started successfully CORS enabled for: [ undefined, 'https://pa11y.wookongmarketing.com';, 'https://pa11y-backend.wookongmarketing.com';, 'http://localhost:3000'; ] 2025-06-03T23:24:41.476Z - GET /health - no origin Health check requested MongoDB disconnected, attempting to reconnect... MongoDB connection error: getaddrinfo ENOTFOUND mongodb.railway.internal Retrying in 5 seconds... (4 retries left) MongoDB connection error: MongoServerSelectionError: getaddrinfo ENOTFOUND mongodb.railway.internal at Timeout._onTimeout (/app/node_modules/mongodb/lib/sdam/topology.js:278:38) at listOnTimeout (node:internal/timers:594:17) at process.processTimers (node:internal/timers:529:7) { reason: TopologyDescription { type: 'Unknown', servers: Map(1) { 'mongodb.railway.internal:27017' => [ServerDescription] }, stale: false, compatible: true, heartbeatFrequencyMS: 10000, localThresholdMS: 15, setName: null, maxElectionId: null, maxSetVersion: null, commonWireVersion: 0, logicalSessionTimeoutMinutes: null }, code: undefined, [Symbol(errorLabels)]: Set(0) {} }

Solved$10 Bounty

14 Replies

ragsntatters
HOBBY

2 months ago

Attempting to connect to MongoDB (1 retries left)...

MongoDB disconnected, attempting to reconnect...

MongoDB connection error: getaddrinfo ENOTFOUND mongodb.railway.internal

Failed to connect to MongoDB after all retries

Initial MongoDB connection failed: MongooseServerSelectionError: getaddrinfo ENOTFOUND mongodb.railway.internal

at handleConnectionErrors (/app/nodemodules/mongoose/lib/connection.js:816:11)

at NativeConnection.openUri (/app/node_modules/mongoose/lib/connection.js:791:11)

at async file:///app/utils/db.js:97:9 {

reason: TopologyDescription {

type: 'Unknown',

servers: Map(1) { 'mongodb.railway.internal:27017' => [ServerDescription] },

stale: false,

compatible: true,

heartbeatFrequencyMS: 10000,

localThresholdMS: 15,

setName: null,

maxElectionId: null,

maxSetVersion: null,

commonWireVersion: 0,

logicalSessionTimeoutMinutes: null

},

code: undefined

}


2 months ago

This thread has been marked as public for community involvement, as it does not contain any sensitive or personal information. Any further activity in this thread will be visible to everyone.

Status changed to Open brody about 2 months ago


2 months ago

Your backend can’t find mongodb.railway.internal because that hostname only works between services inside the same Railway project. If you’re running locally or in a different project, use the public MongoDB URL instead. Make sure both services are in the same project and deployed on Railway for the internal address to work.


testuser123

Your backend can’t find mongodb.railway.internal because that hostname only works between services inside the same Railway project. If you’re running locally or in a different project, use the public MongoDB URL instead. Make sure both services are in the same project and deployed on Railway for the internal address to work.

ragsntatters
HOBBY

2 months ago

Hey all services are in the same project and I'm using a custom URL instead of the default railway one. The setup of that URL is marked as correct so I'm unsure why the db thinks it's not in the same project.


ragsntatters
HOBBY

2 months ago

Is there somewhere I should be checking to see where mongodb thinks it is in relation to the backend service?


ragsntatters

Hey all services are in the same project and I'm using a custom URL instead of the default railway one. The setup of that URL is marked as correct so I'm unsure why the db thinks it's not in the same project.

2 months ago

If you’re still getting ENOTFOUND for mongodb.railway.internal, I'd double-check these:

  • Make sure your backend is actually deployed on Railway (not running locally, which the local host might indicate).

  • If you’re using a custom domain or URL for MongoDB, which apparently you are, try switching back to the default internal hostname to test.

  • Restart both the backend and MongoDB services (sometimes DNS takes a minute to sync up)

  • Check your backend’s logs and environment variables at runtime to confirm it’s using the right MONGO_URL.

If it still fails, try connecting to the MongoDB service from a Railway shell in your backend service. If that works, the problem is likely in your app config or how the env vars are loaded.


testuser123

If you’re still getting ENOTFOUND for mongodb.railway.internal, I'd double-check these:Make sure your backend is actually deployed on Railway (not running locally, which the local host might indicate).If you’re using a custom domain or URL for MongoDB, which apparently you are, try switching back to the default internal hostname to test.Restart both the backend and MongoDB services (sometimes DNS takes a minute to sync up)Check your backend’s logs and environment variables at runtime to confirm it’s using the right MONGO_URL.If it still fails, try connecting to the MongoDB service from a Railway shell in your backend service. If that works, the problem is likely in your app config or how the env vars are loaded.

2 months ago

Please refrain from using AI Answers.


ragsntatters
HOBBY

2 months ago

Hey ya they're not local and my backend and frontend use custom domains but not my MongoDB. I've tried the restarts and it seems to know the correct URL it just can't connect to it


ragsntatters

Hey ya they're not local and my backend and frontend use custom domains but not my MongoDB. I've tried the restarts and it seems to know the correct URL it just can't connect to it

2 months ago

Have you tried switching back to the default internal hostname to test the connection?


ragsntatters
HOBBY

2 months ago

yup same issue thanks


ragsntatters

yup same issue thanks

2 months ago

What about the shell? Try connecting to the DB from there.


testuser123

What about the shell? Try connecting to the DB from there.

ragsntatters
HOBBY

2 months ago

I can't find a Terminal in Railway , where might it live? Or should I be using CLI and login to Railway through there and try?


ragsntatters

I can't find a Terminal in Railway , where might it live? Or should I be using CLI and login to Railway through there and try?

Install the Railway CLI: https://docs.railway.com/guides/cli

Type railway link.

Right click the service and click "Copy SSH Command"

Paste that into your CLI.


ragsntatters
HOBBY

2 months ago

cool thanks yeah so the Public URL I can use to connect to it and it returns the db test>
However I can't connect my backend to the db (which are in the same project) using the non public URL


ragsntatters
HOBBY

2 months ago

k figured it out the issue was the code was looking for an ipv4 but mongo had an ipv6


Status changed to Solved chandrika about 2 months ago