Hi,
I tried an app which connects to MongoDB with Go1.5 and it failed to find the servers ("no reachable servers" error). System is MacOS X 10.10, brew-installed MongoDB with default options, MongoDB host is "localhost". And the same code works with Go 1.4.
The problem appears to be in IPv4/IPv6 addresses resolving. /etc/hosts (autogenerated on mac) file looks like this:
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
MongoDB listens only on local IPv4 address, and, according to wireshark output, mgo client tries to connect to ::1.
If I comment out those two IPv6 lines in /etc/hosts, it start working. Also, if I change "localhost" to "127.0.0.1" it also works.
Debug shows that it uses cgo resolver:
$ GODEBUG=netdns=1 ./myapp
go package net: using cgo DNS resolver
...
panic: no reachable servers
This simple code repeats the problem, given you run it on MacOS X 10.10 with MongoDB w/ default config. One person in Slack chat already confirmed that it has the same behavior.
package main
import (
"log"
"time"
)
func main() {
session, err := mgo.DialWithTimeout("localhost", 1*time.Second)
if err != nil {
log.Fatal(err)
}
defer session.Close()
err = session.Ping()
if err != nil {
log.Fatal(err)
}
}
I tried to repeat this issue with simple net.Dial(), but it works correctly, so have to be something related to mgo host resolving. But, still, it works find with Go 1.4.
Any thoughts?