我在连接到db时遇到问题,这是最近实现的副本集。
有两种连接方式,一种是通过URI,另一种是使用服务器地址和凭证列表创建mongo客户端
他们都没有工作,
MongoClientURI uri = new MongoClientURI(getDatabaseURI()+getDatabaseName()+"?replicaSet=test-replset&readPreference=primary&authMechanism=SCRAM-SHA1");
MongoClient client = new MongoClient(uri);
答案 0 :(得分:0)
我找到了答案
List<MongoCredential> credentialsList = new ArrayList<MongoCredential();
credentialsList.add(MongoCredential.createCredential("USER",
"DB_NAME", "PASSWORD".toCharArray()));
client = new MongoClient("LIST<ServerAddress> YOURSERVERLIST" ,
credentialsList,
MongoClientOptions.builder()
.connectTimeout(TIME_OUT)
.readPreference(ReadPreference.secondaryPreferred())
.build()
);