Java MongoClient无法连接到主服务器

我有一个副本集设置,1个主要(mongo1.test.com),1个辅助(mongo2.test.com)和1个仲裁(mongo3.test.com)。 当我使用MongoClient连接到它们并打印出ReplicaSetStatus时,它显示mongo1.test.com与type = Unknown无关,mongo2.test.com为type = ReplicaSetSecondary,mongo3.test.com为type = Unknown。 因为它不知道哪一个是主要的,我可以找到查询,但我无法插入或更新。

现在我不知道它是Mongo的设置还是驱动程序的配置问题。 任何建议?

Mongo版本2.6.1 Java Mongo驱动程序版本2.12.1 Mongo安装在3个单独的Amazon EC2 Linux服务器上。

这是代码:

MongoClientOptions.Builder optionsBuilder = MongoClientOptions.builder() .acceptableLatencyDifference(10000) .writeConcern(WriteConcern.REPLICA_ACKNOWLEDGED) .readPreference(ReadPreference.secondaryPreferred()) .connectionsPerHost(10) .connectTimeout(15000) .maxWaitTime(30000) .socketTimeout(60000) .threadsAllowedToBlockForConnectionMultiplier(1500); MongoClientOptions options = optionsBuilder.build(); MongoClient mc = new MongoClient(Arrays.asList( new ServerAddress("mongo1.test.com",27017), new ServerAddress("mongo2.test.com",27018), new ServerAddress("mongo3.test.com",27019)), Arrays.asList(MongoCredential.createMongoCRCredential(xxx, "admin", xxx.toCharArray()))), options); System.out.println(mc.getRelicaSetStatus()); 

ReplicaSetStatus打印输出:

 ReplicaSetStatus { name=eeRS1, cluster=ClusterDescription{ type=ReplicaSet, connectionMode=Multiple, all=[ServerDescription{ address=PRIVATE IP:27017, type=Unknown, hosts=[], passives=[], arbiters=[], primary='null', maxDocumentSize=16777216, maxMessageSize=33554432, maxWriteBatchSize=512, tags={}, setName='null', setVersion='null', averagePingTimeNanos=0, ok=false, state=Unconnected, version=ServerVersion{ versionList=[0, 0, 0] }, minWireVersion=0, maxWireVersion=0 }, ServerDescription{ address=mongo2.test.com:27018, type=ReplicaSetSecondary, hosts=[PRIVATE IP:27017, mongo2.test.com:27018], passives=[], arbiters=[mongo3.test.com:27019], primary='PRIVATE IP:27017', maxDocumentSize=16777216, maxMessageSize=48000000, maxWriteBatchSize=1000, tags={}, setName='eeRS1', setVersion='17', averagePingTimeNanos=215754657, ok=true, state=Connected, version=ServerVersion{ versionList=[2, 6, 1] }, minWireVersion=0, maxWireVersion=2 }, ServerDescription{ address=mongo3.test.com:27019, type=ReplicaSetArbiter, hosts=[PRIVATE IP:27017, mongo2.test.com:27018], passives=[], arbiters=[mongo3.test.com:27019], primary='PRIVATE IP:27017', maxDocumentSize=16777216, maxMessageSize=48000000, maxWriteBatchSize=1000, tags={}, setName='eeRS1', setVersion='17', averagePingTimeNanos=132660144, ok=true, state=Connected, version=ServerVersion{ versionList=[2, 6, 1] }, minWireVersion=0, maxWireVersion=2 }] } } 

在任何DB上调用insert都会出现以下错误:

 com.mongodb.MongoServerSelectionException: Unable to connect to any server that matches{ serverSelectors=[ReadPreferenceServerSelector{ readPreference=primary }, LatencyMinimizingServerSelector{ acceptableLatencyDifference=10000 ms }] } 

我认为你的问题不是Java驱动程序,而是ReplSet本身。 启动一个mongo shell并执行rs.status()。 你可能会看到输出几乎与Java驱动程序给你的东西完全一样。 您的mongo1.test.com似乎完全脱机或未启动。 SSH到mongo1.test.com,看看你是否可以从那里进行mongo shell。 我的赌注是“不”。 关闭mongo服务器日志以查看它告诉您的内容。 也许停止服务并在那里启动它以获取日志,让您全面了解问题。

好消息是,我认为你的Java代码实际上正在运行,你只需要在ReplSet配置中使用一个dorked mongod。 Undork它,ReplSet将自我修复,你将在你的路上。