最近花了一些時間學習了下MongoDB數據庫,感受仍是比較全面系統的,涉及了軟件安裝、客戶端操做、安全認證、副本集和分佈式集羣搭建,以及使用Spring Data鏈接MongoDB進行數據操做,收穫很大。特此記錄,以備查看。html
文章目錄:mongodb
MongoDB和Java(1):Linux下的MongoDB安裝shell
MongoDB和Java(2):普通用戶啓動mongod進程數據庫
MongoDB和Java(3):Java操做MongoB安全
MongoDB和Java(4):Spring Data整合MongoDB(XML配置)分佈式
MongoDB和Java(5):Spring Data整合MongoDB(註解配置)學習
MongoDB和Java(6):Spring Data整合MongoDB副本集、分片集羣ui
MongoDB和Java(7):MongoDB用戶管理spa
本文記錄如何開啓MongoDB認證、添加用戶prototype
MongoDB中的每一個數據庫有一些用戶(得建立),這些用戶有的只能操做本身所屬庫的表,有的能夠操做其餘庫的表,這取決於它擁有的角色。
一個用戶能夠有多個角色,角色包含若干權限,權限又擁有資源、操做。
簡單來講就是 用戶 — 角色 — 權限 的權限管理體系。
關於角色、權限、資源等官網有文檔:
https://docs.mongodb.com/manual/reference/built-in-roles/
https://docs.mongodb.com/manual/reference/resource-document/
https://docs.mongodb.com/manual/reference/privilege-actions/
權限資源就不作詳細介紹了,由於建立用戶使用內置角色就足夠了,不太可能去本身去建立角色
看一下內置角色
Database User Roles
read、readWrite
Database Administration Roles
dbAdmin、dbOwner、userAdmin,其中dbOwner權限最高
Cluster Administration Roles
clusterAdmin、clusterManager、clusterMonitor、hostManager
Backup and Restoration Roles
backup、restore
All-Database Roles
readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
Superuser Roles
root
1 [root@xugf-test4 ~]# mongo 2 > use admin 3 switched to db admin 4 > db.createUser({ 5 ... user: "admin", 6 ... pwd: "123456", 7 ... roles: [{role: "root", db: "admin"}], 8 ... mechanisms: ["SCRAM-SHA-1"] 9 ... }) 10 Successfully added user: { 11 "user" : "admin", 12 "roles" : [ 13 { 14 "role" : "root", 15 "db" : "admin" 16 } 17 ], 18 "mechanisms" : [ 19 "SCRAM-SHA-1" 20 ] 21 }
修改mongo.conf配置文件,開啓權限認證功能,auth屬性設置true
1 [mongo@xugf-test4 ~]$ cat /etc/mongo.conf 2 dbpath=/data/mongo/db/ 3 logpath=/data/mongo/log/mongodb.log 4 bind_ip_all=true 5 fork=true 6 auth=true
重啓mongodb
再使用mongo鏈接,進行操做時會提示未認證
1 [root@xugf-test4 ~]# mongo 2 > show dbs 3 2018-10-12T10:16:00.683+0800 E QUERY [js] Error: listDatabases failed:{ 4 "ok" : 0, 5 "errmsg" : "command listDatabases requires authentication", 6 "code" : 13, 7 "codeName" : "Unauthorized" 8 } : 9 _getErrorWithCode@src/mongo/shell/utils.js:25:13 10 Mongo.prototype.getDBs@src/mongo/shell/mongo.js:67:1 11 shellHelper.show@src/mongo/shell/utils.js:876:19 12 shellHelper@src/mongo/shell/utils.js:766:15 13 @(shellhelp2):1:1
此時,有兩種方式進行客戶端認證:
1)在鏈接時使用--authenticationDatabase選項指定認證數據庫,使用-u選項指定用戶名,使用-p指定密碼
1 [root@xugf-test4 ~]# mongo -u admin -p --authenticationDatabase admin 2 MongoDB shell version v4.0.2 3 Enter password: 4 MongoDB server version: 4.0.2 5 > show dbs 6 admin 0.000GB 7 config 0.000GB 8 local 0.000GB
2)在鏈接後切換到認證數據庫後,使用db.auth("username", "password")進行認證
1 [root@xugf-test4 ~]# mongo 2 MongoDB shell version v4.0.2 3 connecting to: mongodb://127.0.0.1:27017 4 MongoDB server version: 4.0.2 5 > use admin 6 switched to db admin 7 > db.auth("admin", "123456") 8 1 9 > show dbs 10 admin 0.000GB 11 config 0.000GB 12 local 0.000GB
給test庫添加一個數據庫管理員testAdmin
1 > use test 2 switched to db test 3 > db.createUser({ 4 ... user: "testAdmin", 5 ... pwd: "123456", 6 ... roles: [{role: "dbOwner", db: "test"}], 7 ... mechanisms: ["SCRAM-SHA-1"] 8 ... }) 9 Successfully added user: { 10 "user" : "testAdmin", 11 "roles" : [ 12 { 13 "role" : "dbOwner", 14 "db" : "test" 15 } 16 ], 17 "mechanisms" : [ 18 "SCRAM-SHA-1" 19 ] 20 }
使用testAdmin鏈接
1 [root@xugf-test4 ~]# mongo -u testAdmin -p --authenticationDatabase test 2 > db 3 test 4 > db.getUsers() 5 [ 6 { 7 "_id" : "test.testAdmin", 8 "user" : "testAdmin", 9 "db" : "test", 10 "roles" : [ 11 { 12 "role" : "dbOwner", 13 "db" : "test" 14 } 15 ], 16 "mechanisms" : [ 17 "SCRAM-SHA-1" 18 ] 19 } 20 ]
給test庫添加一個xugf用戶
1 > use test 2 switched to db test 3 > db.createUser({ 4 ... user: "xugf", 5 ... pwd: "123456", 6 ... roles: [{role: "readWrite", db: "test"}], 7 ... mechanisms: ["SCRAM-SHA-1"] 8 ... }) 9 Successfully added user: { 10 "user" : "xugf", 11 "roles" : [ 12 { 13 "role" : "readWrite", 14 "db" : "test" 15 } 16 ], 17 "mechanisms" : [ 18 "SCRAM-SHA-1" 19 ] 20 }
使用xugf鏈接
1 [root@xugf-test4 ~]# mongo -u xugf -p 123456 --authenticationDatabase test 2 MongoDB shell version v4.0.2 3 connecting to: mongodb://127.0.0.1:27017 4 MongoDB server version: 4.0.2 5 > 6 > db 7 test