Mongodb规范地址与服务器地址不匹配

Posted

技术标签:

【中文标题】Mongodb规范地址与服务器地址不匹配【英文标题】:Mongo DB Canonical address does not match server address 【发布时间】:2019-09-08 12:06:00 【问题描述】:

我正在将我的 Spring Boot 应用程序与 mongo db 连接起来。

代码在本地环境中运行良好,其中 db 具有单个节点且未在集群模式下运行。

但是当 db 在集群模式和单节点和 1 个副本集的暂存环境中运行时,sp​​ring 应用程序显示以下错误:

INFO  |  |  | 2019-04-18 12:43:25 | [cluster-ClusterIdvalue='5cb823951a734cea7f7eebe1', description='null'-10.202.41.254:27017] com.mongodb.diagnostics.logging.SLF4JLogger - Canonical address educationelasticsearch-digital-v1-41-254:27017 does not match server address.  Removing 10.20.41.254:27017 from client view of cluster
INFO  |  |  | 2019-04-18 12:43:25 | [cluster-ClusterIdvalue='5cb823951a734cea7f7eebe1', description='null'-educationelasticsearch-digital-v1-41-254:27017] com.mongodb.diagnostics.logging.SLF4JLogger - Exception in monitor thread while connecting to server educationelasticsearch-digital-v1-41-254:27017
com.mongodb.MongoSocketException: educationelasticsearch-digital-v1-41-254: nodename nor servname provided, or not known
    at com.mongodb.ServerAddress.getSocketAddress(ServerAddress.java:188) ~[mongodb-driver-core-3.8.2.jar:?]
    at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64) ~[mongodb-driver-core-3.8.2.jar:?]
    at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:62) ~[mongodb-driver-core-3.8.2.jar:?]
    at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongodb-driver-core-3.8.2.jar:?]
    at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) [mongodb-driver-core-3.8.2.jar:?]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_171]
Caused by: java.net.UnknownHostException: educationelasticsearch-digital-v1-41-254: nodename nor servname provided, or not known
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) ~[?:1.8.0_171]
    at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928) ~[?:1.8.0_171]
    at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323) ~[?:1.8.0_171]
    at java.net.InetAddress.getAllByName0(InetAddress.java:1276) ~[?:1.8.0_171]
    at java.net.InetAddress.getAllByName(InetAddress.java:1192) ~[?:1.8.0_171]
    at java.net.InetAddress.getAllByName(InetAddress.java:1126) ~[?:1.8.0_171]
    at java.net.InetAddress.getByName(InetAddress.java:1076) ~[?:1.8.0_171]
    at com.mongodb.ServerAddress.getSocketAddress(ServerAddress.java:186) ~[mongodb-driver-core-3.8.2.jar:?]
    ... 5 more

在这两种情况下,应用程序属性都是相同的,只是临时服务器的 IP 发生了变化,而不是 localhost:

spring.elasticsearch.education.hostname=localhost   // staging server IP
spring.elasticsearch.education.scheme=http
spring.elasticsearch.education.port=9200


###### Mongo properties ########
spring.data.mongodb.host=localhost  //staging server ip
spring.data.mongodb.port=27017
spring.data.mongodb.database=ddddd
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=root
spring.data.mongodb.password=root
spring.data.mongodb.repositories.enabled=true
logging.level.org.springframework.data.mongodb.core.MongoTemplate=DEBUG

我检查了this question,但没有多少分辨率。

请提出这里有什么问题?

【问题讨论】:

【参考方案1】:

看这里https://***.com/a/34373673/5712629。

可能这个版本的mongodb驱动不支持spring.data.mongodb.host和spring.data.mongodb.port。

你可以试试:

spring.data.mongodb.uri=mongodb://root:root@localhost:27017

改为。

【讨论】:

以上是关于Mongodb规范地址与服务器地址不匹配的主要内容,如果未能解决你的问题,请参考以下文章

利用PYTHON匹配提取EXCEL表的省市区,有偿?

找不到与方案 net.tcp 匹配的基地址

如何在 WCF 中修复“找不到与架构 http 匹配的基地址”...

linux中匹配正确的ip地址

Linux Kernel 4.2.x:为啥检查时预期的系统调用地址与实际地址不匹配?

「Vue」起步 - vue-router路由与页面间导航