Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. try{
  2. MongoCredential credential = MongoCredential.createCredential("user", "admin",
  3. "password".toCharArray());
  4.  
  5. ServerAddress address = new ServerAddress("localhost", 27017);
  6. mongoClient = new MongoClient(address, Arrays.asList(credential));
  7. }catch (MongoSecurityException e) {
  8. e.printStackTrace();
  9. }
  10.  
  11. May 24, 2017 1:01:08 AM com.mongodb.diagnostics.logging.JULLogger log
  12. INFO: Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
  13. May 24, 2017 1:01:08 AM com.mongodb.diagnostics.logging.JULLogger log
  14. INFO: Exception in monitor thread while connecting to server localhost:27017
  15. com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='test', source='admin', password=<hidden>, mechanismProperties={}}
  16. at com.mongodb.connection.SaslAuthenticator.wrapInMongoSecurityException(SaslAuthenticator.java:157)
  17. at com.mongodb.connection.SaslAuthenticator.access$200(SaslAuthenticator.java:37)
  18. at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:66)
  19. at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:44)
  20. at com.mongodb.connection.SaslAuthenticator.doAsSubject(SaslAuthenticator.java:162)
  21. at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:44)
  22. at com.mongodb.connection.DefaultAuthenticator.authenticate(DefaultAuthenticator.java:32)
  23. at com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:109)
  24. at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:46)
  25. at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:116)
  26. at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113)
  27. at java.lang.Thread.run(Thread.java:745)
  28. Caused by: com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" }
  29. at com.mongodb.connection.CommandHelper.createCommandFailureException(CommandHelper.java:170)
  30. at com.mongodb.connection.CommandHelper.receiveCommandResult(CommandHelper.java:123)
  31. at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32)
  32. at com.mongodb.connection.SaslAuthenticator.sendSaslStart(SaslAuthenticator.java:117)
  33. at com.mongodb.connection.SaslAuthenticator.access$000(SaslAuthenticator.java:37)
  34. at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:50)
  35. ... 9 more
  36.  
  37. MongoClientOptions clientOptions = new MongoClientOptions.Builder().serverSelectionTimeout(500).build();
  38. mongoClient = new MongoClient(serverAddress, Collections.singletonList(credential), clientOptions);
  39. try {
  40. String address = mongoClient.getConnectPoint();
  41. System.out.println(address);
  42. }catch (Throwable e){
  43. System.out.println(e);
  44. }
  45.  
  46. {
  47. MongoClientOptions clientOptions = new MongoClientOptions.Builder().addServerListener(this).build();
  48. mongoClient = new MongoClient(host1, Collections.singletonList(credential), clientOptions);
  49. }
  50.  
  51. @Override
  52. public void serverDescriptionChanged(ServerDescriptionChangedEvent event) {
  53. Throwable exception = event.getNewDescription().getException();
  54. handle(exception);
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement