2

I am trying to connect to MongoDB from my Go application but getting an error with ReplicaSetNoPrimary.

Here is my code connection to MongoDB:

exmple of MongoDB URI:

mongodb+srv://user:[email protected]/?retryWrites=true&w=majority&appName=ntw
func SetupDB(uri string) *mongo.Client {
    log.Printf("Connecting to MongoDB")
    log.Printf("URI: %s", uri)
    ctx := context.TODO()
    client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
    if err != nil {
        log.Fatalf("Failed to connect to MongoDB: %v", err)
    }

    defer func() {
        if err := client.Disconnect(ctx); err != nil {
            log.Fatalf("Failed to disconnect from MongoDB: %v", err)
        }
    }()

    if err := client.Ping(ctx, readpref.Primary()); err != nil {
        log.Fatalf("Failed to ping MongoDB: %v", err)
    }

    log.Printf("Connected to MongoDB")
    return client
}

Here is the error I am getting:

Failed to ping MongoDB: server selection error: server selection timeout, current topology:

{ Type: ReplicaSetNoPrimary,
Servers: [{ Addr: ac-l4ofxbw-shard-00-00.55n9jaw.mongodb.net:27017, 
Type: Unknown,
Last error: tls: failed to verify certificate: x509: certificate has expired or is not yet valid:  },
{ Addr: ac-l4ofxbw-shard-00-01.55n9jaw.mongodb.net:27017,
Type: Unknown, 
Last error: tls: failed to verify certificate: x509: certificate has expired or is not yet valid:  }, 
{ Addr: ac-l4ofxbw-shard-00-02.55n9jaw.mongodb.net:27017, 
Type: Unknown, 
Last error: tls: failed to verify certificate: x509: certificate has expired or is not yet valid:  }, ] }

3
  • Also, I added all IP's for Network connections in MongoDB Atlas Commented Jul 13, 2024 at 7:24
  • 1
    failed to verify certificate: x509: certificate has expired or is not yet valid - this error makes impossible to use any of 3 visible servers, therefore no primary exception is thrown, you should fix it. Since it's atlas, check this support links mongodb.com/community/forums/t/… and mongodb.com/community/forums/t/… Commented Jul 13, 2024 at 17:57
  • If you resolved the issue please post an answer to help future developers that might have similar error messages. Commented Jul 14, 2024 at 7:58

1 Answer 1

1

Problem resoved with adjusting correct timezone on local machine

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

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

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