问题
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.
Right now I don't know if it is the setup with Mongo or a config issue with the driver. Any advice?
Mongo version 2.6.1 Java Mongo driver version 2.12.1 Mongo installed on 3 seperate Amazon EC2 Linux servers.
Here's the code:
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 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
}]
}
回答1:
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.
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.
来源:https://stackoverflow.com/questions/23748868/java-mongoclient-cannot-connect-to-primary