MongoDB4.x 远程连接及用户名密码认证登陆配置

1.创建admin账号
db.createUser(
{
user: "admin",
pwd: "你的密码",
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
}
)

mongoDB 账号权限 文档


创建子帐号
use admin
db.auth('admin','adminpwd')
 db.createUser(
... {
... user : "my_tester",
... pwd : "123456",
... roles: [ { role : "readWrite", db : "test" } ,
... { role : "read", db : "game_report" } ]
... }
... )
 
2.修改配置文件 解除注释
security:
authorization: enabled #注意缩进,参照其他的值来改,若是缩进不对可能导致后面服务不能重启

 3.重启mongo
sudo service mongod restart

auth 对应admin的时候用与链接mongodb 不同数据库需要设置不同的用户来管理最佳 


已邀请:

要回复问题请先登录注册