Mongo Query失败,错误代码为13,错误消息为“未经授权”

时间:2018-07-18 14:23:36

标签: mongodb authorization mlab

当我尝试对mLab上的mongo实例进行查询时发生错误。我从Spring Boot应用程序请求它。与数据库的连接是稳定的。在控制台日志下方。

  

具有描述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 = [],仲裁器= [],primary ='ds131531-a.mlab.com:31531',tagSet = TagSet {[]},eletectionId = 7fffffff0000000000000001,setVersion = 1,lastWriteDate = Wed Jul 18 15 :22:37 CEST 2018,lastUpdateTimeNanos = 20828794373992}

这是确切的错误消息

  

查询在服务器ds131531.mlab.com:31531上失败,错误代码13和错误消息“未在Pizza-store上授权执行命令{查找:“产品”,过滤器:{},batchSize:2147483647}“;嵌套异常是com.mongodb.MongoQueryException:查询失败,错误代码13和错误消息“在披萨店未授权执行命令{查找:“产品”,过滤器:{},batchSize:2147483647}“在服务器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"
    }
]

readProduct角色

{
"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"
        ]
    }
]

}

编辑: 春季application.properties

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

1 个答案:

答案 0 :(得分:1)

问题解决了。问题是我的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}