十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章将为大家详细讲解有关MongoDB中的权限管理,文章内容质量较高,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
创新互联专注于企业成都全网营销、网站重做改版、梁溪网站定制设计、自适应品牌网站建设、H5响应式网站、商城系统网站开发、集团公司官网建设、外贸营销网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为梁溪等各大城市提供网站开发制作服务。mongodb配置文件如下:
[root@localhost ~]# cat /usr/local/mongodb/mongod.cnf
bind_ip = 0.0.0.0
logpath=/data/mongodb/logs/mongodb.log
logappend = true
fork = true
#auth = true
port = 6068
dbpath=/data/mongodb/data
pidfilepath=/data/mongodb/mongod.pid
root超级管理员权限:
关闭mongod.cnf配置文件参数:auth
直接登录mongo,设置登录密码:
mongo --host 127.0.0.1 --port 6068
use admin
db.createUser({user: 'root', pwd:'TdLLQd689', roles:[{role: 'root', db: 'admin'}]});
db.auth("root","TdLLQSdH689")
关闭mongod
开启mongod.cnf配置文件参数:auth =true
mongo --host 127.0.0.1 --port 6068
use admin
db.auth("root","TdLLQH689")
创建测试库dbtest001,以及测试用户testuser 对dbtest001库有读写的权限
use dbtest001
db.createUser({user:"testuser",pwd:"testuser123",roles:[{role:"readWrite",db:"dbtest001"}]})
创建测试用户testuser 对dbtest001库有读的权限:
db.createUser({user:"testuser01",pwd:"testuser123",roles:[{role:"read",db:"dbtest001"}]})
给测试库插入一个集合chenji(所谓的表chenji):
db.chenji.insert({"name":"小花","年级":"二年级","性别":"男","爱好":"学习"})
WriteResult({ "nInserted" : 1 })
> db.chenji.insert({"name":"花花","年级":"一年级","性别":"女","爱好":"唱歌"})
WriteResult({ "nInserted" : 1 })
> db.chenji.insert({"name":"小花","年级":"三年级","性别":"女","爱好":"打球"})
WriteResult({ "nInserted" : 1 })
> db.chenji.insert({"name":"小花","年级":"四年级","性别":"女","爱好":"画画"})
WriteResult({ "nInserted" : 1 })
查看当前库下的用户:
db.getUsers() 或者show users
> show users
{
"_id" : "dbtest001.testuser",
"userId" : UUID("f275fad5-ac4f-4dfa-aea6-af34ed93c3e1"),
"user" : "testuser",
"db" : "dbtest001",
"roles" : [
{
"role" : "readWrite",
"db" : "dbtest001"
}
]
}
查看当前库下某个用户的权限:
db.getUser("testuser")
db.getUser("testuser01")
创建角色:db.createRole()
更新角色: db.updateRole()
删除角色: db.dropRole()
获得某个角色信息: db.getRole()
> db.getRole("read")
{
"role" : "read",
"db" : "dbtest001",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
> db.getRole("readWrite")
{
"role" : "readWrite",
"db" : "dbtest001",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
删除所有用户:db.dropAllUsers()
删除用户:db.dropUsers("xxxxxx")
db.dropUser("testuser01")
true
将一个角色赋予读写给用户: db.grantRolesToUser()
use dbtest001
db.grantRolesToUser('testuser01',[ { role : 'readWrite', db : 'dbtest001' } ] )
撤销某个用户的某个角色权限: db.revokeRolesFromUser()
> db.getUser("testuser01")
{
"_id" : "dbtest001.testuser01",
"userId" : UUID("d1fefe19-a94f-4300-8855-fd722f14e13f"),
"user" : "testuser01",
"db" : "dbtest001",
"roles" : [
{
"role" : "readWrite",
"db" : "dbtest001"
}
]
}
撤回读role读写权限:
> db.revokeRolesFromUser('testuser01',[ { role : 'readWrite', db : 'dbtest001' }])
> db.getUser("testuser01")
{
"_id" : "dbtest001.testuser01",
"userId" : UUID("d1fefe19-a94f-4300-8855-fd722f14e13f"),
"user" : "testuser01",
"db" : "dbtest001",
"roles" : [ ]
}
更改密码: db.changeUserPassword()
db.createUser({user:"testuser01",pwd:"testuser123",roles:[{role:"read",db:"dbtest001"}]})
修改密码:
> db.changeUserPassword("testuser01","123456")
测试:使用原密码登录报错如下:
[root@localhost ~]# mongo 127.0.0.1:6068/dbtest001 -u testuser01 -p 'testuser123'
MongoDB shell version v3.6.16
connecting to: mongodb://127.0.0.1:6068/dbtest001?gssapiServiceName=mongodb
2020-01-01T21:51:35.637+0800 E QUERY [thread1] Error: Authentication failed. :
connect@src/mongo/shell/mongo.js:263:13
@(connect):1:6
exception: connect failed
使用新的密码登录成功
[root@localhost ~]# mongo 127.0.0.1:6068/dbtest001 -u testuser01 -p '123456'
MongoDB shell version v3.6.16
connecting to: mongodb://127.0.0.1:6068/dbtest001?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("966023f0-ff7b-4726-a216-8475bc729971") }
MongoDB server version: 3.6.16
> show collections;
chenji
> db.chenji.find();
{ "_id" : ObjectId("5e0c80a8efc383349153386a"), "name" : "小花", "年级" : "二年级", "性别" : "男", "爱好" : "学习" }
{ "_id" : ObjectId("5e0c80c3efc383349153386b"), "name" : "花花", "年级" : "一年级", "性别" : "女", "爱好" : "唱歌" }
{ "_id" : ObjectId("5e0c83c8efc383349153386c"), "name" : "小花", "年级" : "三年级", "性别" : "女", "爱好" : "打球" }
{ "_id" : ObjectId("5e0c83e9efc383349153386d"), "name" : "小花", "年级" : "四年级", "性别" : "女", "爱好" : "画画" }
查看当前库下的表:
show collections
chenji
查看集合中所有的内容:
> db.chenji.find();
{ "_id" : ObjectId("5e0c80a8efc383349153386a"), "name" : "小花", "年级" : "二年级", "性别" : "男", "爱好" : "学习" }
{ "_id" : ObjectId("5e0c80c3efc383349153386b"), "name" : "花花", "年级" : "一年级", "性别" : "女", "爱好" : "唱歌" }
查看名字叫小花的记录:
> db.chenji.find({"name":"小花"})
{ "_id" : ObjectId("5e0c80a8efc383349153386a"), "name" : "小花", "年级" : "二年级", "性别" : "男", "爱好" : "学习" }
{ "_id" : ObjectId("5e0c83c8efc383349153386c"), "name" : "小花", "年级" : "三年级", "性别" : "女", "爱好" : "打球" }
{ "_id" : ObjectId("5e0c83e9efc383349153386d"), "name" : "小花", "年级" : "四年级", "性别" : "女", "爱好" : "画画" }
到此为止, 关于MongoDB中的权限管理有了一个基础的认识, 但是对于具体的使用方法还是需要多加巩固和练习,如果想了解更多相关内容,请关注创新互联行业资讯。
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。