Mongo DB,Spring启动:异常打开套接字,引起:连接被拒绝

时间:2017-11-02 04:58:17

标签: mongodb spring-boot

我正在使用Spring Boot来连接到MongoDB,但它在尝试连接时给了我一个例外,奇怪的是这对我来说很有用,突然它开始给我这个错误。 任何人都可以帮助我,如果这是问题,因为在这个端口上运行了一些其他服务(27017),如果是这样,我怎么能找到它。

com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:63) ~[mongodb-driver-core-3.4.2.jar:na]
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) ~[mongodb-driver-core-3.4.2.jar:na]
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113) ~[mongodb-driver-core-3.4.2.jar:na]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_45]
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_45]
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[na:1.8.0_45]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345) ~[na:1.8.0_45]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_45]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_45]
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_45]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_45]
at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_45]
at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57) ~[mongodb-driver-core-3.4.2.jar:na]
at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ~[mongodb-driver-core-3.4.2.jar:na]
... 3 common frames omitted

application.properties:

    spring.data.mongodb.host=localhost
    spring.data.mongodb.port=27017
    spring.data.mongodb.database=swt

的pom.xml:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>2.12.3</version>
    </dependency>

1 个答案:

答案 0 :(得分:0)

我通过在VM上安装mongo然后运行Spring启动实例来解决了这个问题。

对我来说,嵌入式mongo驱动程序(de.flapdoodle.embed.mongo)无法正常工作,因此我删除了依赖项。

安装Mongo并运行它

  • sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4

  • echo“ deb [arch = amd64,arm64] https://repo.mongodb.org/apt/ubuntu xenial / mongodb-org / 4.0 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

  • sudo apt-get更新
  • sudo apt-get install -y mongodb-org
  • sudo服务mongod重新启动

运行Spring Boot应用程序