无法将我的spring-boot应用程序与mongodb云图集连接起来

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法将我的spring-boot应用程序与mongodb云图集连接起来相关的知识,希望对你有一定的参考价值。

我们正在尝试从Java应用程序连接到MongoDB云(Atlas)。我不明白为什么我拒绝Connection。这个例外让我产生了:

堆栈跟踪:

sterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2018-09-17 23:22:13.102  INFO 16140 --- [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) ~[mongo-java-driver-3.6.4.jar:na]
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongo-java-driver-3.6.4.jar:na]
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114) ~[mongo-java-driver-3.6.4.jar:na]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_101]
Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_101]
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_101]
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_101]
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_101]
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_101]
    at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_101]
    at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:59) ~[mongo-java-driver-3.6.4.jar:na]
    at com.mongodb.connection.SocketStream.open(SocketStream.java:57) ~[mongo-java-driver-3.6.4.jar:na]
    ... 3 common frames omitted

这是我的依赖pom:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.7.0</version>
</dependency>
<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
</dependency>
<dependency> 
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

这是我的代码java:

        uri = new MongoClientURI(mongodb://[user]:[password]@[hostname]:27017,[hostname]:27017,[hostname]:27017/test?ssl=true&replicaSet=[CLUSTER_NAME]-shard-0&authSource=admin&retryWrites=true);

        mongoClient = new MongoClient(uri);
        database = mongoClient.getDatabase(NAME_DATABASE);

参考:

https://docs.atlas.mongodb.com/driver-connection/#java-driver-example

答案

你的mongoDb数据库是否正在运行?你能通过命令行连接到mongo吗?

以上是关于无法将我的spring-boot应用程序与mongodb云图集连接起来的主要内容,如果未能解决你的问题,请参考以下文章

spring-boot 单元测试获取应用程序属性

无法将docker Spring-Boot应用程序与docker-compose中的mysql容器和flyway连接起来

spring-boot 无法在 docker 中启动

将 Spring Boot 从 2.4.X 升级到 2.6.X 后无法运行我的 jar

Vaadin Flow 是不是支持 Spring-Boot Native

无法在 IntelliJ Idea 中启动 spring-boot 应用程序