Home > Back-end >  localhost defaults to ipv6 address instead of ipv4 on fedora linux. Can't connect to mongodb fr
localhost defaults to ipv6 address instead of ipv4 on fedora linux. Can't connect to mongodb fr

Time:12-16

After a system update on Fedora 36 Linux, Localhost alias now uses the ipv6 address by default which has broken a lot of my code. For instance:

// Connecting to MongoDB using mongoose on express (nodejs)
mongoose.connect('mongodb://localhost:27017/farmStand')
.then(()=>{
    console.log('Connected to Mongo');
}) 

Does not work and throws this error:

MongooseServerSelectionError: connect ECONNREFUSED ::1:27017

I've done some research and found multiple workarounds such as replacing localhost with 127.0.0.1, localhost4 or even removing the following line,

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

from from /etc/hosts,

# Loopback entries; do not change.
# For historical reasons, localhost precedes localhost.localdomain:
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
# See hosts(5) for proper format and other examples:
# 192.168.1.10 foo.mydomain.org foo
# 192.168.1.13 bar.mydomain.org bar

However since this the former forces me to change a lot of code and the later is not recommended by the hosts file itself, How do i return localhost to its former state or bind mongodb to ipv6?

CodePudding user response:

Start your MongoDB with this settings:

net:
  bindIpAll: true
  ipv6: true

Then it should work

  • Related