Case 1. I installed mongo
on my machine (Mac OS), and mongo
is configured with bind-ip : 127.0.0.1
. A remote machine in LAN can connect to my mongodb using my IP. (Wrong observation : See UPDATE section)
Case 2. I installed mongo
on Azure Ubuntu VM
, mongo configured with bind-ip:127.0.0.1
. No remote machine from the same VNet can connect to that mongo using private vnet IP.
MongoDB Network Setting (both case 1 and case 2):
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1
Exception (Case 2):
2018-04-03 06:35:29.948 INFO 44518 --- [72.17.1.4:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server 172.17.1.4:27017
com.mongodb.MongoSocketOpenException: Exception opening socket at com.mongodb.connection.SocketStream.open(SocketStream.java:63) ~[mongodb-driver-core-3.4.1.jar!/:na] at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) ~[mongodb-driver-core-3.4.1.jar!/:na] at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113) ~[mongodb-driver-core-3.4.1.jar!/:na] at java.lang.Thread.run(Thread.java:748) [na:1.8.0_151] Caused by: java.net.ConnectException: Connection refused (Connection refused) at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_151] at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_151] at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_151] at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_151] at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_151] at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_151] at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57) ~[mongodb-driver-core-3.4.1.jar!/:na] at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ~[mongodb-driver-core-3.4.1.jar!/:na] ... 3 common frames omitted
Whats is different in Azure VM that it behaves differently from a Mac OS installation?
Note: Changing the bindIp
to the IP (172.17.1.4)
or 0.0.0.0
works.
UPDATE:
Case 1: The remote access on my local machine's mongo was because of the distribution I picked which was configured to allow remote connections.
127.0.0.1
only works inside the machine, do you ensure you could access Mogodb outside your MAC OS? – Shui shengbaoBy default, MongoDB bind to local interface only, it will restrict the remote connections. If you don’t care about security, just comment out to accept any remote connections (NOT Recommend).
– Shui shengbaobind_ip = 127.0.0.1,172.17.1.4
. – Shui shengbao127.0.0.1
, you should set it listen on VM's private IP or0.0.0.0
. – Shui shengbao