mongodbgosslreplication

MongoDB connection error with Go, ReplicaSetNoPrimary


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:pass@ntw.55n9jaw.mongodb.net/?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:  }, ] }


Solution

  • Problem resoved with adjusting correct timezone on local machine