ECONNREFUSED error when connecting to mongodb from node.js

前端 未结 13 1181
不思量自难忘°
不思量自难忘° 2020-11-27 05:51

I know I\'m doing some very stupid and noobish, but I\'m hoping someone can help me set up a basic database connection to mongodb from node.js on a mac.

I\'ve instal

相关标签:
13条回答
  • 2020-11-27 06:38

    I also got stucked with same problem so I fixed it like this :

    If you are running mongo and nodejs in docker container or in docker compose

    so replace localhost with mongo (which is container name in docker in my case) something like this below in your nodejs mongo connection file.

    var mongoURI = "mongodb://mongo:27017/<nodejs_container_name>";
    
    0 讨论(0)
  • 2020-11-27 06:42

    ECONNREFUSED error

    There are few reasons of this error in node :

    1. Your port is already serving some service so it will refuse your connection.

      go to command line and get pid by using following command

      $ lsof -i:port_number

      Now kill pid by using

      $ kill -9 pid(which you will get by above command)

    2. Your server is not running e.g. in this case please check your mongoose server is running or run by using following command.

      $ mongod

    3. There is also possibility your localhost is not configured properly so use 127.0.0.1:27017 instead of localhost.

    0 讨论(0)
  • 2020-11-27 06:49

    I had the same issue. What I did is to run mongodb command in another terminal. Then, run my application in another tab. This resolved my problem. Though, I am trying other solution such as creating a script to run mongodb before connection is made.

    0 讨论(0)
  • 2020-11-27 06:51

    sometimes you need to check the rightfulness of the IP, firewall, port forwarding, etc, if your target database is in other machines.

    0 讨论(0)
  • 2020-11-27 06:53

    very strange, but in my case, i switch wifi connection...

    I use some public wifi and switch to my phone connection

    0 讨论(0)
  • 2020-11-27 06:54

    I had the same issue, all I did was add a setTimeout of about 10 seconds before trying to connect to the Mongo server and it solved the issue right up. I dont know why I had to add a delay but it worked...

    0 讨论(0)
提交回复
热议问题