Search code examples
mongodbgrails

Mongo DB authentication not working in grails but from console


I want to use authentication in my mongodb. So I created a user. Which this user I can connect on command line and insert data without any problem.

But when I want to use this user in grails, I get this error:

{ "serverUsed" : "127.0.0.1:27017" , "ok" : 0.0 , "errmsg" : "auth failed" , "code" : 18 , "codeName" : "AuthenticationFailed"}

When I connect from commandline, everything works:

mongo --port 27017 -u "mongouser" -p "pwd" mydb

My code in Grails:

 MongoCredential credential = MongoCredential.createMongoCRCredential("mongouser", "mydb", "pwd".toCharArray())
 def mongoClient = new MongoClient( new ServerAddress(host, port), [credential ] )
 gMongoCon = new GMongo(mongoClient)

What is wrong here?


Solution

  • This solution works:

    MongoCredential.createCredential(username, datatable, password)