MongooseServerSelectionError: connect ECONNREFUSED

不打扰是莪最后的温柔 提交于 2020-12-13 03:24:36

问题


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

Hope expert will help me.


回答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 ec2-user@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 ec2-user@10.0.8.10

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



来源:https://stackoverflow.com/questions/62774092/mongooseserverselectionerror-connect-econnrefused

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!