Why can I connect to Mongo in node using 127.0.0.1, but not localhost? - node.js

I'm connecting to Mongo using the Node library, and mongo is up and running on port 27017.
If I set my uri to mongodb://127.0.0.1:27017 it connects, but if I set it to mongodb://localhost:27017 it doesn't connect (times out).
I'm on Linux, and my /etc/hosts looks like this:
127.0.0.1 localhost
::1 localhost
My guess is it has something to do with ipv6, but I have very little understanding of ipv6 to be honest. Can someone explain what's happening here, and if I should do something differently to be able to connect to localhost?

As you pointed out, it seems that localhost resolves to IPv6 address ::1 and not 127.0.0.1.
You can keep using 127.0.0.1 or another option would be changing the address mongod service binds to, for instance, ::1 (you can bind to IPv4 and IPv6 at the same time).

Related

MongoServerSelectionError: connect ECONNREFUSED ::1:27017

Before updating nodejs my code was working fine but after updating my nodejs version.
whenever i am connecting to the server(localhost), after connecting in a few seconds it gives me this error.
enter image description here
and in vs code console it gives
enter image description here
and this is my code:
enter image description here
I am trying to connect to my local database using the localhost server.
The error message indicates the application is attempting to connect to the IPv6 address ::1, i.e. localhost.
The code you have shows uses IPv4 localhost address 127.0.0.1, and appears to connect successfully at that point.
Some time later, another connection is attempted that goes to the IPv6 address.
This suggests that both:
something later uses localhost instead of an IP, and the operating system resolves that to ::1
mongod is not listening on the IPv6 address
To fix this, you could do one or more of:
modify the mongod.conf so that it listens on ::1 in addition to 127.0.0.1
search the code to find where localhost is used and change it to the IP4 address
modify the local operating system so localhost only resolves to an IP4 address (this is probably a bad idea)

can't telnet via IP but can via localhost

I started an instance on AWS ec2 and am trying to connect via my web browser to the app on the server running on port 3000. I've also turned off iptables...
I can telnet via telnet localhost 3000 and telnet 127.0.0.1 3000 but can't telnet via the hostname or ip like telnet ipaddress 3000.
When i do that, I get a connection refused. I think this has somethign to do with my hosts file but can't figure out what. My host file looks like this:
127.0.0.1 ip-108-205-72-168
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost6 localhost6.localdomain6
Provided that you gave the instance a public ip, have you checked the security groups? AWS security groups are associated with instances and apply inbound/outbound rules.
If you have already done that then my next step would probably be to make sure that the port is bound to the correct interface(s). ss -tupan | grep 3000
please make sure you iptable rules is right and can accessed by ip.
Could you confirm whether you using elastic IP over the amazon VM?
If yes, then it will do the entry in the host file automatically when you associate elastic IP to the EC2.
But if not, then need to do a manual entry.
Thanks,
SIM

ECONNREFUSED when using node with nano and couchdb

I was using nodejs + nano + couchdb for my application successfully up until today. For some reason all of a sudden I'm getting ECONNREFUSED when I try to run my application. If I try to query the database using the web browser or using a different application (java application) it works fine. I'm uncertain why just in this scenario it stopped working. I've been researching for the past 2 days and can't find any help. I believe this might have something to do with too many open connections, but that's a little bit out of my realm of knowledge. Can anyone provide me with any insight on debugging this issue or any direction I could go in? I should mention this couchdb lives on iriscouch
Add more information about stack that you're using. But basically it's server machine doesn't want to allow connecting. Also try run your app with DEBUG=*, nano will log via console.log almost everything.
E.g. change in package.json start command to node changetoyourapp.js DEBUG=*
I faced yesterday same issue with nodejitsu/iriscouch. Issue disappeared after some restarts.
Check the version of your node vs the expected node version of nano. It is possible that nano does not work with node > 16.
This is down to Node v18 now preferring an IPv6 address over and IPv4 address if two exist for the same hostname.
i.e. if your /etc/hosts contains entries like this:
127.0.0.1 localhost
::1 localhost
Node v16 will say that "localhost" resolves to 127.0.0.1 where Node v18 will say "localhost" resolves to ::1, the IPv6 equivalent. As CouchDB doesn't listen on an IPv6 port by default, then a connection to ::1 will be refused.
Solutions:
Use 127.0.0.1 instead of localhost in your URLs.
Use a domain name that resolves unambiguously to an IPv4 address e.g. 127.0.0.1 my.pretend.host in your /etc/hosts file.
Revert to Nodev16 which preferred IPv4 addresses in its dns lookup.
Make CouchDB bind to an IPv6 address by changing bind_address = ::1 in couchdb.ini. You can then do curl 'http://USER:PASS#[::1]:5984/.
See
https://github.com/apache/couchdb-nano/issues/313#issuecomment-1321760360

Why won't localhost route to 127.0.0.1 in chrome (OSX)?

When I launch a script using node-debug, it attempts to navigate to the URL localhost/debug?port=5858 but does not find a page served there.
If I change the "localhost" to 127.0.0.1 everything works fine.
I can ping localhost and it resolves appropriately to 127.0.0.1
Any ideas?
localhost has an IPv6 address (::1) as well as an IPv4 address (127.0.0.1). My guess is that your web server is only serving over IPv4, and chrome is preferring the IPv6 address.
$ dscacheutil -q host -a name localhost
name: localhost
ipv6_address: ::1
ipv6_address: fe80:1::1
name: localhost
ip_address: 127.0.0.1
$ netstat -an | grep "[.]80 .*LISTEN"
tcp46 0 0 *.80 *.* LISTEN
Note the "tcp46" in the last line -- that means the web server is listening for both TCP/IPv4 and TCP/IPv6 connections, If you run the same command, I suspect you'll see just "tcp4".
I'm not familiar with Node.js, but this posting seems to imply you can listen on both localhost addresses by using server.listen(80, '::'). Alternately, you could create separate listeners for the IPv4 and IPv6 addresses, as described here.

How to disable loopback interface in Linux (Fedora)?

So that requests to localhost are treated as if coming from remote host in LAN?
Have you tried updating your /etc/hosts file, replacing:
127.0.0.1 localhost
With your actual IP address? e.g.,
192.168.0.42 localhost
(This doesn't actually disable the loopback address, you can still connect to 127.0.0.1, but connecting to localhost should come in through your network interface, which I believe is what you're asking)

Resources