Mongo java驱动程序身份验证无法正常工作

时间:2017-08-03 08:15:58

标签: mongodb mongo-java-driver

我正在尝试使用mongo java driver 3.4从java访问mongodb。我在下面编写代码来访问mongodb,即使我在连接时输入了错误的凭据,我也可以访问我的数据库。

MongoCredential credential = MongoCredential.createScramSha1Credential("rupesh_user", "rupesh_db",
                    "admin123".toCharArray());
MongoClient mongoClient= new MongoClient( new ServerAddress( "localhost", 27017 ), Arrays.asList(credential));

当我尝试从ubuntu中的终端访问mongo数据库时。它要求认证。

> use rupesh_db
switched to db rupesh_db
> show collections
2017-08-03T13:07:52.970+0530 E QUERY    [thread1] Error: listCollections failed: {
    "ok" : 0,
    "errmsg" : "not authorized on rupesh_db to execute command { listCollections: 1.0, filter: {} }",
    "code" : 13,
    "codeName" : "Unauthorized"
} :

1 个答案:

答案 0 :(得分:0)

从控制台,您必须授权

> use rupesh_db
switched to db rupesh_db
> db.auth("rupesh_user","admin123")
> show collections 

More details