[Solved] org.mongodb.driver.cluster – Exception in monitor thread while connecting to server node1:27017

spring   An error was reported when the boot project started to connect mongodb.

2021-06-09 17:10:34,663 INFO  org.mongodb.driver.cluster - Exception in monitor thread while connecting to server node1:27017
com.mongodb.MongoSocketException: node1
	at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:211)
	at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:75)
	at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65)
	at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:128)
	at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117)
	at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.UnknownHostException: node1
	at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
	at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928)
	at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323)
	at java.net.InetAddress.getAllByName0(InetAddress.java:1276)
	at java.net.InetAddress.getAllByName(InetAddress.java:1192)
	at java.net.InetAddress.getAllByName(InetAddress.java:1126)
	at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:203)
	... 5 common frames omitted

Because it’s a new project, Mongo library belongs to other projects. This is just a test application. So the first doubt is that the project version driver is not compatible with mongodb library. After replacing other versions of mongodb library, it is OK.

Detailed explanation

View the

<dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>2.1.3.RELEASE</version>
    </dependency>

Because of spring   Mongodb driver is also the official driver of encapsulated mongodb. When you enter the spring data mongodb project, you can see that the official version of mongodb driver is 3.8.2

Then go to the official website of mongodb to see that version 3.8 supports version 4.0 database at most. I started testing with the 4.2 library (Official website https://docs.mongodb.com/drivers/java/)

So that’s it.

Read More: