1.连接mongo:

mongo

2.注册超级用户:

use admin
db.createUser({user:"testAdmin",pwd:"123456",roles:[{role:"userAdminAnyDatabase",db:"admin"}]})

3.修改mongod.cfg

security:
    authorization: "enabled"

4.重启mongodb服务

5.测试鉴权,提示错误表示鉴权已开启

mongo
use admin
db.system.users.find()

6.登陆

db.auth("testAdmin","123456")

7.进入数据库,创建数据库用户

use test2
db.createUser({user:'test2admin',pwd:'123456',roles:[{role:'readWrite',db:'test2'}]})