我一定是个白痴。
当Mongo实例以--auth模式独立运行时,我只是试图将Grails 2.4RC1应用程序连接到MongoDb 2.6数据库。
grails应用程序很简单:
grails {
mongo {
host = "localhost"
port = 27017
databaseName = "tksoftware"
userid = "dbLogin"
password="login"
}
}
一切都按预期运行。我可以列出人员,按预期添加/编辑人员。
在蒙哥:
`db.createUser( { user:"admin", pwd:"admin", roles: [{role:"root", db:"admin"}]})`
mongo --username "admin" --password "admin" --authenticationDatabase "admin"
db.createUser( { user:"dbLogin", pwd:"login", roles:[{role:"dbOwner", db:"tksoftware"},{role:"readWrite", db:"tksoftware"}]})
use tksoftware
db.auth("dbLogin","login")
show users
显示成功添加的用户:
`{"user": "dbLogin", "roles": [{...dbOwner and readWrite roles in "db":"tksoftware"}]}`
但是,当我运行该应用程序并尝试转到PersonController索引页面时,我得到:
`Class com.mongodb.CommandFailureExceptionMessage
{ "serverUsed" : "localhost:27017" , "ok" : 0.0 , "errmsg" : "not authorized on
tksoftware to execute command { aggregate: \"person\", pipeline: [ { $project: { _id: 1
} }, { $group: { _id: 0, count: { $sum: 1 } } } ] }" , "code" : 13}`
我懂了:
`[conn6] authenticate db: tksoftware { authenticate: 1, nonce: "xxx", user: "dbLogin", key: "xxx" }`
在mongo日志中
我花了很多时间试图使它起作用。任何可以帮助的人将不胜感激。
最佳答案
您需要按照以下说明在驱动程序级别设置身份验证
http://docs.mongodb.org/ecosystem/tutorial/authenticate-with-java-driver/
为此,您需要使用MongoDB连接字符串进行连接。看到
http://api.mongodb.org/java/current/com/mongodb/MongoClientURI.html
格式为:
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database[.collection]][?options]]
其配置在
grails-app/DataSource.groovy
中。例:grails {
mongo {
connectionString = "mongodb://username:password@localhost"
}
}
关于mongodb - MongoDb 2.6/Grails 2.4RC1/GORM身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23571656/