[Solved] MongooseServerSelectionError: connect ECONNREFUSED

I’m trying to connect my remote EC2 mongodb but it’s saying MongooseServerSelectionError: connect ECONNREFUSED awsip:27017

In aws mongodb config file I change bindIp to 0.0.0.0 but I didn’t change the security section.

This is an express application I have try to connect 2 way with aws mongodb and the both way I got the same error, here is my db setup:

Setup: 01

const DATABASE_URL = `mongodb://${cfg.dbUser}:${cfg.dbPass}@${cfg.dbHost}:${cfg.dbPort}/${cfg.dbName}`

server.listen(port, () => {
  mongoose.connect(DATABASE_URL,
    {
      // auth: {
      //   user: cfg.dbUser,
      //   password: cfg.dbPass
      // },
      useNewUrlParser: true,
      useUnifiedTopology: true,
    })
    .then(() => {
      console.log("[ ? ] AWS MongoDB database connected.");
      console.log(`[ Server ] waiting on: ${cfg.serverHost}:${port}`);
    })
    .catch(err => {
      console.log(
        "[ ? ] Database connection error",
        { Error: err }
      );
    })
});


Setup: 02

const DATABASE_URL = `mongodb://${cfg.dbHost}:${cfg.dbPort}/${cfg.dbName}`
server.listen(port, () => {
  mongoose.connect(DATABASE_URL,
    {
      auth: {
        user: cfg.dbUser,
        password: cfg.dbPass
      },
      useNewUrlParser: true,
      useUnifiedTopology: true,
    })
    .then(() => {
      console.log("[ ? ] AWS MongoDB database connected.");
      console.log(`[ Server ] waiting on: ${cfg.serverHost}:${port}`);
    })
    .catch(err => {
      console.log(
        "[ ? ] Database connection error",
        { Error: err }
      );
    })
});

I have created database in aws and all the database user permission credential.

I’m using dotenv to load all the aws credintial

Also the same problem when I’m trying to connect with MongoDB-Compass from my machine.

connect ECONNREFUSED awsip:27017

Here is my terminal picture
enter image description here

Hope expert will help me.

Solution #1:

The solution to this is ssh port forwarding.

First make sure you can ssh into your mongo instance server and access your primary and secondary replica Nodes

Then create a new inbound rule for your security group with these setups on the mongo server with a public IPv4 address

  1. Custom TCP 8000 0.0.0.0/0
  2. Custom TCP 8000 ::/0

For the port forwarding part

SSH Port forwarding (SSH tunnel) creates a connection between a port on your current machine to a port on another server

here is an example

ssh -i aws-ssh-key.pem -g -N -f -L 8000:127.0.0.1:27017 [email protected]10.0.8.10

when you initiate this command, what it does is open and connect a local port from the current machine 8000 to your mongo server:port 127.0.0.1:27017 with the username and address [email protected]

for the flags -g, -N, -f, -L

-g Allows remote hosts to connect to local forwarded ports.

-N Do not execute a remote command. to prevent ssh from opening a shell on the server

-f to run ssh in the background.

-L specify local port to use

Run this command to list the process

ps aux | grep ssh

Run this command to kill the process anytime

kill -9 <pid>

I hope this answers your question for more references

https://linux.die.net/man/1/ssh

https://www.youtube.com/watch?v=JKrO5WABdoY

https://jasonwatmore.com/post/2020/02/05/connect-to-remote-mongodb-on-aws-ec2-simply-and-securely-via-ssh-tunnel

Respondent: i5dr0id

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.