springboot连接mongodb报UncategorizedMongoDbException: Command failed with error 13 (Unauthorized)解决方法
Posted 秋9
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot连接mongodb报UncategorizedMongoDbException: Command failed with error 13 (Unauthorized)解决方法相关的知识,希望对你有一定的参考价值。
【现象】
failed; nested exception is org.springframework.data.mongodb.UncategorizedMongoDbException: Command failed with error 13 (Unauthorized): 'command insert requires authentication' on server localhost:27017. The full response is "ok": 0.0, "errmsg": "command insert requires authentication", "code": 13, "codeName": "Unauthorized"; nested exception is com.mongodb.MongoCommandException: Command failed with error 13 (Unauthorized): 'command insert requires authentication' on server localhost:27017. The full response is "ok": 0.0, "errmsg": "command insert requires authentication", "code": 13, "codeName": "Unauthorized"] with root cause
com.mongodb.MongoCommandException: Command failed with error 13 (Unauthorized): 'command insert requires authentication' on server localhost:27017. The full response is "ok": 0.0, "errmsg": "command insert requires authentication", "code": 13, "codeName": "Unauthorized"
at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:175) ~[mongodb-driver-core-4.2.3.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:358) ~[mongodb-driver-core-4.2.3.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:279) ~[mongodb-driver-core-4.2.3.jar:na]
at com.mongodb.internal.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:100) ~[mongodb-driver-core-4.2.3.jar:na]
at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:490) ~[mongodb-driver-core-4.2.3.jar:na]
at com.mongodb.internal.connection.CommandProtocolImpl.execute(CommandProtocolImpl.java:71) ~[mongodb-driver-core-4.2.3.jar:na]
at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:253) ~[mongodb-driver-core-4.2.3.jar:na]
at com.mongodb.internal.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:202) ~[mongodb-driver-core-4.2.3.jar:na]
at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:118) ~[mongodb-driver-core-4.2.3.jar:na]
at com.mongodb.internal.operation.MixedBulkWriteOperation.executeCommand(MixedBulkWriteOperation.java:431) ~[mongodb-driver-core-4.2.3.jar:na]
at com.mongodb.internal.operation.MixedBulkWriteOperation.executeBulkWriteBatch(MixedBulkWriteOperation.java:251) ~[mongodb-driver-core-4.2.3.jar:na]
at com.mongodb.internal.operation.MixedBulkWriteOperation.access$700(MixedBulkWriteOperation.java:76) ~[mongodb-driver-core-4.2.3.jar:na]
at com.mongodb.internal.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:194) ~[mongodb-driver-core-4.2.3.jar:na]
at com.mongodb.internal.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:185) ~[mongodb-driver-core-4.2.3.jar:na]
at com.mongodb.internal.operation.OperationHelper.withReleasableConnection(OperationHelper.java:621) ~[mongodb-driver-core-4.2.3.jar:na]
at com.mongodb.internal.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:185) ~[mongodb-driver-core-4.2.3.jar:na]
at com.mongodb.internal.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:76) ~[mongodb-driver-core-4.2.3.jar:na]
at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:187) ~[mongodb-driver-sync-4.2.3.jar:na]
at com.mongodb.client.internal.MongoCollectionImpl.executeInsertMany(MongoCollectionImpl.java:498) ~[mongodb-driver-sync-4.2.3.jar:na]
at com.mongodb.client.internal.MongoCollectionImpl.insertMany(MongoCollectionImpl.java:480) ~[mongodb-driver-sync-4.2.3.jar:na]
at com.mongodb.client.internal.MongoCollectionImpl.insertMany(MongoCollectionImpl.java:475) ~[mongodb-driver-sync-4.2.3.jar:na]
at org.springframework.data.mongodb.core.MongoTemplate.lambda$insertDocumentList$17(MongoTemplate.java:1490) ~[spring-data-mongodb-3.2.6.jar:3.2.6]
at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:553) ~[spring-data-mongodb-3.2.6.jar:3.2.6]
at org.springframework.data.mongodb.core.MongoTemplate.insertDocumentList(MongoTemplate.java:1483) ~[spring-data-mongodb-3.2.6.jar:3.2.6]
at org.springframework.data.mongodb.core.MongoTemplate.doInsertBatch(MongoTemplate.java:1346) ~[spring-data-mongodb-3.2.6.jar:3.2.6]
at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:1280) ~[spring-data-mongodb-3.2.6.jar:3.2.6]
【解决方法】
添加如下配置:
spring.data.mongodb.username=admin
spring.data.mongodb.password=admin
application.properties文件修改后为:
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.username=admin
spring.data.mongodb.password=admin
spring.data.mongodb.database=user
以上是关于springboot连接mongodb报UncategorizedMongoDbException: Command failed with error 13 (Unauthorized)解决方法的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot问题记录SpringBoot 配置文件未生效
此客户端所连接的 MongoDB 集群不支持会话-springboot-mongodb-transactions