0

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:

  • create-domain Person
  • Add String fname and String lname as properties
  • Add import org.bson.types.ObjectId
  • Add ObjectId id
  • generate-all Person
  • 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"}]})`
  • logout
  • restart mongod --auth
  • 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.

1 Answer 1

3

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"
   }
}
4
  • Thanks Graeme. Might just want to add a note to the MongoDB GORM plugin indicating that you MUST use the connection string option if Mongo is running in --auth mode. May 11, 2014 at 19:58
  • I was thinking that if the configuration already has everything you need to create the connection string, which it did, why wouldn't the plugin format the connectionString property based upon the other elements in the config, including the defaults. So, as the above example shows, all of the necessary elements of the connection string are there. The plugin could formulate the connection string instead of me having to do it. Obviously, the option to use the connectionString config would need to be there to support multiple hosts. Don't get me wrong. I'm happy to get it working. May 11, 2014 at 20:22
  • The plugin supports various ways to configure the MongoDB connection. Using a connectionString is a relatively new feature of MongoDB, and hence in order to support both older and newer versions of MongoDB we can't always formulate a connection string. May 12, 2014 at 11:35
  • Ok, I understand what you are saying Graeme. But I think you need to at least add a warning to the docs that you MUST use the connection string for version 2.6, if authentication is enabled. May 13, 2014 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.