MongoDB数据库用Robomongo可以远程连接,但是用java代码行不行,无法认证,报下面日志的错误:
发现Robomongo的认证中有一个选项 Mechanism:
因为我使用的mongoDB版本是2.6.7,所以Mechanism只能使用MONGODB-CR而不是SCRAM-SHA
所以需要修改代码
//原来的
MongoCredential credential = MongoCredential.createScramSha1Credential(
USERNAME, DBName, PASSWORD.toCharArray());
//修改后的
MongoCredential credential = MongoCredential.createCredential(
USERNAME, DBName, PASSWORD.toCharArray());
Sep 25, 2019 11:51:08 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[test:27017], mode=MULTIPLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=1000}
Sep 25, 2019 11:51:08 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Adding discovered server coq-a0020964:27017 to client view of cluster
Connect to DataBase successfully
Sep 25, 2019 11:51:08 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Exception in monitor thread while connecting to server test:27017
com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='log', source='mars', password=<hidden>, mechanismProperties={}}
at com.mongodb.connection.SaslAuthenticator.wrapException(SaslAuthenticator.java:162)
at com.mongodb.connection.SaslAuthenticator.access$200(SaslAuthenticator.java:39)
at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:68)
at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:46)
at com.mongodb.connection.SaslAuthenticator.doAsSubject(SaslAuthenticator.java:168)
at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:46)
at com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:122)
at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:52)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:127)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114)
at java.lang.Thread.run(Unknown Source)
Caused by: com.mongodb.MongoCommandException: Command failed with error 59: 'no such cmd: saslStart' on server coq-a0020964:27017. The full response is { "ok" : 0.0, "errmsg" : "no such cmd: saslStart", "code" : 59, "bad cmd" : { "saslStart" : 1, "mechanism" : "SCRAM-SHA-1", "payload" : { "$binary" : "biwsbj1sb2cscj1VImtVI20zPmtmSnpUc3lJcSlAQGpacSk=", "$type" : "00" } } }
at com.mongodb.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:164)
at com.mongodb.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:295)
at com.mongodb.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255)
at com.mongodb.connection.CommandHelper.sendAndReceive(CommandHelper.java:84)
at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:34)
at com.mongodb.connection.SaslAuthenticator.sendSaslStart(SaslAuthenticator.java:119)
at com.mongodb.connection.SaslAuthenticator.access$000(SaslAuthenticator.java:39)
at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:52)
... 8 more
Sep 25, 2019 11:51:08 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster description not yet available. Waiting for 30000 ms before timing out