问题
I am trying to connect from Java Driver 3.0.3 with the connection string below to a Mongo 3.0.5:
mongodb://admin:pass@myIP:myPort/databasename?authSource=databasename
but I am getting the following exception:
com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches PrimaryServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=myIP:myPort, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoException: java.lang.NoClassDefFoundError: com.mongodb.connection.ScramSha1Authenticator$ScramSha1SaslClient}, caused by {java.lang.NoClassDefFoundError: com.mongodb.connection.ScramSha1Authenticator$ScramSha1SaslClient}}]
I have read that MongoDB 3.0 has recently changed authentication API. I am currently using the previous interface client.getDB(), which is deprecated,
DB db = client.getDB(uri.getDatabase()); (deprecated)
instead the new one client.getDatabase():
MongoDatabase db = client.getDatabase("databasename");
This could be the cause of the exception supposing that getDB() is not longer supported.
BUT, the problem is, I am also using GridFS in my project and, currently, GridFS uses DB instead MongoDatabase, so I would expect the DB interface still being able to authenticate on current release because I cannot configure my mongodb to authenticate on normal connections but not authenticate on GridFS connections.
http://api.mongodb.org/java/current/com/mongodb/gridfs/GridFS.html
So, I have 2 options:
- Is anyone being able to authenticate to MongoDB 3.0.5 with the old API (client.getDB())?
- Is there a way to use GridFS from a MongoDatabase interface?
Thanks
回答1:
I managed to work around the issue by changing the authentication mechanism to the previous one (MONGODB-CR) instead the default new one in releases 3.* (SCRAM-SHA-1).
Here is a description:
start MongoDB without authentication (commenting out auth=yes in /etc/mongod.conf)
change the authentication mechanism in mongodb
use admin
var schema = db.system.version.findOne({"_id" : "authSchema"})
schema.currentVersion = 3
db.system.version.save(schema)
create a user in mydatabase database (users must be created after changing the auth mechanism)
use mydatabase
db.createUser( { user: "root", pwd: "pass", roles: [ "readWrite" ] } )
restart MongoDB with authentication
invoke from the driver specifying MONGODB-CR in the query string
mongodb://root:pass@myIP:myPort/mydatabase?authMechanism=MONGODB-CR
The key things are:
- changing the authentication mechanism in mongoDB (2) and
- specify this mechanism in the query string (5)
来源:https://stackoverflow.com/questions/32019778/authentication-on-mongodb-3-0-5-with-java-driver-3-0-3-and-gridfs