为什么在MongoDB中发现UserNotFound错误?

我想能够访问mongodb://admin:password@db:27017/testdb ,但我不断收到

 Command: ./node_modules/east/bin/east migrate --adapter east-mongo --url mongodb://admin:password@db/testdb [conn7] SCRAM-SHA-1 authentication failed for admin on testdb from client 172.17.2.60 ; UserNotFound Could not find user admin@testdb 

db是主机名。 这是我所做的:

 mongod --storageEngine wiredTiger --httpinterface --rest --master --auth & mongo admin --eval "db.createUser({user: 'admin', pwd: 'password', roles:[{role:'root',db:'admin'}]});" 

如果我做

 mongo admin -u admin -p password --host db --port 27017 

然后show dbs作品。

现在我想访问我还没有创build的testdb ,但是从我的理解数据库不存在是在飞行中创build?

 mongo testdb -u admin -p password --host localhost --port 27017 2016-06-08T16:14:02.146+0200 E QUERY Error: 18 Authentication failed. 

任何人都可以看到为什么我不能连接?

您正试图对不存在或空的数据库进行身份validation。 这就是validation失败的原因。

我会

  1. login到MongoDB。
  2. 执行use testdb切换到testdb数据库
  3. 使用适当的选项执行db.createuser(...)以创build可用于进行身份validation的用户/密码

那么你应该可以做你所描述的。