java連線mongodb3.2驗證失敗
阿新 • • 發佈:2019-02-19
一、錯誤描述
在專案中使用java連線mongodb資料庫,使用的是mongoclient連線,程式碼如下:
public void mongodbTest (String ip,int port,String user,String password,String dbname){ MongoDatabase db=null; MongoClient client=null; try{ MongoClientOptions.Builder build = new MongoClientOptions.Builder(); //與資料最大連線數50 build.connectionsPerHost(50); //如果當前所有的connection都在使用中,則每個connection上可以有50個執行緒排隊等待 build.threadsAllowedToBlockForConnectionMultiplier(50); build.connectTimeout(1*60*1000); build.maxWaitTime(2*60*1000); MongoClientOptions options = build.build(); //設定伺服器資訊 ServerAddress serverAddress = new ServerAddress(ip, port); List<ServerAddress> seeds = new ArrayList<ServerAddress>(); seeds.add(serverAddress); //設定驗證資訊 MongoCredential credentials = MongoCredential.createMongoCRCredential(user, dbname, password.toCharArray()); List<MongoCredential> credentialsList = new ArrayList<MongoCredential>(); credentialsList.add(credential); client =new MongoClient(seeds, credentialsList,options); //獲取資料庫mydb,不存在的話,會自動建立該資料庫 db = client.getDatabase("mydb"); }catch (Exception e) { return e.getClass().getName() + ": " + e.getMessage(); } if(db!=null&&client!=null){ //獲取集合 MongoCollection<Document> documents = db.getCollection("mydb"); Document document=documents.find().first(); System.out.println(document.toJson()); } client.close(); }
執行後,報如下錯誤
com.mongodb.MongoSecurityException: Exception authenticating at com.mongodb.connection.NativeAuthenticator.authenticate(NativeAuthenticator.java:48) at com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:109) at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:46) at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:116) at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113) at java.lang.Thread.run(Unknown Source) Caused by: com.mongodb.MongoCommandException: Command failed with error 18: 'auth failed' on server xxx.xxx.xxx.xxx:27017. The full response is { "ok" : 0.0, "errmsg" : "auth failed", "code" : 18 }at com.mongodb.connection.CommandHelper.createCommandFailureException(CommandHelper.java:170) at com.mongodb.connection.CommandHelper.receiveCommandResult(CommandHelper.java:123) at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32) at com.mongodb.connection.NativeAuthenticator.authenticate(NativeAuthenticator.java:46) ... 5 more
二、解決辦法
官網查了一下官網API,找到了問題的所在,文件如下
到mongodb資料庫的admin資料庫中一查,發現驗證機制為SCRAM-SHA-1,於是在java程式碼中將createMongoCRCredential()方法換成createScramShalCredential()方法,驗證通過,成功解決問題
public void mongodbTest (String ip,int port,String user,String password,String dbname){
MongoDatabase db=null;
MongoClient client=null;
try{
MongoClientOptions.Builder build = new MongoClientOptions.Builder();
//與資料最大連線數50
build.connectionsPerHost(50);
//如果當前所有的connection都在使用中,則每個connection上可以有50個執行緒排隊等待
build.threadsAllowedToBlockForConnectionMultiplier(50);
build.connectTimeout(1*60*1000);
build.maxWaitTime(2*60*1000);
MongoClientOptions options = build.build();
//設定伺服器資訊
ServerAddress serverAddress = new ServerAddress(ip, port);
List<ServerAddress> seeds = new ArrayList<ServerAddress>();
seeds.add(serverAddress);
//設定驗證資訊
MongoCredential credentials = MongoCredential.createScramSha1Credential(user, dbname, password.toCharArray());
List<MongoCredential> credentialsList = new ArrayList<MongoCredential>();
credentialsList.add(credential);
client =new MongoClient(seeds, credentialsList,options);
//獲取資料庫mydb,不存在的話,會自動建立該資料庫
db = client.getDatabase("mydb");
}catch (Exception e) {
return e.getClass().getName() + ": " + e.getMessage();
}
if(db!=null&&client!=null){
//獲取集合
MongoCollection<Document> documents = db.getCollection("mydb");
Document document=documents.find().first();
System.out.println(document.toJson());
}
client.close();
}