MongoDB connects with public url, but no private
evertith
PROOP

9 months ago

I have a NodeJS server with Mongoose. It connects to the MongoDB server just fine if I use the MONGO_PUBLIC_URL, but if I use the MONGO_URL, it says it can't connect.

$10 Bounty

8 Replies

lofimit
HOBBY

9 months ago

You are probably trying to use the private MONGO_URL from outside of Railway (like your local machine), and that won't work because it only works inside Railway's network. So if your Node server isn't running on Railway, it can't connect to the private URL.
In case your app is deployed on Railway, make sure your Node service is linked to the Mongo service and then use the private URL there.
Also, sometimes just restarting your Node service on Railway after linking fixes some DNS/internal network issues.

If that's not the case, let me know and I will try to help further! smile emoji


lofimit

You are probably trying to use the private MONGO_URL from outside of Railway (like your local machine), and that won't work because it only works inside Railway's network. So if your Node server isn't running on Railway, it can't connect to the private URL.In case your app is deployed on Railway, make sure your Node service is linked to the Mongo service and then use the private URL there.Also, sometimes just restarting your Node service on Railway after linking fixes some DNS/internal network issues.If that's not the case, let me know and I will try to help further!

evertith
PROOP

9 months ago

Yeah, in my workspace I have the node server setup. In the same workspace, I have a MongoDB instance. I have my MONGO_URL variable on the NodeJS service set to ${{MongoDB-dev.MONGO_URL}}, which is the internal URL for my MongoDB service. When I restart the NodeJS server, it says it can't connect to MongoDB. If I change my MONGO_URL to ${{MongoDB-dev.MONGO_PUBLIC_URL}}, which is the public URL, it works fine, but I have to deal with egress charges, so I need to figure out why public works, but internal doesn't.


evertith

Yeah, in my workspace I have the node server setup. In the same workspace, I have a MongoDB instance. I have my MONGO_URL variable on the NodeJS service set to ${{MongoDB-dev.MONGO_URL}}, which is the internal URL for my MongoDB service. When I restart the NodeJS server, it says it can't connect to MongoDB. If I change my MONGO_URL to ${{MongoDB-dev.MONGO_PUBLIC_URL}}, which is the public URL, it works fine, but I have to deal with egress charges, so I need to figure out why public works, but internal doesn't.

lofimit
HOBBY

9 months ago

Could you tell me if they are running in the same Railway environment? If not, that is the problem.


lofimit

Could you tell me if they are running in the same Railway environment? If not, that is the problem.

evertith
PROOP

9 months ago

Yes, they are both in the development environment I have setup. I had to setup another NodeJS server, and it's having the same issue. Both servers require the public URL of the mongo instance in order for them to connect to mongo.

Attachments


evertith

Yes, they are both in the development environment I have setup. I had to setup another NodeJS server, and it's having the same issue. Both servers require the public URL of the mongo instance in order for them to connect to mongo.

lofimit
HOBBY

9 months ago

All I can think of is that your NodeJS service isn’t properly linked to the MongoDB service inside Railway. Even if they’re in the same environment, you gotta make sure you link the MongoDB plugin to your NodeJS service, otherwise the private URL won’t work. Also, after linking, restart or redeploy your both services so it picks up the internal network and DNS.


9 months ago

Hello,

Can you confirm at which point your node service tries to connect to the db


angobello

Hello,Can you confirm at which point your node service tries to connect to the db

evertith
PROOP

9 months ago

Hey. It connects right after ExpressJS is initialized.


9 months ago

Can you share any error logs you see


Loading...