没有"连接到mongodb实例 - authenticationDatabase admin"

时间:2015-02-03 22:07:31

标签: mongodb

所以我准备好了mongodb实例,打开远程连接。

由于某些原因,我无法将其与用户连接/传递而没有--authenticationDatabase admin 添加到连接字符串。

当我尝试与用户/传递单独执行连接时(没有" authenticationDatabase",我得到:

2015-02-01T16:07:24.818-0500 Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 } at src/mongo/shell/db.js:1210
exception: login failed

2 个答案:

答案 0 :(得分:8)

这是因为用户凭据存储在管理数据库中,存储在mongo shell连接到的(默认)数据库中(测试)。

您可以使用连接网址进行更改:

使用admin数据库:

$ mongo localhost/admin --username user -p
MongoDB shell version: 2.6.9
Enter password:
connecting to: localhost/admin
replset:PRIMARY>

没有特定数据库(默认为test):

$ mongo localhost --username user -p
MongoDB shell version: 2.6.9
Enter password:
connecting to: localhost
2015-04-22T15:34:28.743+0100 Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 } at src/mongo/shell/db.js:1260
exception: login failed

使用--authenticationDatabase

$ mongo localhost --username user --authenticationDatabase admin -p
MongoDB shell version: 2.6.9
Enter password:
connecting to: localhost
replset:PRIMARY>

请注意,使用--host不会假设数据库名称是/之后的名称(斜杠:

$ mongo --host localhost/admin --username user -p
MongoDB shell version: 2.6.9
Enter password:
connecting to: localhost/admin:27017/test
2015-04-22T15:37:40.703+0100 starting new replica set monitor for replica set localhost with seeds admin:27017
2015-04-22T15:37:40.703+0100 [ReplicaSetMonitorWatcher] starting
2015-04-22T15:37:40.920+0100 getaddrinfo("admin") failed: nodename nor servname provided, or not known
2015-04-22T15:37:40.922+0100 warning: No primary detected for set localhost
2015-04-22T15:37:40.922+0100 All nodes for set localhost are down. This has happened for 1 checks in a row. Polling will stop after 29 more failed checks
2015-04-22T15:37:40.923+0100 Error: connect failed to replica set localhost/admin:27017 at src/mongo/shell/mongo.js:148
exception: connect failed

答案 1 :(得分:5)

感谢@Gianfranco P。

简要说明:如果要连接到没有authenticationDatabase选项的数据库,则应在要连接的同一数据库中创建用户。

例如

mongodb://demouser:demopass@<host>:<port>/demodb

对于此URI,您应该在demouser数据库中创建demodb用户,否则您必须提供authenticationDatabase这样的选项

mongodb://demouser:demopass@<host>:<port>/demodb?authSource=<authDatabase>