基于賬號(hào)的驗(yàn)證登錄
屏蔽conf的security
$ vi /etc/mongod.conf
# security:
# authorization: "enabled"
$ systemctl restart mongod.service
創(chuàng)建超級(jí)用戶(hù)
$ mogodb
$ use admin
$ db.createUser({ user: "root" , pwd: "123", roles: ["userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase"]});
規(guī)則
- 參考
- 超級(jí)權(quán)限
readWriteAnyDatabase, dbAdminAnyDatabase, userAdminAnyDatabase, clusterAdmin
- 上線只用一個(gè)超級(jí)用戶(hù)肯定不合適坦冠,可以針對(duì)具體數(shù)據(jù)建用戶(hù)
上線運(yùn)行設(shè)置 readWrite
單庫(kù)管理可以設(shè)置 dbOwner
- 權(quán)限維度
單庫(kù)膨疏、超級(jí)用戶(hù)、服務(wù)器群組髓需、備份
啟動(dòng)授權(quán)登錄
$ vi /etc/mongod.conf
security:
authorization: "enabled"
驗(yàn)證登錄
$ mongo
$ show dbs
2016-12-07T13:06:20.774-0500 E QUERY [main] Error: listDatabases failed:{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
"code" : 13,
"codeName" : "Unauthorized"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1
shellHelper.show@src/mongo/shell/utils.js:755:19
shellHelper@src/mongo/shell/utils.js:645:15
@(shellhelp2):1:1
現(xiàn)在默認(rèn)登錄 就沒(méi)權(quán)限了
> use admin
> db.auth('root','123');
1
> show dbs
admin 0.000GB
local 0.000GB
驗(yàn)證成功