用compass community和java程序连不上远程linux上的mongodb问题解决

1.用compass community连接时:
用compass community和java程序连不上远程linux上的mongodb问题解决_第1张图片
2.用java程序连接时:

com.mongodb.MongoSocketOpenException: Exception opening socket
	at com.mongodb.connection.SocketStream.open(SocketStream.java:62) ~[mongodb-driver-core-3.5.0.jar:na]
	at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:107) ~[mongodb-driver-core-3.5.0.jar:na]
	at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:111) ~[mongodb-driver-core-3.5.0.jar:na]
	at java.lang.Thread.run(Unknown Source) [na:1.8.0_171]
Caused by: java.net.SocketTimeoutException: connect timed out
	at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_171]
	at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) ~[na:1.8.0_171]
	at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) ~[na:1.8.0_171]
	at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) ~[na:1.8.0_171]
	at java.net.AbstractPlainSocketImpl.connect(Unknown Source) ~[na:1.8.0_171]
	at java.net.PlainSocketImpl.connect(Unknown Source) ~[na:1.8.0_171]
	at java.net.SocksSocketImpl.connect(Unknown Source) ~[na:1.8.0_171]
	at java.net.Socket.connect(Unknown Source) ~[na:1.8.0_171]
	at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:59) ~[mongodb-driver-core-3.5.0.jar:na]
	at com.mongodb.connection.SocketStream.open(SocketStream.java:57) ~[mongodb-driver-core-3.5.0.jar:na]
	... 3 common frames omitted

原因:linux上的防火墙没有关。
我是centos7,关闭防火墙:
systemctl stop firewalld.service
systemctl disable firewalld.service
开始防火墙:
systemctl start firewalld.service
systemctl enable firewalld.service
查看防火墙状态:
systemctl status firewalld
关闭之后可以正常连接。
补充:第二天连的时候发现连不上了,用systemctl status firewalld查看防火墙是关闭的,百度了很久没解决,无赖查看了一下iptables的状态:systemctl status iptables.service,发现时开启的,centos7应该是没这个命令的,不知道自己是什么时候下的,然后把这个关闭后问题解决了。
关闭iptables:
systemctl stop iptables.service
systemctl disable iptables.service

你可能感兴趣的:(mongodb,compass,关闭和开启linux防火墙)