Mongo查询失败,错误代码为13,错误消息为“未授权" [英] Mongo Query failed with error code 13 and error message 'not authorized'
问题描述
当我尝试对mLab上的mongo实例进行查询时发生错误.我从Spring Boot应用程序请求它.与数据库的连接是稳定的.在控制台的日志下方.
Error occures when I am trying to make an query to mongo instance on mLab. I am requesting it from spring boot app. Connection to database is stable. Below log from console.
具有描述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}
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}
这是确切的错误消息
查询在服务器ds131531.mlab.com:31531上失败,并显示错误代码13和错误消息未经披萨店授权执行命令{查找:产品",过滤器:{},batchSize:2147483647};嵌套的异常是com.mongodb.MongoQueryException:查询失败,错误代码13和错误消息在披萨店未授权执行命令{查找:产品",过滤器:{},batchSize:2147483647}在服务器ds131531.mlab上.com:31531
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是内置角色.
If i make the same query from mongo shell everything is ok. I have added readWrite role and my custom readProduct role role to user who is making request but nothing is helping. dbOwner and read are builtin roles.
用户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角色
readProduct role
{
"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 application.properties
spring:
profiles: heroku
data:
mongodb:
uri: mongodb://login:pass@ds131531.mlab.com:31531/pizza-db
推荐答案
问题已解决.问题是我的application.yml文件上有更多配置文件,并且我未在配置文件heroku中声明数据库名称,因此它正在从默认配置文件读取数据库名称. 在application.yml文件下方
Problem solved. The issue was i have more profiles on my application.yml file and i don't declare database name in profile heroku so it was reading database name from default profile. Below application.yml file
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}
这篇关于Mongo查询失败,错误代码为13,错误消息为“未授权"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!