doulongdan2264 2019-08-23 20:43
浏览 127

MongoDB Atlas Golang mgo软件包“无法访问的服务器”错误

I continuously get the following error when attempting to connect to my MongoDB Atlas database.

no reachable servers
exit status 1

I am running MongoDB version 3.4, which is compatible with mgo. I have whitelisted all IP addresses on MongoDB Atlas, including my own.
I have created a user named 'root' on MongoDB Atlas with admin rights.
I am running on Ubuntu 18.04, and I have set my DNS Nameserver to 8.8.8.8.
I am using the Community supported version of mgo from github.com/globalsign/mgo.
My code is as follows:

package main

import (
    "fmt"
    "log"

    "github.com/globalsign/mgo"
    "github.com/globalsign/mgo/bson"
)

func main() {
    database := "bookstore"
    collection := "books"
    user := "root"
    pword := "root"
    url := "mongodb://" + user + ":" + pword + "@<prefix>.mongodb.net:27017/" + database + "?w=majority"

    session, err := mgo.Dial(url)
    if err != nil {
        log.Fatal(err)
    } else {
                fmt.Println("Success.")
        }
}

I have looked at the following questions. These do not solve my problem:
CreateSession: no reachable servers - mgo
How to solve panic: no reachable servers
Connecting to MongoDB Atlas using Golang mgo: Persistent no reachable server to replica set
Golang / MGO -- panic: no reachable servers
No reachable servers on static linked go binary
Go and MongoDB connection won't work with panic log "no reachable server"
No reachable servers on static linked go binary
How to use new URL from mongodb 3.6 to connect from golang
How to connect to remote Mongodb server
Any help is truly appreciated!

  • 写回答

1条回答 默认 最新

  • doucuoyan0426 2019-08-26 03:27
    关注

    One issue is the MongoDB Connection String URI that you're using to connect to the Atlas cluster.

    There are two types of MongoDB connection string format:

    The DNS seedlist format is shorter because it leverages discovery of available servers using DNS. In order to leverage the DNS seedlist, use a connection string prefix of mongodb+srv: rather than the standard mongodb:.

    Based on your posted snippet, it seems that you're mixing the two formats. If you're trying to use the DNS seedlist format, you need to specify mongodb+srv:, if you're trying to use the standard format, you need to specify the other hosts. For example:

    "mongodb://FOO-shard-00-00-BAR.mongodb.net:27017,FOO-shard-00-01-BAR.mongodb.net:27017,FOO-shard-00-02-BAR.mongodb.net:27017/test?replicaSet=FOO-shard-0"
    

    On that note, you're also using globalsign/mgo driver. Currently, it does not support the DNS seedlist format, see globalsign/mgo/issues#112. Also, globalsign/mgo currently only has experimental support for MongoDB v4.0+.

    You can try to use mongo-go-driver instead if you're intending to use the DNS seedlist connection format, and/or using MongoDB v3.6+ (At the moment latest version of MongoDB is v4.2)

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?