Spring Boot data-mongodb连接MongoDB数据库失败
用了spring-boot-starter-data-mongodb模块后,连接本机的Mongodb一直失败,用户也创建了,密码也设了,就是连不上啊。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
总是报错:
com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='test', source='test', 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.DefaultAuthenticator.authenticate(DefaultAuthenticator.java:32)
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(Thread.java:748)
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" }
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.sendSaslContinue(SaslAuthenticator.java:123)
at com.mongodb.connection.SaslAuthenticator.access$100(SaslAuthenticator.java:39)
at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:65)
... 9 common frames omitted
References:
由于mongodb在本机登录是不需要验证的,所以起初我认为是没有用户导致的,就去创建了用户。
用以下语句创建root用户,用户名和密码为root/123456
。按需修改。
use admin
db.createUser({
user: 'root', // 用户名
pwd: '123456', // 密码
roles:[{
role: 'root', // 角色
db: 'admin' // 数据库
}]
})
然后操作了下,发现还是报错,就创建了针对单数据库的普通用户。
use test // 跳转到需要添加用户的数据库
db.createUser({
user: 'test', // 用户名
pwd: '123456', // 密码
roles: [{
role: 'readWrite', // 角色拥有读写权限
db: 'test' // 数据库
}]
})
发现还是不行……
然后查了文章,发现不知道为什么配置文件的配置方式改下就行了,辣鸡mongo
连接失败的配置:
spring:
data:
mongodb:
host: localhost
port: 27017
database: test
username: test
password: 123456
连接成功的配置:
spring:
application:
name: xc-service-manage-cms
data:
mongodb:
# uri: mongodb://用户名:密码@IP:端口/数据库名
uri: mongodb://test:123456@localhost:27017/test