mongodb基础之用户权限管理实例教程
前言
本文主要介绍了mongodb用户权限管理的相关内容,关于接着上次实践的部分,下面话不多说了,来一起看看详细的介绍吧
启动mongodb并连接
./bin/mongod -f conf/mongod.conf ./bin/mongo 127.0.0.1:12345
查看默认的数据库情况
> show dbs admin 0.000GB local 0.000GB > use admin switched to db admin > show tables system.version
可以看到,目前数据库里除了一些基本信息,什么都没有
在创建设置用户权限之前,先了解一下文档知识
创建用户
# demo db.createUser( { user: "reportsUser", pwd: "12345678", roles: [ { role: "read", db: "reporting" }, { role: "read", db: "products" }, { role: "read", db: "sales" }, { role: "readWrite", db: "accounts" } ] } )
数据库内建角色
数据库用户角色
- read (读取指定数据库)
- readWrite (读写指定数据库)
数据库管理角色
- dbAdmin (数据库管理员)
- dbOwner (数据库所有者,合并了 readWrite, dbAdmin and userAdmin roles.)
- userAdmin (用户管理员,可以找指定数据库里创建、删除和管理用户)
集群管理角色
- clusterAdmin (集群管理员)
- clusterManager (集群管理者)
- clusterMonitor (集合监视者)
- hostManager (主机管理者)
备份恢复角色
- backup (备份)
- restore (还原)
所有数据库角色
- readAnyDatabase (读任何数据库)
- readWriteAnyDatabase (读写任何数据库)
- userAdminAnyDatabase (用户管理任何数据库)
- dbAdminAnyDatabase (任意数据库管理员)
超级用户角色
- root
内部角色
- __system
有了创建语法,和参数说明,接下来开始实践.
注意,还有一点,账号是跟着数据库绑定的,在那个库里授权,就在那个库里验证(auth)
否则会失败
创建 账号管理授权权限 的账号
> db.createUser( ... { ... user: 'admin', ... pwd: '123456', ... roles: [{role: 'userAdminAnyDatabase', db: 'admin'}] ... } ... ) Successfully added user: { "user" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }
然后退出数据库
> use admin switched to db admin > db.shutdownServer()
重新启动mongodb,记得在配置文件mongod.conf里加上 auth = true
./bin/mongod -f conf/mongod.conf ./bin/mongo 127.0.0.1:12345 > show dbs # 没有验证,没有权限,会出错 "errmsg" : "not authorized on admin to execute command > use admin > db.auth('admin', '123456') 1 # 返回 1 表示授权成功,0表示失败 > show dbs #已经授权,可以查看了
创建 读、读写权限的账户
> use book switched to db book > db.createUser( ... { ... user: 'zhangsan', ... pwd: 'zhangsan', ... roles: [{role: 'read', db: 'book'}] ... } ... ) Successfully added user: { "user" : "zhangsan", "roles" : [ { "role" : "read", "db" : "book" } ] } > db.createUser( ... { ... user: 'lisi', ... pwd: 'lisi', ... roles: [{role: 'readWrite', db: 'book'}] ... } ... ) Successfully added user: { "user" : "lisi", "roles" : [ { "role" : "readWrite", "db" : "book" } ] } > show users { "_id" : "book.lisi", "user" : "lisi", "db" : "book", "roles" : [ { "role" : "readWrite", "db" : "book" } ] } { "_id" : "book.zhangsan", "user" : "zhangsan", "db" : "book", "roles" : [ { "role" : "read", "db" : "book" } ] }
然后验证用户权限是否正确
> db.book.insert({book: '小人书'}) # 没验证,会出错 WriteResult({ "writeError" : { "code" : 13, "errmsg" : "not authorized on book to execute command { insert: \"book\", docum ents: [ { _id: ObjectId('5959b56edcc047dfe5c9b336'), book: \"小人书\" } ], ordered: true }" } }) > db.auth('lisi', 'lisi') 1 > db.book.insert({book: '小人书'}) WriteResult({ "nInserted" : 1 }) > db.auth('zhangsan', 'zhangsan') # 用户切到 zhangsan 1 > db.book.find() # 可以查看 { "_id" : ObjectId("5959b59fdcc047dfe5c9b337"), "book" : "小人书" } > db.book.insert({book: '择天记'}) # 没有write权限,会失败 WriteResult({ "writeError" : { "code" : 13, "errmsg" : "not authorized on book to execute command { insert: \"book\", docum ents: [ { _id: ObjectId('5959b650dcc047dfe5c9b338'), book: \"择天记\" } ], ordered: true }" } })
创建 root 超级权限账号
这个超级权限包括 授权 和 操控数据库集合数据,比较简单,只需要把role设置成 root
> use admin switched to db admin > db.auth('admin', '123456') 1 > db.createUser( ... { ... user: 'dongsheng', ... pwd: '123456', ... roles: [{role: 'root', db: 'admin'}] ... } ... ) Successfully added user: { "user" : "dongsheng", "roles" : [ { "role" : "root", "db" : "admin" } ] } > db.auth('dongsheng', '123456') 1 > use book switched to db book > db.book.insert({book: '笑傲江湖'}) WriteResult({ "nInserted" : 1 }) > db.book.find() { "_id" : ObjectId("5959b59fdcc047dfe5c9b337"), "book" : "小人书" } { "_id" : ObjectId("5959b7abdcc047dfe5c9b339"), "book" : "笑傲江湖" }
总结
相关推荐
我心似明月 2020-11-09
lbyd0 2020-11-17
BigYellow 2020-11-16
sushuanglei 2020-11-12
zhushenghan 2020-11-09
sunnnyduan 2020-10-16
不要皱眉 2020-10-14
xiaohai 2020-09-29
songxiugongwang 2020-09-22
萌亖 2020-09-17
LuckyLXG 2020-09-08
sdmzhu 2020-09-01
mkhhxxttxs 2020-09-16
xiaohai 2020-09-16
newcome 2020-09-09
jaylong 2020-08-19
大秦铁骑 2020-08-19
thatway 2020-08-19