21.29 mongodb用户管理

21.29 mongodb用户管理

use admin//需要切换到admin库
db.createUser( { user: "admin", customData: {description: "superuser"}, pwd: "admin122", roles: [ { role: "root", db: "admin" } ] } )
user指定用户,customData为说明字段,可以省略,pwd为密码,roles指定用户的角色,db指定库名
use admin //切换到admin库
db.system.users.find() //列出所有用户,需要切换到admin库
show users //查看当前库下所有的用户
db.dropUser('admin') //删除用户
若要用户生效,还需要编辑启动脚本vim /usr/lib/systemd/system/mongod.service,在OPTIONS=后面增--auth
重启服务systemctl restart mongod
mongo -u "admin" -p "admin122" --authenticationDatabase "admin"

[root@Dasoncheng src]# mongo
MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.9
> use admin    ##需要切换到admin库里面才能创建用户;切换到库才能创建用户(用户针对库)
switched to db admin
> db.createUser( { user: "admin", customData: {description: "superuser"}, pwd: "admin122", roles: [ { role: "root", db: "admin" } ] } )  
##user: "admin"  //用户名
##customData: {description: "superuser"}  //描述,可不要
##pwd: "admin122" //密码
##roles:  //角色,里面又包含了两个键值对;role: "root"角色是root、db: "admin"针对的是admin库;
Successfully added user: {
	"user" : "admin",
	"customData" : {
		"description" : "superuser"
	},
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}
> db.system.users.find()  
{ "_id" : "admin.admin", "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "Z69r/apOkJK2zq56nktG3w==", "storedKey" : "vpka49IDqjDTb7tFeRK+YqyCmvA=", "serverKey" : "NNcnjtMeXqmn6SOVspyHtKz/mqU=" } }, "customData" : { "description" : "superuser" }, "roles" : [ { "role" : "root", "db" : "admin" } ] }
> db.createUser({user:"aming",pwd:"p@ssw0rd",roles:[{role:"read",db:"testdb"}]})
##创建用户aming 并设为只读;
Successfully added user: {
	"user" : "aming",
	"roles" : [
		{
			"role" : "read",
			"db" : "testdb"
		}
	]
}
> show users   ##查看用户,已经包含了aming
{
	"_id" : "admin.admin",
	"user" : "admin",
	"db" : "admin",
	"customData" : {
		"description" : "superuser"
	},
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}
{
	"_id" : "admin.aming",
	"user" : "aming",
	"db" : "admin",
	"roles" : [
		{
			"role" : "read",
			"db" : "testdb"
		}
}
> db.dropUser('aming')    ##删除用户aming
true
> use testdb   ##切换库,若库不存在 则创建!
switched to db testdb
> show users   ##无法查看用户,在哪个库里面创建的用户 就去哪个库查看;
> use admin
switched to db admin
> show users
{
	"_id" : "admin.admin",
	"user" : "admin",
	"db" : "admin",
	"customData" : {
		"description" : "superuser"
	},
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}	

如果要使创建的用户生效,则需要编辑启动脚本:
vim /usr/lib/systemd/system/mongod.service 在OPTIONS=后面增--auth

[root@Dasoncheng src]# vim /usr/lib/systemd/system/mongod.service
Environment="OPTIONS=--auth -f /etc/mongod.conf"
……
[root@Dasoncheng src]# systemctl restart mongod
Warning: mongod.service changed on disk. Run 'systemctl daemon-reload' to reload units.
##看清楚提示!
[root@Dasoncheng src]# systemctl daemon-reload
[root@Dasoncheng src]# systemctl restart mongod
[root@Dasoncheng src]# ps aux |grep mongo
mongod    39100  7.4  3.5 972180 35668 ?        Sl   18:53   0:01 /usr/bin/mongod --auth -f /etc/mongod.conf
##进程这里多了一个--auth验证,用户才会生效
root      39125  0.0  0.0 112664   968 pts/1    S+   18:53   0:00 grep --color=auto mongo
[root@Dasoncheng src]# mongo --host 192.168.60.11 --port 27017 -u admin -p 'admin122' --authenticationDatabase "admin"  
##登录;
MongoDB shell version v3.4.9
connecting to: mongodb://192.168.60.11:27017/
MongoDB server version: 3.4.9
> use admin
switched to db admin
> show users
{
	"_id" : "admin.admin",
	"user" : "admin",
	"db" : "admin",
	"customData" : {
		"description" : "superuser"
	},
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}
权限说明:

use db1
db.createUser( { user: "test1", pwd: "123aaa", roles: [ { role: "readWrite", db: "db1" }, {role: "read", db: "db2" } ] } )
test1用户对db1库读写,对db2库只读。
之所以先use db1,表示用户在 db1 库中创建,就一定要db1库验证身份,即用户的信息跟随随数据库。比如上述 test1虽然有 db2 库的读取权限,但是一定要先在db1库进行身份验证,直接访问会提示验证失败。
use db2
db.auth("test1", "123aaa")

> use db1
switched to db db1
> db.createUser( { user: "test1", pwd: "123aaa", roles: [ { role: "readWrite", db: "db1" }, {role: "read", db: "db2" } ] } )
Successfully added user: {
	"user" : "test1",
	"roles" : [
		{
			"role" : "readWrite",
			"db" : "db1"
		},
		{
			"role" : "read",
			"db" : "db2"
		}
	]
}
> use db2
switched to db db2
> db.auth('test1','123aaa')
Error: Authentication failed.
0
> use db1    ##只有先在db1里面验证身份之后,才能对db2有该有的权限;
switched to db db1
> db.auth('test1','123aaa')
1
MongoDB用户角色
  • Read:允许用户读取指定数据库
  • readWrite:允许用户读写指定数据库
  • dbAdmin:允许用户在指定数据库中执行管理函数,如索引创建、删除,查看统计或访问system.profile
  • userAdmin:允许用户向system.users集合写入,可以找指定数据库里创建、删除和管理用户
  • clusterAdmin:只在admin数据库中可用,赋予用户所有分片和复制集相关函数的管理权限。
  • readAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读权限
  • readWriteAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读写权限
  • userAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的userAdmin权限
  • dbAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的dbAdmin权限。
  • root:只在admin数据库中可用。超级账号,超级权限
MongoDB库管理
  • db.version() //查看版本
  • use userdb //如果库存在就切换,不存在就创建
  • show dbs //查看库,此时userdb并没有出现,这是因为该库是空的,还没有任何集合,只需要创建一个集合就能看到了
  • db.createCollection('clo1') //创建集合clo1,在当前库下面创建
  • db.dropDatabase() //删除当前库,要想删除某个库,必须切换到那个库下
  • db.stats() //查看当前库的信息
  • db.serverStatus() //查看mongodb服务器的状态

小说明:创建用户只针对库;

转载于:https://my.oschina.net/u/3651233/blog/1552102

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值