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:
Thanks
答案 0 :(得分:1)
我设法通过将身份验证机制更改为上一个(MONGODB-CR)而不是版本3中的默认新机制来解决此问题。*(SCRAM-SHA-1)。
以下是说明:
启动MongoDB而不进行身份验证(在/etc/mongod.conf中注释掉auth = yes)
更改mongodb中的身份验证机制
使用管理员
var schema = db.system.version.findOne({" _id":" authSchema"})
schema.currentVersion = 3
db.system.version.save(模式)
在mydatabase数据库中创建用户(必须在更改身份验证机制后创建用户)
使用mydatabase
db.createUser( { 用户:" root", pwd:"传递", 角色:[" readWrite" ] } )
使用身份验证重启MongoDB
从查询字符串
中指定MONGODB-CR的驱动程序调用的mongodb://根:通@ MYIP:MyPort上/ MyDatabase的的AuthMechanism = MongoDB的-CR
关键是: