如何在添加root用户后在MongoDB中使用Auth

Mic*_*ael 1 remote-access remote-server mongodb mongodb-query

我需要远程连接到我的MongoDB.所以我注释掉线bind_ipmongod.conf,并设置有也auth=true.现在我能够远程连接.

然后我在运行mongodb的机器上键入以下内容:

  1. use admin
  2. db.addUser("myadmin", "mypw")

是成功创建但现在当我尝试远程连接或甚至在本地计算机上,我尝试执行任何它sais: not authorized on admin to execute command

即使我尝试这样连接:( mongo -u myadmin -p mypw本地)

我收到以下错误:

MongoDB shell version: 2.6.9 connecting to: test 2015-04-08T15:25:53.425+0200 Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 } at src/mongo/shell/db.js:1260 exception: login failed

也许是因为他试图连接test数据库?如何指定哪个数据库.远程它不能mongo --host myurl -u myadmin-p mypw 与以下错误一起使用:

2015-04-08T14:31:00.373+0200 E QUERY Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 } at DB._authOrThrow (src/mongo/shell/db.js:1236:32) at (auth):6:8 at (auth):7:2 at src/mongo/shell/db.js:1236 exception: login failed

小智 7

--authenticationDatabase在连接到mongo shell时尝试使用,如下所示:

mongo -u myadmin -p mypw --authenticationDatabase admin
Run Code Online (Sandbox Code Playgroud)