Spring boot 中集成MongoDB遇到报错的问题

centos7防火墙导致spring boot访问mongodb是出现异常查看详情http://blog.csdn.net/hkh_1012/article/details/44899561

环境描述:

数据库:mongodb3.0.1

数据库系统:centos7,(虚拟机,最小安装)

数据库驱动:mongo-java-driver-3.0.0.jar

问题描述:shell环境下用mongo客户端程序能正常连接mongod,但java程序始终报错。

java代码很简单,如下:

[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);  

        }  

报错如下:

四月 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)

报错信息中有“Exception opening socket”推测应该是java程序与服务器建立连接时报错,

执行命令ps -aux | grep mong,查看mongod进程,结果显示mongod运行正常。

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

执行命令netstat -ntlp ,查看数据库27017端口监听是否正常,结果正常

tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      2139/mongod

执行命令 cat /usr/local/mongodb/mongodb-linux-x86_64-3.0.1/bin/mongodb.conf,再次查看mongod的启动配置文件,结果如下,仍没有问题

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

至此,再结合shell下mongo客户端可以正常使用,基本可以推断不是mongodb数据库的问题,应该是系统安全设置的问题,首先想到的是防火墙。

查看centos7的官方文档,知道centos7最小安装默认只安装防火墙的动态配置规则服务(firewalld),静态配置规则服务(iptables与ip6tables)需要另行安装。

安装防火墙静态配置规则服务,并设置成可用状态

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

停用动态规则服务,启用静态规则服务

systemctl stop firewalld.service

systemctl start iptables.service

systemctl start ip6tables.service

再次运行java程序,得到结果如下:

信息: Opened connection [connectionId{localValue:2, serverValue:3}] to 192.168.59.128:27017

inventory

person

personInfo

system.indexes

testData

与想要的结果一致。

    原文作者:shoage
    原文地址: https://www.jianshu.com/p/c8a7d37b57ab
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞