基于 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.thoth.ecgreport.algorithm.AlgorithmTest - 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()