Can not connect to mongodb locally: Server selection timed out after 30000 ms

Welcome to the MongoDB Community @mahmoud_abdelfatah!

If a direct connection works but a replica set connection does not, I expect your replica set configuration has hostnames that cannot be resolved locally.

Per the MongoDB Server Discovery and Monitoring (SDAM) spec, Clients use the hostnames listed in the replica set config.

A client using a replication set connection must be able to connect to members of the replica set using the configured hostnames/IPs and ports. A workaround might be adding hostnames to local DNS resolution via /etc/hosts or an equivalent for your O/S.

Regards,
Stennie

1 Like