Impossible to connect : MongoServerSelectionError despite allowing all IP

In a nutshell, I can’t connect to mongodb from my VPS. Tho I can from my pc and Heroku.

  • Are all ports open ? Yes
  • Is there any firewall/VPN ? No
  • Does the VPS have static IP : Yes
  • Which IP is allowed on Network Access ? 0.0.0.0/0
  • Tried changing the DNS ? Yes, tried both Google and Cloudflare
resolvectl status
Global
         Protocols: +LLMNR +mDNS -DNSOverTLS DNSSEC=no/unsupported
  resolv.conf mode: stub
Current DNS Server: 8.8.8.8
        DNS Servers 8.8.8.8 8.8.4.4

Link 2 (eth0)
Current Scopes: LLMNR/IPv4 LLMNR/IPv6
     Protocols: -DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported

Link 3 (docker0)
Current Scopes: none
     Protocols: -DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
  • Can I connect to :27017 ? yes
curl portquiz.net:27017
Port test successful!
Your IP: [IP]
  • Tried using the old connection string ? Yes
edm115@VPS:~/unzip-bot$ mongosh $MONGODB_URL
Current Mongosh Log ID: 65f94ff55c890d27f9c72f04
Connecting to:          mongodb+srv://<credentials>@unzip-bot-cluster.abcde.mongodb.net/unzipDB?retryWrites=true&w=majority&appName=mongosh+2.2.0
MongoServerSelectionError: Server selection timed out after 30000 ms. It looks like this is a MongoDB Atlas cluster. Please ensure that your Network Access List allows connections from your IP.
edm115@VPS:~/unzip-bot$ mongosh $MONGO_URI
Current Mongosh Log ID: 65f9501eecdaae1be0c72f04
Connecting to:          mongodb://<credentials>@unzip-bot-cluster-shard-00-00.abcde.mongodb.net:27017,unzip-bot-cluster-shard-00-01.abcde.mongodb.net:27017,unzip-bot-cluster-shard-00-02.abcde.mongodb.net:27017/unzipDB?retryWrites=true&w=majority&appName=mongosh+2.2.0
MongoServerSelectionError: connection <monitor> to 65.52.148.81:27017 closed. It looks like this is a MongoDB Atlas cluster. Please ensure that your Network Access List allows connections from your IP.

So far I feel like I’ve tried everything. Connecting with a driver (to check if it is a mongosh error) isn’t any better :

Error in main loop:
SSL handshake failed:
	unzip-bot-cluster-shard-00-01.abcde.mongodb.net:27017:
		_ssl.c:989: The handshake operation timed out (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms),
SSL handshake failed:
	unzip-bot-cluster-shard-00-00.abcde.mongodb.net:27017:
		_ssl.c:989: The handshake operation timed out (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms),
SSL handshake failed:
	unzip-bot-cluster-shard-00-02.abcde.mongodb.net:27017:
		_ssl.c:989: The handshake operation timed out (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms),
Timeout: 30s,
Topology Description:
	<TopologyDescription id: 65f799042447913c4e692b94,
	topology_type: ReplicaSetNoPrimary,
	servers: [<ServerDescription
		('unzip-bot-cluster-shard-00-00.abcde.mongodb.net', 27017) server_type: Unknown,
		rtt: None,
		error=NetworkTimeout('SSL handshake failed:
			unzip-bot-cluster-shard-00-00.abcde.mongodb.net:27017:
				_ssl.c:989: The handshake operation timed out (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms)')>,
	<ServerDescription
		('unzip-bot-cluster-shard-00-01.abcde.mongodb.net', 27017) server_type: Unknown,
		rtt: None,
			error=NetworkTimeout('SSL handshake failed:
				unzip-bot-cluster-shard-00-01.abcde.mongodb.net:27017:
					_ssl.c:989: The handshake operation timed out (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms)')>,
	<ServerDescription
		('unzip-bot-cluster-shard-00-02.abcde.mongodb.net', 27017) server_type: Unknown,
		rtt: None,
		error=NetworkTimeout('SSL handshake failed:
			unzip-bot-cluster-shard-00-02.abcde.mongodb.net:27017:
				_ssl.c:989: The handshake operation timed out (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms)')>]>

Really a pain, trying to fix this since 5 days, so if anyone can help me I’d be very grateful :heart_hands:

1 Like

Did you read this kind of posts SSL handshake failed error when trying to connect to MongoDB with Python client - Stack Overflow ?

yes I did
tried basically every solution so far (adding CA certificates, both with certifi and system-wide; passing options to disable ssl, larger timeouts, …)
that py script was running in a docker container based off arch, and it’s upon that failure that I got the idea to test on the host machine directly. With as a result the issue I reported (MongoServerSelectionError)

so I found how to fix it.
if nothing seems to work, there may be a solution : lower the MTU
base value is given by ip addr show

giving that the interface you’re using is eth0 :
sudo ip link set dev eth0 mtu 1400
and then try a simple mongosh or mongoexport to test if it works. lower until you can connect.
to set it permanently :

  • if you’re using ifupdown :
    sudo nano /etc/network/interfaces (or a specific interface in /etc/network/interfaces.d/)
    Edit the part of your interface (here eth0) by adding the mtu value that works :
    iface eth0 inet static
          mtu 1400
    
    then do sudo systemctl restart networking or sudo /etc/init.d/networking restart
  • if you’re using systemd-networkd :
    Find or create the .network file for your interface in /etc/systemd/network/ . The file name and location might vary, but a common pattern is 10-eth0.network for an interface named eth0
    edit the file, for example with eth0 :
    [Match]
    Name=eth0
    
    [Link]
    MTUBytes=1400
    
    then do sudo systemctl daemon-reload && sudo systemctl restart systemd-networkd
  • for docker :
    Docker have its own config. the best way to change that is by doing :
    sudo nano /etc/docker/daemon.json
    Edit the file by adding the MTU config (keep it formatted as a JSON)
    {
      "mtu": 1400
    }
    
    Then restart docker with sudo systemctl restart docker

however note that doing that will absolutely tank the connection speed and may increase packet loss

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