MongoDB认证——从MONGODB-CR升级为SCRAM-SHA-

15 篇文章 0 订阅

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值