Java MongoClient无法连接到主数据库 [英] Java MongoClient cannot connect to primary

查看:75
本文介绍了Java MongoClient无法连接到主数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个副本集设置,其中包含1个主副本(mongo1.test.com),1个辅助副本(mongo2.test.com)和1个仲裁者(mongo3.test.com).当我使用MongoClient连接到它们并打印出ReplicaSetStatus时,它表明mongo1.test.com的类型= Unknown断开连接,mongo2.test.com的类型= ReplicaSetSecondary,mongo3.test.com的连接类型为type = Unknown.因为它不知道哪个是主要查询,所以我可以查找查询,但不能插入或更新.

I have a replica set setup with 1 primary (mongo1.test.com), 1 secondary(mongo2.test.com) and 1 arbiter(mongo3.test.com). When I use MongoClient to connect to them and print out the ReplicaSetStatus, it shows that mongo1.test.com is unconnected with type=Unknown, mongo2.test.com as type=ReplicaSetSecondary, and mongo3.test.com as type=Unknown. Because it doesn't know which one is the primary, I can do find queries but I cannot insert or update.

现在我不知道这是Mongo的设置还是驱动程序的配置问题.有什么建议吗?

Right now I don't know if it is the setup with Mongo or a config issue with the driver. Any advice?

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

Mongo version 2.6.1 Java Mongo driver version 2.12.1 Mongo installed on 3 seperate Amazon EC2 Linux servers.

代码如下:

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 printout:

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
           }]
       }
   }

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

Calling insert on any DB gives following error:

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服务器日志以查看提示信息.也许停止该服务并在那里启动它以获取日志,以使您对问题有一个全新的了解.

I think your issue isn't the Java driver but the ReplSet itself. Fire up a mongo shell and do rs.status(). You'll likely see output almost exactly like the stuff the Java driver is giving you. It seems that your mongo1.test.com is either offline entirely or not starting up. SSH onto mongo1.test.com and see if you can mongo shell from there. My bet is "no". Tail the mongo server log to see what that tells you. Maybe stop the service and start it there to get the log to give you a fresh view of the issue.

好消息是,我认为您的Java代码实际上正在运行,您在ReplSet配置中只有一个奇怪的mongod.取消整理后,ReplSet会自愈,然后您便会康复.

The good news is, I think your Java code is actually working, you just have a dorked mongod in the ReplSet configuration. Undork it, the ReplSet will heal itself, and you'll be on your way.

这篇关于Java MongoClient无法连接到主数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆