Reputation: 1067
I must be a complete idiot.
I am simply trying to connect a Grails 2.4RC1 application to a MongoDb 2.6 database when the Mongo instance is running standalone in --auth mode.
The grails app is simple:
Modify DataSource.groovy to:
grails {
mongo {
host = "localhost"
port = 27017
databaseName = "tksoftware"
userid = "dbLogin"
password="login"
}
}
run-app // with mongo running without auth
Everything runs as expected. I can list Persons, add/edit Persons as expected.
In Mongo:
`db.createUser( { user:"admin", pwd:"admin", roles: [{role:"root", db:"admin"}]})`
start client:
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
Shows successfully added user:
`{"user": "dbLogin", "roles": [{...dbOwner and readWrite roles in "db":"tksoftware"}]}`
But when I run the app and try to go to the PersonController index page I get:
`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}`
I see:
`[conn6] authenticate db: tksoftware { authenticate: 1, nonce: "xxx", user: "dbLogin", key: "xxx" }`
in the mongo log
I have spent allot of time trying to get this to work. Anyone that can help would be appreciated.
Upvotes: 0
Views: 807
Reputation: 7985
You need to setup authentication at the driver level as per
http://docs.mongodb.org/ecosystem/tutorial/authenticate-with-java-driver/
To do this you need to connect using a MongoDB connection string. See
http://api.mongodb.org/java/current/com/mongodb/MongoClientURI.html
The format is:
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database[.collection]][?options]]
The configuration for this goes in grails-app/DataSource.groovy
. Example:
grails {
mongo {
connectionString = "mongodb://username:password@localhost"
}
}
Upvotes: 3