0

I have two EC2 instances. On one instance I am running mongodb and on other instance I am running Golang code so as to test the Mongodb connection. Following is the Go code:

    package main 

    import (
        "gopkg.in/mgo.v2"
    "time"
    "fmt"
)

    func main() {
        mongoDBDialInfo := &mgo.DialInfo{
            Addrs:    []string{"DBSERVERIP:27017"},
            Timeout:  60 * time.Second,
            Database: "my_db",
            Username: "myusername",
            Password: "mypassword",
        }

        mongoSession, err := mgo.DialWithInfo(mongoDBDialInfo)
        if err != nil {
            fmt.Println("CreateSession: ", err)
        }
        mongoSession.SetMode(mgo.Monotonic, true)

        fmt.Println(mongoSession)
    }

In Mongodb, I have enabled security authorization and changed the bindIp to 0.0.0.0. But its giving me following error:

CreateSession:  no reachable servers
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x53f207]

goroutine 1 [running]:
gopkg.in/mgo%2ev2.(*Session).SetMode(0x0, 0x1, 0x1)
        /home/ubuntu/go/src/gopkg.in/mgo.v2/session.go:1681 +0x37
main.main()
        /home/ubuntu/go/src/dbconnect/main.go:27 +0x1a0
exit status 2

Can anybody tell me how should I fix it?? Thanks!

Amandeep kaur
  • 985
  • 3
  • 15
  • 35
  • 1
    Pretty self explanatory error. Ping the servers from your location and test with other tools to see that you can actually reach and talk to them. The error message says that there is no listening server at the current address and port you are using. – Neil Lunn Jun 09 '18 at 06:09
  • If you can see ping is working but your client is still unable to reach the DB, check AWS security. 27017 is a non-standard port and you have to explicitly enable it. This may help you: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/authorizing-access-to-an-instance.html – Soumya Kanti Jun 11 '18 at 09:47

0 Answers0