I want to connect to a remote Mongodb server. And in the process I get the error
selection error: server selection timeout, current topology: { Type: Unknown, Servers: [{ Addr: <addr>:<port>, Type: Unknown, State: Connected, Average RTT: 0, Last error: connection() : dial tcp <addr>:<port>: i/o timeout }, ] }
This is my code,
func Connect() {
client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://server_addr:port").
SetAuth(options.Credential{
AuthSource: "db_name", Username: "user", Password: "password",
}))
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
err = client.Connect(ctx)
//ping db
err = client.Ping(ctx, readpref.Primary())
if err != nil {
log.Fatal("Error while connecting db ", err)
}
log.Println("Connected\n")
}
In the etc/mongod.conf file, I've entered
bindIpAll: true
And to allow port through firewall
sudo firewall-cmd --zone=public --add-port=27017/tcp --permanent
but I still can't understand what is causing the error. I'm still new so It'll be really helpful if you could help me understand. Thanks!