问题
CentOS 7 + docker + mongodb 4.4.4 部署了一个 mongodb 集群,然后在项目(Java-springboot)中去连接 mongodb 进行数据的读写,出现如下提示:
2021-05-12 13:42:33.003 INFO 17864 --- [io-8084-exec-10] org.mongodb.driver.cluster : Cluster created with settings {hosts=[121.*.*.56:27017], mode=MULTIPLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=1500000}
2021-05-12 13:42:33.003 INFO 17864 --- [io-8084-exec-10] org.mongodb.driver.cluster : Adding discovered server 121.*.*.56:27017 to client view of cluster
2021-05-12 13:42:33.006 INFO 17864 --- [io-8084-exec-10] org.mongodb.driver.cluster : No server chosen by WritableServerSelector from cluster description ClusterDescription{type=UNKNOWN, connectionMode=MULTIPLE, serverDescriptions=[ServerDescription{address=121.36.16.56:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
2021-05-12 13:42:33.108 INFO 17864 --- [.*.*.56:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:985, serverValue:377}] to 121.*.*.56:27017
2021-05-12 13:42:33.133 INFO 17864 --- [.*.*.56:27017] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=121.*.*.56:27017, type=REPLICA_SET_PRIMARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 4, 4]}, minWireVersion=0, maxWireVersion=9, maxDocumentSize=16777216, roundTripTimeNanos=25262872, setName='mongoreplset', canonicalAddress=172.17.0.4:27017, hosts=[172.17.0.4:27017, 121.*.*.103:27017, 172.17.0.6:27017], passives=[], arbiters=[], primary='172.17.0.4:27017', tagSet=TagSet{[]}, electionId=7fffffff0000000000000024, setVersion=2, lastWriteDate=Wed May 12 13:42:42 CST 2021, lastUpdateTimeNanos=533944664886564}
2021-05-12 13:42:33.134 INFO 17864 --- [.*.*.56:27017] org.mongodb.driver.cluster : Discovered cluster type of REPLICA_SET
2021-05-12 13:42:33.134 INFO 17864 --- [.*.*.56:27017] org.mongodb.driver.cluster : Adding discovered server 172.17.0.4:27017 to client view of cluster
2021-05-12 13:42:33.135 INFO 17864 --- [.*.*.56:27017] org.mongodb.driver.cluster : Adding discovered server 121.*.*.103:27017 to client view of cluster
2021-05-12 13:42:33.136 INFO 17864 --- [.*.*.56:27017] org.mongodb.driver.cluster : Adding discovered server 172.17.0.6:27017 to client view of cluster
2021-05-12 13:42:33.137 INFO 17864 --- [.*.*.56:27017] org.mongodb.driver.cluster : Server 121.*.*.56:27017 is no longer a member of the replica set. Removing from client view of cluster.
2021-05-12 13:42:33.137 INFO 17864 --- [.*.*.56:27017] org.mongodb.driver.cluster : Canonical address 172.17.0.4:27017 does not match server address. Removing 121.36.16.56:27017 from client view of cluster
2021-05-12 13:42:34.212 INFO 17864 --- [*.*.103:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server 121.*.*.103:27017
进入 mongodb 容器中,查看当前 rs.conf()
mongoreplset:PRIMARY> rs.conf()
{
"_id" : "mongoreplset",
"version" : 2,
"term" : 36,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "172.17.0.4:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "172.17.0.6:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
...
可以看到现在的复制集中使用的是非外网 IP 配置的,此时,在项目中连接时就会出现上面的连接不通问题。
后来重新部署,然后将其中的 host 换为服务器 IP:port 后,就可以正常连接读写数据了。
问题:
是否只能如此配置外网 ip?
如果配置宿主机内的 ip ,应该如何配置才能不报错,是不是需要配置 宿主机上的 hosts 映射到内网 ip ?