Failed to set up listener: SocketException: Cannot assign requested address when using hostname

Hi, I’m trying to setup a replication cluster, but on my oracle cloud vm I can’t set the hostname without giving an error.

I used nslookup to make sure if the A record was correct in the vm’s dns server and it was, on another vps I have somewhere else it works fine.
This is the config file template I’m using for all my servers and the error log: https://gist.github.com/GameMaster2030/b714d164577d4f1808aa3cfa0e53ef1d

XX is a number, so 01, 02 etc.

Here is my DNS config.
Any idea why it is giving this error and how I can fix it?

@GameMaster2030

The message “Cannot assign requested address” suggests that the hostname/IP you are trying to bind does not resolve to a local network interface.

A few things to check:

  • Are the full hostnames (eg 01.mongodb.DOMAIN.nl) resolvable on all of your replica set members? For a quick test, try ping or nslookup using the expected hostname. Your screenshot of DNS details suggests that the replica set members would need to include DOMAIN.nl in their DNS search domain in order to resolve the full hostname.

  • Does the hostname resolve to an IP address that is also local to the machine (eg on Linux the IP should be listed in the output of ifconfig -a | grep "inet").

If the name resolution appears to check out, can you confirm what O/S version is running in your VMs?

Regards,
Stennie

1 Like

All the hostnames are resolvable on all the hosts:
Host 1:

Host 2:

Host 3:

The ip isn’t listed on host 2 and 3, as seen on the screenshots. Host 1 does have the ip listed and is the only host that works. Maybe that is the issue, how can I add the IP?

All of them are running Ubuntu 18.04

It sounds like DNS resolution is fine on all your VMs, but two of them are missing the expected local IPs.

If an IP isn’t associated with a local network interface, the original error message that you encountered would be expected. The MongoDB process cannot bind (aka “listen to”) an IP address that isn’t local.

The fix for this would be outside of MongoDB: your VMs need to have the extra network interfaces assigned & configured.

However, I also note that the IPs you are trying to add are public IPs (the existing 10.* IPs are private IPv4 addresses). Typically you would only want to bind to private IPs and have access to the database servers secured via VPN or SSH tunnel from trusted application servers. For more information on security measures please see the MongoDB Security Checklist.

Regards,
Stennie

2 Likes

So I got it to work and it’s up now, but now when I try to connect to hosts 2 and 3 externally, it gives the following error:

E QUERY    [js] Error: couldn't connect to server 02.mongodb.DOMAIN.nl:27017, connection attempt failed: NetworkTimeout: Error connecting to 02.mongodb.DOMAIN.nl:27017 (X.X.X.X:27017) :: caused by :: Socket operation timed out :
connect@src/mongo/shell/mongo.js:344:17
@(connect):2:6
exception: connect failed

But when I connect to the hostname on the host itself it works fine.

Error connecting to 02.mongodb.DOMAIN.nl:27017 (X.X.X.X:27017) :: caused by :: Socket operation timed out

I assume the expected IP is logged here, so the issue is probably related to firewall rather than DNS.

Drivers/clients connecting to a replica set will use the hostnames listed in the replica set config for server discovery & monitoring. A Socket operation timed out error is expected if a driver cannot connect to a mongod instance using the hostnames and ports discovered via the replica set configuration.

Possible solutions are:

  • Open access to all members of your replica set from your originating IP address. You can limit network exposure using a firewall to only allow access from trusted network sources (assuming your applications have a fixed IP address).
  • Use a VPN to securely connect to your replica set deployment without opening up direct outside network access to the mongod instances.
  • Connect to a single member of your replica set in standalone mode rather than using a replica set connection string (i.e. remove the replicaSet parameter from your connection). Using a standalone connection string will avoid discovering and monitoring other members of the replica set, but that connection won’t have the benefits of replica set failover or discovery of the current primary.

Regards,
Stennie


Figured out where the firewall is on Oracle, so I added this rule. (Will change the IP range later to only allow trusted IP’s)
But now I get the following error:

2020-02-17T15:30:48.483+0000 E  QUERY    [js] Error: couldn't connect to server 03.mongodb.DOMAIN.nl:27017, connection attempt failed: SocketException: Error connecting to 03.mongodb.DOMAIN.nl:27017 (X.X.X.X:27017) :: caused by :: No route to host :
connect@src/mongo/shell/mongo.js:341:17
@(connect):2:6
2020-02-17T15:30:42.322+0000 F  -        [main] exception: connect failed
2020-02-17T15:30:42.322+0000 E  -        [main] exiting with code 1

No route to host indicates a networking problem: there currently isn’t a valid network route to communicate with the target IP address.

If the target IP in that message is as expected and your firewall rules are open, one likely cause is that you may be trying to connect to private IPs (for example, 10.* in one of your earlier output examples) from a public IP. Private IPs are only routable within the same local network or VPN.

If the destination IPs are definitely not private IPs, this may be a networking or firewall issue to follow up on with your ISP/hosting providers.

Regards,
Stennie

1 Like

Hi @Stennie_X,

I took the time to set up a VPN to connect the servers together, but I still get the no route to host error.

> rs.initiate( {    _id : "rs0",    members: [       { _id: 0, host: "01.mongodb.DOMAIN.nl:27017" },       { _id: 1, host: "02.mongodb.DOMAIN.nl:27017" },       { _id: 2, host: "03.mongodb.DOMAIN.nl:27017" }
    ] })
{
        "ok" : 0,
        "errmsg" : "replSetInitiate quorum check failed because not all proposed set members responded affirmatively: 02.mongodb.DOMAIN.nl:27017 failed with Error connecting to 02.mongodb.DOMAIN.nl:27017 (10.8.0.3:27017) :: caused by :: No route to host, 03.mongodb.DOMAIN.nl:27017 failed with Error connecting to 03.mongodb.DOMAIN.nl:27017 (10.8.0.2:27017) :: caused by :: No route to host",
        "code" : 74,
        "codeName" : "NodeNotFound"

They can ping each other just fine and connect to each other with ssh.

@Stennie_X Do you know what’s causing this? I’m using OpenVPN