报错信息:
2020-08-10 10:33:17.553 [http-nio-31001-exec-1] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName=‘root’, source=‘admin’, password=, mechanismProperties={}}}, 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” }}}]] with root cause
com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName=‘root’, source=‘admin’, password=, mechanismProperties={}}}, 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” }}}]
原因:连接配置不对
解决:
情况1:配置密码认证
配置文件
spring:
data:
mongodb:
uri: mongodb://root:123@localhost:27017
database: user
配置文件解析
root 用户名
123 密码
localhost ip地址
27017 端口
user 数据库名称
情况2 未配置密码认证
spring:
data:
mongodb:
uri: mongodb://localhost:27017
database: user