Home > database >  Why can I connect to Mongo in node using 127.0.0.1, but not localhost?
Why can I connect to Mongo in node using 127.0.0.1, but not localhost?

Time:07-28

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?

CodePudding user response:

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