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!