Java MongoClient无法连接到主服务器

时间:2014-05-19 23:44:20

标签: java mongodb mongodb-java

我有一个副本集设置,包含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
           }]
       }
   }

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

com.mongodb.MongoServerSelectionException: 
Unable to connect to any server that matches{

    serverSelectors=[ReadPreferenceServerSelector{
        readPreference=primary
    }, 
    LatencyMinimizingServerSelector{
        acceptableLatencyDifference=10000 ms
    }]
 }

1 个答案:

答案 0 :(得分:1)

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

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