2

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?

3
  • Did you try to connect MongoDB by using some mongo client like mongo compass installed on your home pc? Commented Aug 31, 2022 at 8:14
  • @nipuna No. I am using the "default" setup. I am using Mongo Atlas, and just simply connect to it with the example snippets. Commented Aug 31, 2022 at 10:03
  • Please do try to connect to your server using the mongosh client tool from the same computer. Commented Sep 5, 2022 at 5:54

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

It connects perfectly 10/9 times. Whitelist feature also turned off.
@rihekopo, so does " It happens always" or "It connects perfectly 10/9 times" ? All works for me now. You really need to enable whitelisting. If it is free tier, it's multi-tenant and you are at mercy of other users who you share ec2 resources with.
@AlexBlex It works fine 10/9, but when it happens, it is always at the same line of code I added. I am on a paid plan.
@rihekopo, paid plans are entitled to mongo support. Check if you did not exhaust connection quota, CPU usage, elections on Atlas, and reach out to support to investigate.
0

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.

Comments

-2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.