Impossible to connect : MongoServerSelectionError despite allowing all IP

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