如何连接到远程Mongodb服务器

时间:2018-06-09 06:06:13

标签: mongodb go remote-server

我有两个EC2实例。在一个实例上,我正在运行mongodb,而在其他实例上,我正在运行Golang代码,以便测试Mongodb连接。以下是Go代码:

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

在Mongodb中,我启用了安全授权,并将bindIp更改为0.0.0.0。但它给了我以下错误:

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

任何人都可以告诉我应该如何解决它? 谢谢!

0 个答案:

没有答案
相关问题