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

i am trying to test connectivity to my local mongodb through mongo compass, but i still reach out same error, however the service was running properly before.

also, this is the most recent logs for mongodb:

{"t":{"$date":"2022-07-28T15:31:25.696+00:00"},"s":"I",  "c":"NETWORK",  "id":4712102, "ctx":"ReplicaSetMonitor-TaskExecutor","msg":"Host failed in replica set","attr":{"replicaSet":"rs","host":"52.x.x.x:1996","error":{"code":202,"codeName":"NetworkInterfaceExceededTimeLimit","errmsg":"Couldn't get a connection within the time limit of 500ms"},"action":{"dropConnections":false,"requestImmediateCheck":false,"outcome":{"host":":1996","success":false}}}}
{"t":{"$date":"2022-07-28T15:31:36.195+00:00"},"s":"I",  "c":"-",        "id":4333222, "ctx":"ReplicaSetMonitor-TaskExecutor","msg":"RSM received failed isMaster","attr":{"host":"52.x.x.x:1996","error":"NetworkInterfaceExceededTimeLimit: Couldn't get a connection within the time limit of 500ms","replicaSet":"rs","isMasterReply":"{}"}}
{"t":{"$date":"2022-07-28T15:31:36.196+00:00"},"s":"I",  "c":"NETWORK",  "id":4712102, "ctx":"ReplicaSetMonitor-TaskExecutor","msg":"Host failed in replica set","attr":{"replicaSet":"rs","host":"52.x.x.x:1996","error":{"code":202,"codeName":"NetworkInterfaceExceededTimeLimit","errmsg":"Couldn't get a connection within the time limit of 500ms"},"action":{"dropConnections":false,"requestImmediateCheck":false,"outcome":{"host":":1996","success":false}}}}

i only have access to db if i used directconnection option when connecting to db, but we use replicaset and not available for us to use.

Are you sure the port is right? it’s non-default

What happens if you run the following commands?

netstat -na | grep 1996

and

telnet localhost 1996

These will help you determine if you have an application running on the port and help you to troubleshoot if connectivity is an issue. Note that these are Linux commands so if your local machine is Windows you would have to look for equivalent commands (I can’t remember if these are standard/available on Windows machines or not).

1 Like

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

Right , I am not using the default port, so I have changed it to 1996 in mongod.cfg file

Hi stennie,

The thing is, I have just captured an image “actually AMI because I am using AWS” and the machine which I captured image from, it was working properly with no issues and no local DNS reolution as you have suggested.

In the old machine i had not used replicaset members as they should be used, I just enabled replicaset in the .cfg file and added a name for it, so later I can add members when I decide to do that.

so, for new machine, I just spined up another mongoDB machine from the image and applied the machine NEW elastic ip in the application code which already in another machine.

All things should work properly as before but unfortunately no.

Hi doug,
Hope all is well.

this is the output of two testing commands, i also tested the same commands in our live mongo which is the machine that working properly, and have the same result in two machines.

Thanks for showing the output of the commands @mahmoud_abdelfatah. In playing around, I was able to get a similar timeout message while trying to connect to a local replica set using Compass if my replica set name was incorrect. Have you validated that you are using the correct name?

You can check the name of the replica set by connecting directly to one member of the set and looking to see what information is provided in the upper left box in Atlas:

image

If that name (foo) in the example above matches the value you’re typing in for the replicaSet parameter, then I’m not sure why you’re still timing out.

issue solved, but the problem was little bit different for our case

i have created a new mongodb machine from an image of old machine, so the replicaset host ip address still the same for the old machine, so i should change it to the new machine ip

image

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.