Go and MongoDB connection won't work with panic log “no reachable server”

雨燕双飞 提交于 2020-01-05 05:10:33

问题


I am using mGo as a driver for my Go Web App to another MongoDB system. So I am not running Mongo on the same system. (URL is not localhost).

However, I get "panic: no reachable servers" error.

Here is the test function that runs right when the Go server starts:

dialInfo, err0 := mgo.ParseURL("mongodb://1234MY456IP:27017,27018")
if err0 != nil {
    panic(err0)
}
dialInfo.Direct = true
dialInfo.FailFast = true
session, err := mgo.DialWithInfo(dialInfo)
if err != nil {
    panic(err)
}
defer session.Close()

One of the answers in a similar question was to make sure MongoDB is running on netstat or ps.

I don't see any processes running on ps except ps and bash. And I don't see it on netstat either.

That being I've ran sudo service mongod start/stop/restart and tested with the mongo shell. What am I doing wrong?

Err0 and err print the same error message.


回答1:


First, the URI should follow the mgo URI format

[mongodb://][user:pass@]host1[:port1][,host2[:port2],...][/database][?options]

See mgo.v2 Dial func for more information.

If you're connecting to MongoDB Atlas (or your servers require SSL) see also Connecting to MongoDB Atlas using mgo

Second, make sure you can reach the MongoDB server(s) from your application server. You can utilise mongo shell to test the connection (eliminating your code issue for now).



来源:https://stackoverflow.com/questions/46757753/go-and-mongodb-connection-wont-work-with-panic-log-no-reachable-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!