Cannot connect remotely to mongodb

I have followed several guides on installing and configuring mongodb. None are working, but I think I’m close… I’ve have managed to get to a point that I can connect to the db server from the local server using the following command:

mongo -u user my_url/admin

However, I’m not able to connect to the server using the above command from a remote machine.

I’ve tried both with authentication enabled and disabled, and I’ve tried setting the bindIP to, or adding the remote url after a comma, or commenting out the line but nothing worked. I also created the firewall rules for port 127 with the following commands:

sudo iptables -A INPUT -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT
sudo iptables -A OUTPUT -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT

What am I doing wrong? I am a beginner and it might be some stupid error :confused:

1 Like

By remote, do you mean a different machine on the same network or a different machine from another location?

If a different machine on the same network, it should work easily.

As for the other case, you need to connect using the public IP address of the server. That is not necessarily the IP address of the machine running mongod, if behind a NAT router or VPN. You may find the current public IP using

1 Like

Hi Steeve, thank you for your answer. I am using a VM from the data center of my university and in order to access it I need to be connected to a VPN from the same data center. As I am still connected to the VPN when I try to access the server on the VM, I guess that we should be on the same network. Nevertheless as none of what I read is working I am not sure.

This is how I configured the mongo.conf file:

 # network interfaces
    port: 27017

I got by running sudo ifconfig on the virtual machine and looking in the result for ens160

When I run mongo -u user on the VM it works, however when I do it from my pc I get this error:

MongoDB shell version v4.4.6
connecting to: mongodb://
Error: couldn't connect to server, connection attempt failed: NetworkTimeout: Error connecting to :: caused by :: Socket operation timed out :
exception: connect failed
exiting with code 1

Am I using the wrong urls in the mongo.conf?

The first thing I would try would be to bindIp: might not be the address the correct address. The message

connecting to: mongodb://
Error: couldn't connect to server

seems to indicate that there is some mapping happening between the client and the server.

Then, Socket operation timed out might means a routing or firewall issue.

Is the VM host your PC or another machine? If another machine then the mapping might be different from the one on the VM host.
Can you ping
Where did you create the firewall rules?

1 Like

Is it really or is that to obfuscate your actual ip address ?

1 Like

It is my obscurated IP address

I just noticed I didn’t obscurate my IP address properly. is

Security through obscurity (Security through obscurity - Wikipedia) has never been favourable.

And it does not help us helping you because we do not have the real facts.

1 Like

I am experiencing this same issue Unable to setup remote access for mongodb

you have to try new SSH client for this.

Could you provide some details about the command line options required to use ssh in this case?

Just mentioning SSH is not very usefull.