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 1.2.3.4 by running sudo ifconfig on the virtual machine and looking in the result for ens160
When I run mongo -u user 1.2.3.4/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://1.2.3.4:27017/admin?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server 131.159.56.49:27017, connection attempt failed: NetworkTimeout: Error connecting to 131.159.56.49:27017 :: caused by :: Socket operation timed out :
connect@src/mongo/shell/mongo.js:374:17
@(connect):2:6
exception: connect failed
exiting with code 1
The first thing I would try would be to bindIp:0.0.0.0. 1.2.3.4 might not be the address the correct address. The message
connecting to: mongodb://1.2.3.4:27017/...
Error: couldn't connect to server 131.159.56.49: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 1.2.3.4 mapping might be different from the one on the VM host.
Can you ping 131.159.56.49?
Where did you create the firewall rules?