且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MongoDB 3.2 身份验证失败

更新时间:2021-08-09 01:12:44

好吧,您需要按顺序执行几个步骤才能成功创建用户.

Well, you'll need to take couple of steps in sequence to create user successfully.

首先,您需要创建一个管理员用户.我更喜欢创建超级用户.

First of all, you need to create an administrator user. I prefer creating super user.

> use admin
> db.createUser({user: "root", pwd: "123456", roles:["root"]})

重新启动您的 MongoDB 服务器并使用 --auth 标志启用身份验证.

Restart your MongoDB server and enable authentication with --auth flag.

> mongod --auth --port 27017 --dbpath /var/lib/mongodb

一旦您的服务器启动,请以管理员身份连接到它

Once your server is up, connect to it as administrator

>mongo -u "root" -p "123456" --authenticationDatabase "admin"

连接后,创建普通用户.假设您的用户数据库名称是 cd2.

Once you are connected, create normal user. Assuming your user database name is cd2.

> use cd2
> db.createUser({user: "cd2", pwd: "cd2", roles:["dbOwner"]})

如果您看到成功消息,请断开与 mongo shell 的连接并与新用户重新连接凭据.

If you see success messsage, disconnect from mongo shell and reconnect with new user credentials.

> mongo <host:port>/cd2 -u "cd2" -p "cd2"