【问题标题】:Java MongoClient cannot connect to primaryJava MongoClient 无法连接到主节点
【发布时间】:2014-05-19 23:44:19
【问题描述】:

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

【问题讨论】:

  • 您似乎没有主节点。你在 Unknown 中有一个节点,一个 Secondary 和一个 Arbiter。所以,是的,这是您的 Mongo 集群设置的问题。尝试查看 mongod 日志。

标签: java mongodb mongodb-java


【解决方案1】:

我认为您的问题不是 Java 驱动程序,而是 ReplSet 本身。启动一个 mongo shell 并执行 rs.status()。您可能会看到几乎与 Java 驱动程序提供给您的内容完全相同的输出。您的 mongo1.test.com 似乎完全离线或未启动。 SSH 到 mongo1.test.com,看看你是否可以从那里获得 mongo shell。我的赌注是“不”。跟踪 mongo 服务器日志,看看它告诉你什么。也许停止服务并在那里启动它以获得日志,让您对问题有一个全新的看法。

好消息是,我认为您的 Java 代码实际上可以工作,您只是在 ReplSet 配置中有一个笨拙的 mongod。取消它,ReplSet 会自行修复,然后您就可以上路了。

【讨论】:

  • 看起来像你说的那样是一个 replSet 问题,但我能够从 mongo 1 开始 mongo shell 没问题。问题是主服务器的主机名不是我在种子列表中传递给驱动程序的 URL。当我创建 replSet 时,我 mongo'ed 进入 mongo 1 并将 mongo 2 和 3 添加到 replSet。默认情况下,mongo 1 的主机名变成了 linux 服务器的名称(ip-xxx-xxx-x-x)。我将 replSet 上的主服务器的主机名更改为 mongo1.test.com,一切正常。谢谢!
猜你喜欢
  • 2016-12-10
  • 1970-01-01
  • 1970-01-01
  • 2018-06-30
  • 1970-01-01
  • 2015-03-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-21
相关资源
最近更新 更多