MongooseServerSelectionError: connect ECONNREFUSED

So I wanted to try making like a blog website with Node js and MongoDB. I’m a beginner with it so it’s kinda a mess, but everything worked fine locally (on localhost 3000). I found Railway as a free alternative to Heroku and tried to deploy my project there.

For some reason it failed to connect to the database, and this is what I got in the logs.

Here is a small piece of code from app.js

What could be the problem? I will share more code if it is needed.

Welcome to the MongoDB Community @zmarko !


This error message indicates a local MongoDB server isn’t listening on (all local IPv4 addresses on local machine), port 27017.

By default, a running MongoDB server will only bind to localhost (‘’), so you could try changing your connection string to use the localhost IP (or localhost hostname) instead.

Alternatively, if your app server is running on a separate host from MongoDB, you could configure your MongoDB process to bind to all IPs. However, please review the MongoDB Security Checklist and configure appropriate security measures before allowing remote access.

If you haven’t installed a MongoDB server yet, please follow the appropriate MongoDB Installation Tutorial or consider using a hosted service like MongoDB Atlas.