MongoDB的2.6.1命令行身份验证失败

问题描述:

我建立副本身份验证设置。我用教程。我设置了密钥文件,管理员用户和其他用户。这一切工作正常 - 禁用匿名访问,我可以登录

I set up replica set with authentication. I used this tutorial. I set up keyfile, admin user and other users. It all works fine - anonymous access is disabled and I can login

$ mongo
MongoDB shell version: 2.6.1
connecting to: test
Error while trying to show server startup warnings: not authorized on admin to execute     command { getLog: "startupWarnings" }
rs0:PRIMARY> use admin
switched to db admin
rs0:PRIMARY> db.auth("USERNAME","PASSWORD")
1
rs0:PRIMARY> 

使用我创建的用户凭据。

using credentials of users I created.

但我不能命令行登录

$ mongo -u 'USERNAME' -p 'PASSWORD'
MongoDB shell version: 2.6.1
connecting to: test
2014-05-18T14:23:47.324+0200 Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 } at   src/mongo/shell/db.js:1210
exception: login failed

使用相同的凭据。我还没有找到,所以,只要在文档中有益还是在这里。

using the same credentials. I haven't find anything helpful in the documentation or here on SO.

在你的例子进行带壳认证是针对在管理数据库。命令行上面贴不指定数据库,因此进行身份验证这是默认的数据库的测试即可。试试这个通过对管理数据库命令行来验证:

The in-shell authentication performed in your example is against the admin database. The command line posted above does not specify a database and is therefore authenticating against the default database which is test. Try this instead to authenticate via command line against the admin db:

mongo admin -u 'USERNAME' -p 'PASSWORD'

如果服务器不在本地主机上,那么你可以使用这样的:

if the server is not on the local host then you can use this:

mongo your_host_name:your_port/admin -u 'USERNAME' -p 'PASSWORD'