简体   繁体   中英

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

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM