1.springboot连接mongoDB 出现异常认证
异常详情:
com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='admin', source='admin', password=<hidden>, mechanismProperties={}}
at com.mongodb.connection.SaslAuthenticator.wrapException(SaslAuthenticator.java:162) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.SaslAuthenticator.access$200(SaslAuthenticator.java:39) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:68) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:46) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.SaslAuthenticator.doAsSubject(SaslAuthenticator.java:168) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:46) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.DefaultAuthenticator.authenticate(DefaultAuthenticator.java:32) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:122) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:52) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:127) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114) ~[mongodb-driver-core-3.6.4.jar:na]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_172]
Caused by: com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server 192.9.104.125:29017. The full response is { "ok" : 0.0, "code" : 18, "errmsg" : "Authentication failed." }
at com.mongodb.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:164) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:295) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.CommandHelper.sendAndReceive(CommandHelper.java:84) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:34) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.SaslAuthenticator.sendSaslStart(SaslAuthenticator.java:119) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.SaslAuthenticator.access$000(SaslAuthenticator.java:39) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:52) ~[mongodb-driver-core-3.6.4.jar:na]
... 9 common frames omitted
解决方案:
-
报错解决之前,MongoDB 数据库连接配置
-
spring: data: mongodb: host: 127.0.0.1 #mongoDB地址 port: 8888 #当前mongoDB的端口号 database: javaAnalysisWord #当前数据库的名称 username: xiaoming #当前数据库的用户名 password: 123456 #当前数据库的用户密码
-
报错解决之后,MongoDB 数据库连接配置
-
spring: data: mongodb: uri: mongodb://xiaoming:123456@localhost:8888/javaAnalysisWord
-
全配置
-
spring: data: mongodb: uri: mongodb://xiaoming:123456@localhost:8888/javaAnalysisWord # 需要用户名和密码认证 #uri: mongodb://username:password@ip:port/admin #不需要用户名和密码认证 #uri: mongodb://ip:port/admin