MongoDB用户管理

用户管理范围

  • 用户名、密码
  • 角色(权限:只读,读写,超管等等)
  • 范围(对哪些库有执行权限)

启用认证

use admin
> db.createUser({user:"root",pwd:"root",roles:[{role:"root",db:"admin"}]})
Successfully added user: {
	"user" : "root",
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}
> db.auth("root","root")
1

认证用户创建成功后退出修改mongodb配置文件然后重启
在行尾加入

vim mongod.conf
security:
    authorization: enabled
  • 验证认证用户是否创建成功
    未输入验证用户
mongo
> show dbs
> show tables
Warning: unable to run listCollections, attempting to approximate collection names by parsing connectionStatus
> exit

输入验证用户

[root@localhost conf]# mongo -uroot -proot admin
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
  • 创建一个对test库的只读用户test-read
    用root登录mongodb
> use test
switched to db test
> db.createUser({user:"test-read",pwd:"123456",roles:[{role:"read",db:"test"}]})
Successfully added user: {
	"user" : "test-read",
	"roles" : [
		{
			"role" : "read",
			"db" : "test"
		}
	]
}
  • 验证test-read用户是否创建成功
> db.system.users.find()
{ "_id" : "admin.root", "userId" : UUID("d0781985-a9ff-454a-bc60-cd4ac45902f8"), "user" : "root", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "PuLTpRJAnK2MdVwBj5KAlw==", "storedKey" : "tejQhliRyhTn+XDwTSO8vfzZJJY=", "serverKey" : "yRw+MD/ryymGrgGbsDNmnt9yYoE=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "/EeWxFocy/cxdnrD+XZ0Tx+z/FRnZv8ybpJdUg==", "storedKey" : "WjMLjd7zHwIKbyEiM/iaBsOWv75jRPUoN3m/tOONLFo=", "serverKey" : "UcYveQMY/k4TXhr10xBy+voU21kt4Mu3G52BrHDTdsE=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] }
{ "_id" : "test.test-read", "userId" : UUID("6fad7d9d-9987-43a0-8fe5-63937251d9f7"), "user" : "test-read", "db" : "test", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "iiJEuG/mHAnhTjIpGY9jKw==", "storedKey" : "1KVPCa1Qv9rsWE0NHj5GFAxoz3E=", "serverKey" : "yiOmZzS5Y/y/mSyBI2dbjxgVjnI=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "BiE7N07G0pBdCkThO65OpZTiOoHkLn/tD1aDZg==", "storedKey" : "n+H+kStdlin08wFrqQn2Ab/iqDvYNzMIs2bRnkrVkO8=", "serverKey" : "oEftyIyv8RobzLlHtOPilWusXQdhu1hbnrZgZ2xYh/U=" } }, "roles" : [ { "role" : "read", "db" : "test" } ] }
  • 以test-read用户登录并创建一个test集合
 mongo -u"test-read" -p123456 test
 > db.test.insert({name:"mongo"})
WriteCommandError({
	"ok" : 0,
	"errmsg" : "not authorized on test to execute command { insert: \"test\", ordered: true, lsid: { id: UUID(\"8c9e94c8-d9bf-4378-b9eb-2f811162c374\") }, $db: \"test\" }",
	"code" : 13,
	"codeName" : "Unauthorized"
})
  • 创建一个test库的读写用户
    还是以root用户登录
> use test
switched to db test
> db.createUser({user:"test-rw",pwd:"123456",roles:[{role:"readWrite",db:"test"}]})
Successfully added user: {
	"user" : "test-rw",
	"roles" : [
		{
			"role" : "readWrite",
			"db" : "test"
		}
	]
}
  • 退出以test-rw用户登录创建test集合
> db.test.insert({name:"mongo"})
WriteResult({ "nInserted" : 1 })
> show tables
test
> db.test.find()
{ "_id" : ObjectId("5f0bd0c96409669331de55cd"), "name" : "mongo" }
  • 再以test-read用户登录查看是否可读
> show tables
test
> db.test.find()
{ "_id" : ObjectId("5f0bd0c96409669331de55cd"), "name" : "mongo" }
  • 删除用户
    还是以root用户登录
> use test
switched to db test
> db.dropUser("test-read")
true
> use admin
switched to db admin
> db.system.users.find()
{ "_id" : "admin.root", "userId" : UUID("d0781985-a9ff-454a-bc60-cd4ac45902f8"), "user" : "root", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "PuLTpRJAnK2MdVwBj5KAlw==", "storedKey" : "tejQhliRyhTn+XDwTSO8vfzZJJY=", "serverKey" : "yRw+MD/ryymGrgGbsDNmnt9yYoE=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "/EeWxFocy/cxdnrD+XZ0Tx+z/FRnZv8ybpJdUg==", "storedKey" : "WjMLjd7zHwIKbyEiM/iaBsOWv75jRPUoN3m/tOONLFo=", "serverKey" : "UcYveQMY/k4TXhr10xBy+voU21kt4Mu3G52BrHDTdsE=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] }
{ "_id" : "test.test-rw", "userId" : UUID("87ced292-7002-4aa8-97f4-51fe2b2092eb"), "user" : "test-rw", "db" : "test", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "fm8yHKrzyzjZYu9z+zoKSA==", "storedKey" : "IGTHdZqX323gaeSubUnArB1CCfY=", "serverKey" : "3CfFP2T4LSxkuoQ43aJQbQiPQwA=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "sBhYrXGUIhP5M9CtTAiDrsR1Ph+B1RxIlC7xVw==", "storedKey" : "Ml88HbnDzTtt9swZZamPAL9TIWOeKIj7gdoLrTHhh54=", "serverKey" : "AFxnf9A3rQlmr8O5y3sTGID8aMAiHpteOOuFLgVNpP0=" } }, "roles" : [ { "role" : "readWrite", "db" : "test" } ] }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值