Mongodb服务器选择错误,由于网络超时。

huangapple go评论89阅读模式
英文:

Mongodb server selection error due network timeout

问题

我有一个使用Mongo DB的Go程序。我从我的家用电脑上运行它,但经常遇到以下错误:

panic: 服务器选择错误:服务器选择超时,当前拓扑:{ 类型:ReplicaSetNoPrimary,服务器:[{ 地址:cluster0-shard-00-00.tvm1o.mongodb.net:27017,类型:未知,最后错误:connection() error occurred during connection handshake: dial tcp 3.216.112.85:27017: i/o timeout }, { 地址:cluster0-shard-00-01.tvm1o.mongodb.net:27017,类型:未知,最后错误:connection() error occurred during connection handshake: dial tcp 34.197.85.254:27017: i/o timeout }, { 地址:cluster0-shard-00-02.tvm1o.mongodb.net:27017,类型:未知,最后错误:connection() error occurred during connection handshake: dial tcp 18.206.5.2:27017: i/o timeout }, ] }

以下是出错的确切代码:

if err := clientMongo.Ping(context.TODO(), readpref.Primary()); err != nil {
	panic(err)
}

我理解这是一个连接超时错误,但我不明白在简单的客户端连接过程中为什么会发生这种情况。我进行了速度测试,当前的上传速度为22 Mbps,我没有上传大型JSON数组或其他任何东西。每次尝试连接客户端时都会发生这种情况。所以我想知道这可能是因为我的互联网连接还是Mongo的问题?

英文:

I have a Go program which uses Mongo DB. I run it from my home pc and I keep getting this error very often:

> panic: server selection error: server selection timeout, current
> topology: { Type: ReplicaSetNoPrimary, Servers: [{ Addr:
> cluster0-shard-00-00.tvm1o.mongodb.net:27017, Type: Unknown, Last
> error: connection() error occurred during connection handshake: dial
> tcp
> 3.216.112.85:27017: i/o timeout }, { Addr: cluster0-shard-00-01.tvm1o.mongodb.net:27017, Type: Unknown, Last
> error: connection() error occurred during connection handshake: dial
> tcp 34.197.85.254:27017: i/o timeout }, { Addr:
> cluster0-shard-00-02.tvm1o.mongodb.net:27017, Type: Unknown, Last
> error: connection() error occurred during connection handshake: dial
> tcp 18.206.5.2:27017: i/o timeout }, ] }

And this is the exact code where it breaks:

if err := clientMongo.Ping(context.TODO(), readpref.Primary()); err != nil {
	panic(err)
}

I understand this is a connection timeout, but I don't understand how can this happen at all during a simple client connection. I made a speedtest and my current upload speed is 22 Mbps, I am not uploading big json arrays or anything. It happens always when I try to connect to the client. So I would like to know if this can be caused because my internet connection or something on Mongo's end?

答案1

得分: 1

你可能需要将你的 IP 添加到 MongoDB 的白名单中。

英文:

You might need to add your IP to the whitelist of MongoDB.

答案2

得分: 0

几件事情--

  1. 我们需要看到创建连接的完整代码。我假设你正在使用文档中的代码这里?
  2. 你应该尝试使用mongosh和Compass进行连接。如果你在使用其他工具时遇到问题,那么问题很可能是你的Atlas集群或集群上的安全设置,而不是你的应用程序代码。
  3. 话虽如此,大约95%的情况下问题出在白名单或数据库用户上。你在UI的数据库访问区域创建了一个具有管理员/读/写任何数据库权限的用户吗?你的IP地址在白名单中吗?

如果第3点没问题,第2点还是不行,你可以在MongoDB的UI界面上的绿色聊天按钮中获得免费的Atlas支持。

英文:

A few things --

  1. we would need to see the complete code for creating a connection. I'm going to assume you're using exactly what is in the documentation here?
  2. You should try to connect with mongosh and Compass also. If you have problems with another tool, then the odds are it is your Atlas cluster OR your security settings on the cluster, rather than your application code.
  3. that being said about 95% of the time the issue is whitelist or database users. do you have a user created in database access area of the UI that has admin/read/write any database? is your IP in the whitelist?

if 3 is good, and 2 doesn't work, there is free Atlas support available in the green chat button of the UI in MongoDB.

huangapple
  • 本文由 发表于 2022年8月31日 15:26:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/73552273.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定