最近在做的项目用到了Mongo DB,发现在开启用户认证的情况下,用URI的方式连接总是提示认证失败,只有在分别设置了username ,password , database等字段才能连接成功。
1 | spring: |
之前一直认为Spring Boot连接Mongo DB的方式和连JDBC差不多,今天抽空研究了下,发现URI中的数据库名其实是存储用户认证信息的数据库,实际数据库要通过database设定:
1 | spring: |
官方文档:
/database
Optional. The name of the database to authenticate if the connection string includes authentication credentials in the form of
username:password@. If/databaseis not specified and the connection string includes credentials, the driver will authenticate to theadmindatabase. See alsoauthSource.