doujing9972 2018-06-09 06:06
浏览 213

如何连接到远程Mongodb服务器

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!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥35 平滑拟合曲线该如何生成
    • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
    • ¥15 名为“Product”的列已属于此 DataTable
    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集