如何以管理员身份登录 mongodb?

时间:2021-06-30 15:32:08

标签: database mongodb authentication console admin

我创建了管理员:

> use admin
switched to db admin
> db.createUser(
...   {
...     user: 'admin',
...     pwd: 'password',
...     roles: [ { role: 'root', db: 'admin' } ]
...   }
... );
Successfully added user: {
        "user" : "admin",
        "roles" : [
                {
                        "role" : "root",
                        "db" : "admin"
                }
        ]
}
> exit;

但是当我想登录时出现错误:

> use admin
switched to db admin
> db.auth('admin','password');
Error: Authentication failed.
0

我也尝试过:

mongo --port 27017  --authenticationDatabase "admin" -u "admin" -p "password"

和:

mongo localhost:27017/admin -u admin -p password

但它也不起作用。

1 个答案:

答案 0 :(得分:0)

重新启动我的电脑后,我能够以管理员身份登录到 mongodb。