Spring Boot显示"连接被拒绝"连接mongoDB时出错

时间:2018-03-26 16:29:12

标签: java spring mongodb spring-boot

我正在使用Spring Boot项目设置MongoDB。但是,当我通过gradle编译我的应用程序并运行jar时,它会抛出以下错误:

2018-03-26 16:17:00.634  INFO 9321 --- [localhost:27017] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server localhost:27017

com.mongodb.MongoSocketOpenException: Exception opening socket
        at com.mongodb.connection.SocketStream.open(SocketStream.java:62) ~[mongodb-driver-core-3.6.3.jar!/:na]
        at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongodb-driver-core-3.6.3.jar!/:na]
        at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114) ~[mongodb-driver-core-3.6.3.jar!/:na]
        at java.lang.Thread.run(Thread.java:745) [na:1.8.0_121]
Caused by: java.net.ConnectException: Connection refused (Connection refused)
        at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_121]
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_121]
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_121]
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_121]
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_121]
        at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_121]
        at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:59) ~[mongodb-driver-core-3.6.3.jar!/:na]
        at com.mongodb.connection.SocketStream.open(SocketStream.java:57) ~[mongodb-driver-core-3.6.3.jar!/:na]
        ... 3 common frames omitted

在运行jar之前,我已经通过mongod在我的mac上运行了mongodb,它在终端I NETWORK [initandlisten] waiting for connections on port 27017上显示以下消息

另外,我的spring项目的applications.properties文件是空白的。任何关于如何诊断这个问题的线索都将不胜感激!

1 个答案:

答案 0 :(得分:0)

经过近一天的调试,Spring Boot项目与localhost上的mongodb安装成功连接。仍然不确定究竟是什么导致了这个问题,但这是我调试它的方式:

1)通过将实例绑定到所有接口来运行mongodb:

g2

2)我按如下方式构建了我的应用程序的配置文件:

sudo mongod —bind_ip_all  

然后将此配置文件读入主应用程序文件:

@Configuration
@EnableAutoConfiguration(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
@ComponentScan
public class AppConfiguration {
} 

最后,将application.properties文件留空。