本篇适用情形:
- MongoDB 启用权限控制
- MongoDB 用户管理
- 了解MongoDB 内置角色
正文:
【1】内建角色介绍
Built-In Roles
Database User Roles (exist in each database.)
Database Administration Roles (exist in each database.)
All-Database Roles (in the admin database apply to all but the local and config databases)
Superuser Roles
root (readWriteAnyDatabase + dbAdminAnyDatabase + userAdminAnyDatabase + clusterAdmin)
【2】用户基本操作
//查看现有用户
> use admin
> db.system.users.find()
//删除用户dbuser
> use admin
> db.system.users.remove({user:"dbuser"});
//创建管理用户dbadmin
> use admin
> db.createUser({ user:"dbadmin", pwd:"passowrd", roles: [{ role: "root", db: "admin" }] });
//为db_files库创建读写权限用户dbuser
> use db_files
> db.createUser({ user:"dbuser", pwd:"passowrd", roles: [{ role: "dbOwner", db: "db_files" }] });
//为db_files库创建只写权限用户rduser
> use db_files
> db.createUser({ user:"rduser", pwd:"passowrd", roles: [{ role: "read", db: "db_files" }] });
【3】启用MongoDB用户访问控制
- 导出keyfile(用于集群之间验证身份),并拷贝到各个机器中
- 编辑配置文件
- 重启服务
导出认证key文件
openssl rand -base64 741 > mongo-key
chmod 600 mongo-key #这里必须是600
$ sudo vi /etc/mongod.conf
security:
authorization: enabled
keyFile: /home/soft/mongodb-3.2.1/mongo-key
clusterAuthMode: "keyFile"
$ sudo service mongod restart
【4】以授权方式访问MongoDB
- 命令行
- URL
- GUI客户端
mongo -u dbuser -p passwd dbtest
mongodb://user_rw:[email protected]:27017/db_test?replicaSet=???&authSource=db_test
Comments
Post a Comment