使用Java驱动程序3.0.3和GridFS在MongoDB 3.0.5上进行身份验证 [英] Authentication on MongoDB 3.0.5 with Java Driver 3.0.3 and GridFS

查看:85
本文介绍了使用Java驱动程序3.0.3和GridFS在MongoDB 3.0.5上进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用下面的连接字符串从Java Driver 3.0.3连接到Mongo 3.0.5:

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}}]

我已阅读到MongoDB 3.0最近更改了身份验证API.我当前正在使用以前的接口client.getDB(),该接口已弃用

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)

代替新的一个client.getDatabase():

instead the new one client.getDatabase():

        MongoDatabase db = client.getDatabase("databasename");

这可能是导致假定不再支持getDB()的异常的原因.

This could be the cause of the exception supposing that getDB() is not longer supported.

但是,问题是,我也在项目中使用了GridFS,目前,GridFS使用的是DB而不是MongoDatabase,所以我希望数据库接口仍能够在当前版本上进行身份验证,因为我无法配置mongodb进行身份验证在普通连接上,但在GridFS连接上不进行身份验证.

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/当前/com/mongodb/gridfs/GridFS.html

所以,我有2个选择:

  • 有人能够使用旧版API(client.getDB())向MongoDB 3.0.5进行身份验证吗?
  • 是否可以通过MongoDatabase接口使用GridFS?

谢谢

推荐答案

我设法通过将身份验证机制更改为上一个(MONGODB-CR)而不是版本3 *中的默认新身份验证机制来解决此问题. -SHA-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).

这里是一个描述:

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

  1. start MongoDB without authentication (commenting out auth=yes in /etc/mongod.conf)

更改mongodb中的身份验证机制

change the authentication mechanism in mongodb

使用管理员

use admin

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

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

schema.currentVersion = 3

schema.currentVersion = 3

db.system.version.save(schema)

db.system.version.save(schema)

  • 在mydatabase数据库中创建一个用户(必须在更改auth机制之后创建用户)

  • create a user in mydatabase database (users must be created after changing the auth mechanism)

    使用mydatabase

    use mydatabase

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

    db.createUser( { user: "root", pwd: "pass", roles: [ "readWrite" ] } )

  • 使用身份验证重新启动MongoDB

  • restart MongoDB with authentication

    从驱动程序中调用,在查询字符串中指定MONGODB-CR

    invoke from the driver specifying MONGODB-CR in the query string

    mongodb://root:pass @ myIP:myPort/mydatabase?authMechanism = MONGODB-CR

    mongodb://root:pass@myIP:myPort/mydatabase?authMechanism=MONGODB-CR

  • 关键是:

    1. 在mongoDB(2)和
    2. 中更改身份验证机制
    3. 在查询字符串(5)中指定此机制
    1. changing the authentication mechanism in mongoDB (2) and
    2. specify this mechanism in the query string (5)

    这篇关于使用Java驱动程序3.0.3和GridFS在MongoDB 3.0.5上进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆