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 0.0.0.0, 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
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 https://www.whatismyip.com/.
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.
I got 184.108.40.206 by running sudo ifconfig on the virtual machine and looking in the result for ens160
When I run mongo -u user 220.127.116.11/admin 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://18.104.22.168:27017/admin?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server 22.214.171.124:27017, connection attempt failed: NetworkTimeout: Error connecting to 126.96.36.199:27017 :: caused by :: Socket operation timed out :
exception: connect failed
exiting with code 1
The first thing I would try would be to bindIp:0.0.0.0. 188.8.131.52 might not be the address the correct address. The message
connecting to: mongodb://184.108.40.206:27017/...
Error: couldn't connect to server 220.127.116.11:27017...
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 18.104.22.168 mapping might be different from the one on the VM host.
Can you ping 22.214.171.124?
Where did you create the firewall rules?