作者:lady张昕彤 | 来源:互联网 | 2023-07-28 21:08
com.mongodb.MongoSocketOpenException:Exceptionopeningsocketatcom.mongodb.connection.Socket
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:63) ~[mongodb-driver-core-3.4.3.jar:na]
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) ~[mongodb-driver-core-3.4.3.jar:na]
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113) ~[mongodb-driver-core-3.4.3.jar:na]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_161]
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_161]
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[na:1.8.0_161]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_161]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_161]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_161]
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_161]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_161]
at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_161]
at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57) ~[mongodb-driver-core-3.4.3.jar:na]
at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ~[mongodb-driver-core-3.4.3.jar:na]
... 3 common frames omitted
网上搜索了一下解决方案
1.说是springboot自动配置了支持mongodb。在启动springboot时会自动实例化一个mongo实例。 要加上
@SpringBootApplication(exclude = MongoAutoConfiguration.class),但是我的不行,原文链接如下。
http://blog.csdn.net/xuyw10000/article/details/72918360
2.说在配置文件里把初始的 mongodb 取消掉就好了(不懂怎么取消)
.http://www.cnblogs.com/zhuangfei/p/7338488.html
3.最后发现我用的idea,spring initializr选择的依赖是testCompile的,在运行环境下没有
这是springboot默认的
testCompile('de.flapdoodle.embed:de.flapdoodle.embed.mongo')
打包成jar在Linux下运行报错
改成
compile('de.flapdoodle.embed:de.flapdoodle.embed.mongo')
正常运行