当前位置:Gxlcms > 数据库问题 > python | ubuntu 中 mongodb 数据读写权限配置

python | ubuntu 中 mongodb 数据读写权限配置

时间:2021-07-01 10:21:17 帮助过:4人阅读

二、设置单数据库用户权限

接着上一个步骤,我们分别为不同的数据库创建不同的用户角色。注意:每次创建新的用户之前,需要use admin 和 db.auth(‘fayin‘, ‘fayin@39.108‘).


// 创建用户 fayin_blog 对数据库 vueBlog 拥有读写权限。
use vueBlog

db.createUser({user: ‘fayin_blog‘, pwd: ‘fayin_blog@39.108‘, roles: [{
    role: ‘readWrite‘,
    db: ‘vueBlog‘
}])

// 创建用户 fayin_blog_read 对数据库 vueBlog 拥有读权限。
use vueBlog

db.createUser({user: ‘fayin_blog_read‘, pwd: ‘fayin_blog_read@39.108‘, roles: [{role: ‘read‘,db: ‘vueBlog‘}])

三、修改配置文件,开启数据库登录验证

sudo vi /etc/mongod.conf

找到 security 项,键入:

security
  authorization: ‘enabled‘

保存并退出,重启mongodb:sudo service mongod restart

四、重新登录数据库

当重启生效后,我们再次使用数据库,会报错:not authorized on admin to execute command ,所以,我们需要先登录 admin 数据库:即使用哪个数据库之前,需要先对用户进行授权。

use admin

db.auth(‘fayin‘, ‘fayin@39.108‘)

python | ubuntu 中 mongodb 数据读写权限配置

标签:class   role   bsp   blog   nginx   port   exe   man   base   

人气教程排行