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"mongodb

[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=後面增--authshell

[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服務器的狀態

小說明:建立用戶只針對庫;vim

相關文章
相關標籤/搜索