1

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.

Cit5
  • 400
  • 5
  • 19
  • 1
    try checking the error returned from mgo.ParseURL, this example is ignoring any error that may be returned. Also, try to `telnet IP:PORT` to make sure you can connect to the target host/port – David Budworth Oct 15 '17 at 17:52
  • updated the code and same error is returned. telnet just stalls. My AWS settings have the port open, not sure what is going on. – Cit5 Oct 18 '17 at 00:39

1 Answers1

3

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

Wan B.
  • 18,367
  • 4
  • 54
  • 71
  • I fixed this a while ago but that must have been the issue: having a bad mgo URI format. thanks for answering! – Cit5 Nov 06 '17 at 05:20