rihekopo
rihekopo

Reputation: 3350

Mongodb server selection error due network timeout

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?

Upvotes: 2

Views: 9248

Answers (3)

Anjan Karmakar
Anjan Karmakar

Reputation: 31

In Network Access set a new IP address to 0.0.0.0/0

Upvotes: -2

Rachelle Palmer Frick
Rachelle Palmer Frick

Reputation: 71

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.

Upvotes: 0

Malik Mussabeheen Noor
Malik Mussabeheen Noor

Reputation: 864

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

Upvotes: 2

Related Questions