情况描述如下:
1、用集群连接方式创建mongodb连接
List seeds = new ArrayList<>();
for(String h : host_list){
ServerAddress sa = new ServerAddress(h,port);
seeds.add(sa);
}
MongoOptions opt = new MongoOptions();
opt.connectionsPerHost = poolSize;
opt.threadsAllowedToBlockForConnectionMultiplier = blockSize;
mg = new Mongo(seeds, opt);
2、其中一个ip是虚假的,为了测试集群连接方式能够正常工作。
3、验证登录
db_con.authenticate(user, password.toCharArray());
问题所在:
如果用debug模式,手动跟踪代码,在有ip无效的情况下,验证能正常通过,能进行下面的mongodb操作。
但是如果正常执行,验证不通过,会提示mongodb所操作的集合需要认证。如下
com.mongodb.MongoException: Rare case where master=null, probably all servers are down
at com.mongodb.DBTCPConnector$MyPort.get(DBTCPConnector.java:438)
at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:280)
at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:257)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:310)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:295)
at com.mongodb.DBCursor._check(DBCursor.java:368)
at com.mongodb.DBCursor._hasNext(DBCursor.java:459)
at com.mongodb.DBCursor.hasNext(DBCursor.java:484)
at com.hw.jgpt.mongo.SimpleTest.main(SimpleTest.java:98)
如果在 mg = new Mongo(seeds, opt); 这句话之后加上休眠语句,会报错:
com.mongodb.MongoException: not authorized for query on ************************
at com.mongodb.MongoException.parse(MongoException.java:82)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:314)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:295)
at com.mongodb.DBCursor._check(DBCursor.java:368)
at com.mongodb.DBCursor._hasNext(DBCursor.java:459)
at com.mongodb.DBCursor.hasNext(DBCursor.java:484)
at com.hw.jgpt.mongo.SimpleTest.main(SimpleTest.java:98)