MongoDB error connecting to host: could not connect to server: server selection error: server selection timeout

In this article, we will see to troubleshoot the problem for connectivity between different MongoDB servers. I got below error when tried to restore database from local to remote host.

2019-11-20T15:56:59.536-0800 error connecting to host: could not connect to server: server selection error: server selection timeout current topology: Type: Single Servers: Addr: 192.168.152.141:27017, Type: Unknown, State: Connected, Average RTT: 0, Last error: connection(192.168.152.141:27017[-121]) connection is closed

Solution for server selection error: server selection timeout:

1. Check the firewall status. If not started start it.

systemctl status ufw

If firewall not started or disabled, then executes below commands.

systemctl start ufw sudo efw enable

2. If firewall is already enabled and still having issue means, we have to add the ip in bindIP in the remote host’s /etc/mongod.conf file. Before modifying any changes, please take backup of mongod.conf file.

3. I have replaced existing one as below:

net: port: 27017 bindIp: 192.168.152.141 #127.0.0.1

where 192.168.152.141 is my host ip.

4. Restart the MongoDB server to take effect changes.

sudo systemctl restart mongod.service

So, issue is with bindIP.

Leave a Reply

Your email address will not be published. Required fields are marked *