Authentication on MongoDB 3.0.5 with Java Driver 3.0.3 and GridFS

时间:2015-08-14 22:59:34

标签: mongodb mongodb-query mongodb-java

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 个答案:

答案 0 :(得分:1)

我设法通过将身份验证机制更改为上一个(MONGODB-CR)而不是版本3中的默认新机制来解决此问题。*(SCRAM-SHA-1)。

以下是说明:

  1. 启动MongoDB而不进行身份验证(在/etc/mongod.conf中注释掉auth = yes)

  2. 更改mongodb中的身份验证机制

      

    使用管理员

         

    var schema = db.system.version.findOne({" _id":" authSchema"})

         

    schema.currentVersion = 3

         

    db.system.version.save(模式)

  3. 在mydatabase数据库中创建用户(必须在更改身份验证机制后创建用户)

      

    使用mydatabase

         

    db.createUser(       {         用户:" root",         pwd:"传递",         角色:[" readWrite" ]       }   )

  4. 使用身份验证重启MongoDB

  5. 从查询字符串

    中指定MONGODB-CR的驱动程序调用
      

    的mongodb://根:通@ MYIP:MyPort上/ MyDatabase的的AuthMechanism = MongoDB的-CR

  6. 关键是:

    1. 更改mongoDB(2)和
    2. 中的身份验证机制
    3. 在查询字符串(5)
    4. 中指定此机制