Mongo 查询失败,错误代码 13 和错误消息 'not authorized'

Mongo Query failed with error code 13 and error message 'not authorized'

当我尝试在 mLab 上查询 mongo 实例时发生错误。我从 spring 启动应用程序请求它。与数据库的连接是稳定的。下面是来自控制台的日志。

Monitor thread successfully connected to server with description ServerDescription{address=ds131531.mlab.com:31531, type=REPLICA_SET_PRIMARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 4, 15]}, minWireVersion=0, maxWireVersion=5, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=null, roundTripTimeNanos=138364906, setName='rs-ds131531', canonicalAddress=ds131531-a.mlab.com:31531, hosts=[ds131531-a.mlab.com:31531], passives=[], arbiters=[], primary='ds131531-a.mlab.com:31531', tagSet=TagSet{[]}, electionId=7fffffff0000000000000001, setVersion=1, lastWriteDate=Wed Jul 18 15:22:37 CEST 2018, lastUpdateTimeNanos=20828794373992}

这是确切的错误消息

Query failed with error code 13 and error message 'not authorized on pizza-store to execute command { find: "product", filter: {}, batchSize: 2147483647 }' on server ds131531.mlab.com:31531; nested exception is com.mongodb.MongoQueryException: Query failed with error code 13 and error message 'not authorized on pizza-store to execute command { find: "product", filter: {}, batchSize: 2147483647 }' on server ds131531.mlab.com:31531

如果我从 mongo shell 进行相同的查询,一切正常。 我已经向发出请求但没有任何帮助的用户添加了 readWrite 角色和我的自定义 readProduct 角色角色。 dbOwner 和 read 是内置角色。

用户mongo 角色:

"_id" : "pizza-db.kubiakb",
"user" : "kubiakb",
"db" : "pizza-db",
"roles" : [
    {
        "role" : "readProduct",
        "db" : "pizza-db"
    },
    {
        "role" : "read",
        "db" : "pizza-db"
    },
    {
        "role" : "dbOwner",
        "db" : "pizza-db"
    }
]

阅读产品角色

{
"role" : "readProduct",
"db" : "pizza-db",
"isBuiltin" : false,
"roles" : [ ],
"inheritedRoles" : [ ],
"privileges" : [
    {
        "resource" : {
            "db" : "pizza-db",
            "collection" : "product"
        },
        "actions" : [
            "find"
        ]
    }
],
"inheritedPrivileges" : [
    {
        "resource" : {
            "db" : "pizza-db",
            "collection" : "product"
        },
        "actions" : [
            "find"
        ]
    }
]

}

编辑: Springapplication.properties

spring:
  profiles: heroku
  data:
    mongodb:
      uri: mongodb://login:pass@ds131531.mlab.com:31531/pizza-db

问题已解决。问题是我的 application.yml 文件中有更多配置文件,并且我没有在配置文件 heroku 中声明数据库名称,因此它正在从默认配置文件中读取数据库名称。 application.yml 文件下方

spring:
    data:
      mongodb:
        database: pizza-store
        uri: mongodb://localhost:27017

price-point-multiplier : 0.1

---
spring:
  profiles: docker
  data:
    mongodb:
      database: pizza-store
      uri: mongodb://mongodb:27017

---
spring:
  profiles: heroku
  data:
    mongodb:
      uri: ${MONGODB_URI}

我在另一个数据库上创建了具有角色的用户。我花了几个小时才得到它。 我知道你修复了,但也许可以帮助其他开发者。

db.createUser({user: "right_user", pwd: "right_pass",
roles: [{ role: "readWrite", db: "WRONG_DB" }], passwordDigestor: "server", mechanisms: ["SCRAM-SHA-1","SCRAM-SHA-256"]})