centos7防火墻導(dǎo)致spring?boot訪問(wèn)mongodb是出現(xiàn)異常查看詳情http://blog.csdn.net/hkh_1012/article/details/44899561
環(huán)境描述:
數(shù)據(jù)庫(kù):mongodb3.0.1
數(shù)據(jù)庫(kù)系統(tǒng):centos7,(虛擬機(jī),最小安裝)
數(shù)據(jù)庫(kù)驅(qū)動(dòng):mongo-java-driver-3.0.0.jar
問(wèn)題描述:shell環(huán)境下用mongo客戶端程序能正常連接mongod,但java程序始終報(bào)錯(cuò)颂翼。
java代碼很簡(jiǎn)單忽孽,如下:
[java]?view plain?copy
Mongo?mg?=?new?Mongo("192.168.59.128",27017);??
DB?db?=?mg.getDB("mydb");??
for(String?s:db.getCollectionNames()){??
????????????System.out.println(s);??
????????}??
報(bào)錯(cuò)如下:
四月?05,?2015?8:26:27?下午?com.mongodb.diagnostics.logging.JULLogger?log
信息:?Cluster?created?with?settings?{hosts=[192.168.59.128:27017],?mode=SINGLE,?requiredClusterType=UNKNOWN,?serverSelectionTimeout='30000?ms',?maxWaitQueueSize=50}
四月?05,?2015?8:26:28?下午?com.mongodb.diagnostics.logging.JULLogger?log
信息:?No?server?chosen?by?ReadPreferenceServerSelector{readPreference=primary}?from?cluster?description?ClusterDescription{type=UNKNOWN,?connectionMode=SINGLE,?all=[ServerDescription{address=192.168.59.128:27017,?type=UNKNOWN,?state=CONNECTING}]}.?Waiting?for?30000?ms?before?timing?out
四月?05,?2015?8:26:48?下午?com.mongodb.diagnostics.logging.JULLogger?log
信息:?Exception?in?monitor?thread?while?connecting?to?server?192.168.59.128:27017
com.mongodb.MongoSocketOpenException:?Exception?opening?socket
at?com.mongodb.connection.SocketStream.open(SocketStream.java:63)
at?com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:114)
at?com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127)
at?java.lang.Thread.run(Unknown?Source)
Caused?by:?java.net.SocketTimeoutException:?connect?timed?out
at?java.net.DualStackPlainSocketImpl.waitForConnect(Native?Method)
at?java.net.DualStackPlainSocketImpl.socketConnect(Unknown?Source)
at?java.net.AbstractPlainSocketImpl.doConnect(Unknown?Source)
at?java.net.AbstractPlainSocketImpl.connectToAddress(Unknown?Source)
at?java.net.AbstractPlainSocketImpl.connect(Unknown?Source)
at?java.net.PlainSocketImpl.connect(Unknown?Source)
at?java.net.SocksSocketImpl.connect(Unknown?Source)
at?java.net.Socket.connect(Unknown?Source)
at?com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:50)
at?com.mongodb.connection.SocketStream.open(SocketStream.java:58)
...?3?more
Exception?in?thread?"main"?com.mongodb.MongoTimeoutException:?Timed?out?after?30000?ms?while?waiting?for?a?server?that?matches?ReadPreferenceServerSelector{readPreference=primary}.?Client?view?of?cluster?state?is?{type=UNKNOWN,?servers=[{address=192.168.59.128:27017,?type=UNKNOWN,?state=CONNECTING,?exception={com.mongodb.MongoSocketOpenException:?Exception?opening?socket},?caused?by?{java.net.SocketTimeoutException:?connect?timed?out}}]
at?com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:370)
at?com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:101)
at?com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.(ClusterBinding.java:75)
at?com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.(ClusterBinding.java:71)
at?com.mongodb.binding.ClusterBinding.getReadConnectionSource(ClusterBinding.java:63)
at?com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:166)
at?com.mongodb.operation.ListCollectionsOperation.execute(ListCollectionsOperation.java:172)
at?com.mongodb.operation.ListCollectionsOperation.execute(ListCollectionsOperation.java:80)
at?com.mongodb.Mongo.execute(Mongo.java:736)
at?com.mongodb.Mongo$2.execute(Mongo.java:723)
at?com.mongodb.OperationIterable.iterator(OperationIterable.java:47)
at?com.mongodb.OperationIterable.forEach(OperationIterable.java:66)
at?com.mongodb.MappingIterable.forEach(MappingIterable.java:50)
at?com.mongodb.MappingIterable.into(MappingIterable.java:60)
at?com.mongodb.DB.getCollectionNames(DB.java:223)
at?mongodbtest.TestMongodbCurd.main(TestMongodbCurd.java:13)
報(bào)錯(cuò)信息中有“Exception opening socket”推測(cè)應(yīng)該是java程序與服務(wù)器建立連接時(shí)報(bào)錯(cuò)姐刁,
執(zhí)行命令ps -aux | grep mong余掖,查看mongod進(jìn)程田篇,結(jié)果顯示mongod運(yùn)行正常许布。
root ? ? ?2139 ?1.1 ?6.9 631964 70364 ? ? ? ? ?Sl ? 16:15 ? 0:48 mongod -f /usr/local/mongodb/mongodb-linux-x86_64-3.0.1/bin/mongodb.conf
執(zhí)行命令netstat -ntlp 兴革,查看數(shù)據(jù)庫(kù)27017端口監(jiān)聽(tīng)是否正常,結(jié)果正常
tcp ? ? ? ?0 ? ? ?0 0.0.0.0:27017 ? ? ? ? ? 0.0.0.0:* ? ? ? ? ? ? ? LISTEN ? ? ?2139/mongod
執(zhí)行命令?cat /usr/local/mongodb/mongodb-linux-x86_64-3.0.1/bin/mongodb.conf蜜唾,再次查看mongod的啟動(dòng)配置文件杂曲,結(jié)果如下,仍沒(méi)有問(wèn)題
dbpath = /usr/local/mongodb/data/db
logpath = /usr/local/mongodb/logs/mongodb.log
#bind_ip = 192.1168.59.128
port = 27017
fork = true
#nohttpinterface = true
至此袁余,再結(jié)合shell下mongo客戶端可以正常使用擎勘,基本可以推斷不是mongodb數(shù)據(jù)庫(kù)的問(wèn)題,應(yīng)該是系統(tǒng)安全設(shè)置的問(wèn)題颖榜,首先想到的是防火墻棚饵。
查看centos7的官方文檔,知道centos7最小安裝默認(rèn)只安裝防火墻的動(dòng)態(tài)配置規(guī)則服務(wù)(firewalld)掩完,靜態(tài)配置規(guī)則服務(wù)(iptables與ip6tables)需要另行安裝噪漾。
安裝防火墻靜態(tài)配置規(guī)則服務(wù),并設(shè)置成可用狀態(tài)
yum install iptables-services
systemctl mask firewalld.service
systemctl enable iptables.service
systemctl enable ip6tables.service
修改配置:vi /etc/sysconfig/iptables
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 27017 -j ACCEPT
停用動(dòng)態(tài)規(guī)則服務(wù)且蓬,啟用靜態(tài)規(guī)則服務(wù)
systemctl stop firewalld.service
systemctl start iptables.service
systemctl start ip6tables.service
再次運(yùn)行java程序,得到結(jié)果如下:
信息: Opened connection [connectionId{localValue:2, serverValue:3}] to 192.168.59.128:27017
inventory
person
personInfo
system.indexes
testData
與想要的結(jié)果一致欣硼。