MongoDB 副本模式,会映射到本地 127.0.0.1 错误

本文涉及的产品
云数据库 MongoDB,独享型 2核8GB
推荐场景:
构建全方位客户视图
简介: MongoDB 副本模式,会映射到本地 127.0.0.1 错误

基于 MongoDB 读写分离——Windows MongoDB 副本集配置 ,里面配置了一个坑,导致出现下列错误

[2021-05-10 10:06:11.981] [cluster-ClusterId{value='60989513a224157532b8e092', description='null'}-172.16.3.40:27018] [INFO ] org.mongodb.driver.connection - Opened connection [connectionId{localValue:3, serverValue:819}] to 172.16.3.40:27018
[2021-05-10 10:06:11.981] [cluster-ClusterId{value='60989513a224157532b8e092', description='null'}-172.16.3.40:27019] [INFO ] org.mongodb.driver.connection - Opened connection [connectionId{localValue:1, serverValue:433}] to 172.16.3.40:27019
[2021-05-10 10:06:11.981] [cluster-ClusterId{value='60989513a224157532b8e092', description='null'}-172.16.3.40:27017] [INFO ] org.mongodb.driver.connection - Opened connection [connectionId{localValue:2, serverValue:532}] to 172.16.3.40:27017
[2021-05-10 10:06:11.987] [cluster-ClusterId{value='60989513a224157532b8e092', description='null'}-172.16.3.40:27017] [INFO ] org.mongodb.driver.cluster - Monitor thread successfully connected to server with description ServerDescription{address=172.16.3.40:27017, type=REPLICA_SET_PRIMARY, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=3758700, setName='rs0', canonicalAddress=127.0.0.1:27017, hosts=[127.0.0.1:27019, 127.0.0.1:27017, 127.0.0.1:27018], passives=[], arbiters=[], primary='127.0.0.1:27017', tagSet=TagSet{[]}, electionId=7fffffff000000000000001d, setVersion=1, lastWriteDate=Mon May 10 10:06:07 CST 2021, lastUpdateTimeNanos=4601054829900}
[2021-05-10 10:06:11.987] [cluster-ClusterId{value='60989513a224157532b8e092', description='null'}-172.16.3.40:27019] [INFO ] org.mongodb.driver.cluster - Monitor thread successfully connected to server with description ServerDescription{address=172.16.3.40:27019, type=REPLICA_SET_SECONDARY, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=3789600, setName='rs0', canonicalAddress=127.0.0.1:27019, hosts=[127.0.0.1:27019, 127.0.0.1:27017, 127.0.0.1:27018], passives=[], arbiters=[], primary='127.0.0.1:27017', tagSet=TagSet{[]}, electionId=null, setVersion=1, lastWriteDate=Mon May 10 10:06:07 CST 2021, lastUpdateTimeNanos=4601054830000}
[2021-05-10 10:06:11.987] [cluster-ClusterId{value='60989513a224157532b8e092', description='null'}-172.16.3.40:27018] [INFO ] org.mongodb.driver.cluster - Monitor thread successfully connected to server with description ServerDescription{address=172.16.3.40:27018, type=REPLICA_SET_SECONDARY, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=3726600, setName='rs0', canonicalAddress=127.0.0.1:27018, hosts=[127.0.0.1:27019, 127.0.0.1:27017, 127.0.0.1:27018], passives=[], arbiters=[], primary='127.0.0.1:27017', tagSet=TagSet{[]}, electionId=null, setVersion=1, lastWriteDate=Mon May 10 10:06:07 CST 2021, lastUpdateTimeNanos=4601054829900}
[2021-05-10 10:06:11.988] [cluster-ClusterId{value='60989513a224157532b8e092', description='null'}-172.16.3.40:27018] [INFO ] org.mongodb.driver.cluster - Adding discovered server 127.0.0.1:27019 to client view of cluster
[2021-05-10 10:06:11.989] [cluster-ClusterId{value='60989513a224157532b8e092', description='null'}-172.16.3.40:27018] [INFO ] org.mongodb.driver.cluster - Adding discovered server 127.0.0.1:27017 to client view of cluster
[2021-05-10 10:06:11.990] [cluster-ClusterId{value='60989513a224157532b8e092', description='null'}-172.16.3.40:27018] [INFO ] org.mongodb.driver.cluster - Adding discovered server 127.0.0.1:27018 to client view of cluster
[2021-05-10 10:06:11.990] [cluster-ClusterId{value='60989513a224157532b8e092', description='null'}-172.16.3.40:27018] [INFO ] org.mongodb.driver.cluster - Canonical address 127.0.0.1:27018 does not match server address.  Removing 172.16.3.40:27018 from client view of cluster
[2021-05-10 10:06:11.992] [cluster-ClusterId{value='60989513a224157532b8e092', description='null'}-172.16.3.40:27017] [INFO ] org.mongodb.driver.cluster - Server 172.16.3.40:27019 is no longer a member of the replica set.  Removing from client view of cluster.
[2021-05-10 10:06:11.992] [cluster-ClusterId{value='60989513a224157532b8e092', description='null'}-172.16.3.40:27017] [INFO ] org.mongodb.driver.cluster - Server 172.16.3.40:27017 is no longer a member of the replica set.  Removing from client view of cluster.
[2021-05-10 10:06:11.992] [cluster-ClusterId{value='60989513a224157532b8e092', description='null'}-172.16.3.40:27017] [INFO ] org.mongodb.driver.cluster - Canonical address 127.0.0.1:27017 does not match server address.  Removing 172.16.3.40:27017 from client view of cluster
[2021-05-10 10:06:12.595] [main] [INFO ] o.s.scheduling.concurrent.ThreadPoolTaskExecutor - Initializing ExecutorService 'applicationTaskExecutor'
[2021-05-10 10:06:12.945] [main] [INFO ] com.iron.VipSoftTest - Started AlgorithmTest in 3.608 seconds (JVM running for 4.841)
[2021-05-10 10:06:12.947] [main] [INFO ] c.t.ecgreport.algorithm.handler.ApplicationHandler - AI Algorithm 服务已运行
[2021-05-10 10:06:13.193] [main] [INFO ] org.mongodb.driver.cluster - No server chosen by com.mongodb.client.internal.MongoClientDelegate$1@79617c3d from cluster description ClusterDescription{type=REPLICA_SET, connectionMode=MULTIPLE, serverDescriptions=[ServerDescription{address=127.0.0.1:27019, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=127.0.0.1:27018, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=127.0.0.1:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
[2021-05-10 10:06:14.030] [cluster-ClusterId{value='60989513a224157532b8e092', description='null'}-127.0.0.1:27019] [INFO ] org.mongodb.driver.cluster - Exception in monitor thread while connecting to server 127.0.0.1:27019
com.mongodb.MongoSocketOpenException: Exception opening socket
    at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70)
    at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:127)
    at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:606)
    at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:63)
    at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79)
    at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65)
    ... 3 common frames omitted

 

因为 副本集配置时,配置信息错误导致

rsconf = {
   _id: "rs0",
   members: [
     {
      _id: 0,
      host: "127.0.0.1:27017" 【这个要是对外IP,否则代码连接时,会转到本地127.0.0.1地址】
     },
     {
      _id: 1,
      host: "127.0.0.1:27018"
     },
     {
      _id: 2,
      host: "127.0.0.1:27019"
     }
    ]
}
rs.initiate(rsconf)

解决方案

cfg = rs.conf()
cfg.members[0].host = "172.16.3.40:27017"
cfg.members[1].host = "172.16.3.40:27018"
cfg.members[2].host = "172.16.3.40:27019"
rs.reconfig(cfg)
rs.status()

 

相关实践学习
MongoDB数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。   相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
目录
相关文章
|
运维 NoSQL 安全
【最佳实践】高可用mongodb集群(1分片+3副本):规划及部署
结合我们的生产需求,本次详细整理了最新版本 MonogoDB 7.0 集群的规划及部署过程,具有较大的参考价值,基本可照搬使用。 适应数据规模为T级的场景,由于设计了分片支撑,后续如有大数据量需求,可分片横向扩展。
1145 1
|
存储 缓存 负载均衡
高可用mongodb集群(分片+副本):规划及部署
高可用mongodb集群(分片+副本):规划及部署
1231 0
|
NoSQL MongoDB Docker
Docker Compose安装MongoDB,并向宿主机映射数据文件/配置文件/日志文件
本文为博主实践Docker Compose方式安装MongoDB记录,希望对大家有所帮助。
3039 0
|
存储 NoSQL Java
高可用mongodb集群(分片+副本):性能测试
高可用mongodb集群(分片+副本):性能测试
673 0
|
6月前
|
JSON NoSQL MongoDB
实时计算 Flink版产品使用合集之要将收集到的 MongoDB 数据映射成 JSON 对象而非按字段分割,该怎么操作
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStream API、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。
|
NoSQL MongoDB 数据安全/隐私保护
Docker 安装MongoDB 以及 相关配置文件、数据文件映射
本文是博主使用docker安装mongodb的方法记录,希望对大家有所帮助。
1956 0
|
3月前
|
存储 NoSQL 算法
MongoDB保姆级指南(中):从副本集群、分片集群起航,探索分布式存储的趋势!
本文一起来聊聊MongoDB集群,顺带以MongoDB集群为起点,共同探讨一下分布式存储的发展趋势~
226 15
|
3月前
|
存储 监控 NoSQL
震撼!揭秘高可用 MongoDB 分片集群搭建的神秘魔法,开启数据存储的无敌模式!
【8月更文挑战第9天】在数字化时代,数据至关重要。MongoDB作为流行非关系型数据库,通过搭建高可用分片集群确保系统稳定性和性能。分片技术将大数据集分布于多服务器以实现水平扩展。搭建集群需准备服务器资源,配置环境,启动配置服务器、路由服务器及分片服务器,并设置分片策略。例如,对特定数据库和集合启用分片。此架构适用于高流量应用如大型电商平台,确保数据高效处理和高可用性。搭建过程需持续监控和优化,合理规划分片策略以维持系统稳定运行。
39 3
|
5月前
|
存储 负载均衡 NoSQL
MongoDB的架构设计基于三种集群模式
【6月更文挑战第5天】MongoDB的架构设计基于三种集群模式
220 3
|
6月前
|
存储 NoSQL MongoDB
MongoDB 助力移动式汽车保养运营模式优化,将开发请求减少 90%
MongoDB针对初级,中级及熟练的技术开发人员推出系列技术文章与行业案例。深入浅出地剖析MongoDB产品基础原理,使用技巧,典型行业场景及应用,还有Code Demo及线上线下活动推荐!
4893 1
MongoDB 助力移动式汽车保养运营模式优化,将开发请求减少 90%