MongoDB3.0.x版本用戶授權配置(單機環境)


      MongoDB數據庫默認情況下是沒有做權限控制的,只要能夠連接所開放的端口就能進行訪問,而且擁有root級別的權限;對於生產環境而言是極不安全的,所以需要建立用戶,進行授權控制。

  • 單機環境下的用戶授權模塊配置:

MongoDB的社區版本中有兩個模塊可以控制用戶的訪問:

--auth: 在mongod啟動項中加入--auth,mongodb啟動后,就可以完成授權模塊的啟用);
PS:雖然auth模塊啟用后本機還能否登陸到數據庫,但是不具備增刪改查的權限了,所以啟動auth模塊之前就應該創建一個超級用戶
--keyFile <file>: 主要用於分片集群與副本集相互之間的授權使用,在單機情況下只要用到auth,如果是在集群(分片+副本集)環境下,就必須要用到該參數;
security.authorization: 在MongoDB 2.6版本開始,mongod/mongos的啟動配置文件增加了YAML格式的寫法,功能更auth是一樣的,后面的操作中,都是采用該格式
security.keyFile: 格式與security.authorization相同,功能與--keyFile相同。
  • 首先驗證下非配置認證模塊的訪問:

[root@fo169 bin]# ./mongo
MongoDB shell version: 3.0.7 connecting to: test Server has startup warnings: 2015-10-29T15:12:14.257+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended. 2015-10-29T15:12:14.257+0800 I CONTROL [initandlisten] > show dbs local 0.000GB

 在沒有配置的情況下,登錄到數據庫后,可以做任何操作。

  • 配置認證模塊及重啟服務: 

編寫了一個啟動配置文件:mongodb.conf(文件中標紅部分就為auth的授權模塊)

[root@fo169 bin]# cat mongodb.conf 
systemLog:
   destination: file path: "/data/auth/log/mongod.log" logAppend: true storage: journal: enabled: true dbPath: "/data/auth/db" directoryPerDB: true engine: wiredTiger wiredTiger: engineConfig: cacheSizeGB: 4 directoryForIndexes: true journalCompressor: zlib collectionConfig: blockCompressor: zlib indexConfig: prefixCompression: true net: port: 27017 processManagement: fork: true security: authorization: enabled 
  • 創建授權用戶(超級管理員): 

MongoDB在V3.0版本之后內置了root 角色,也就是結合了readWriteAnyDatabase、dbAdminAnyDatabase、userAdminAnyDatabase、clusterAdmin4個角色權限,類似於ORACLE的sysdba角色,但是MongoDB的超級管理員用戶名稱是可以隨便定義的:

[root@fo169 bin]# ./mongo
MongoDB shell version: 3.0.7
connecting to: test
Server has startup warnings: 
2015-10-30T16:24:36.127+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2015-10-30T16:24:36.127+0800 I CONTROL  [initandlisten] 
> use admin
switched to db admin
> db.createUser(
...    {
...      user: "ljaiadmin",
...      pwd: "123456",
...     roles: [ { role: "root", db: "admin" } ] ... } ... )
Successfully added user: {
        "user" : "ljaiadmin",
        "roles" : [
                {
                        "role" : "root",
                        "db" : "admin"
                }
        ]
}

這樣就創建好一個ljaiadmin的超級管理員用戶,創建全局用戶或者超級用戶,需要在MongoDB的admin數據庫中創建(在其他庫也可以創建,但是沒有該角色功能),重啟完mongod進程后,接下來做一下權限的驗證:

[root@fo169 bin]# ./mongo
MongoDB shell version: 3.0.7
connecting to: test
> show dbs  (注:此時查看已提示沒有授權執行listDatabases命令了) 2015-10-30T16:41:31.131+0800 E QUERY    Error: listDatabases failed:{
        "ok" : 0,
        "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
        "code" : 13
}
    at Error (<anonymous>)
    at Mongo.getDBs (src/mongo/shell/mongo.js:47:15)
    at shellHelper.show (src/mongo/shell/utils.js:630:33)
    at shellHelper (src/mongo/shell/utils.js:524:36)
    at (shellhelp2):1:1 at src/mongo/shell/mongo.js:47
> use admin
switched to db admin
> db.auth('ljaiadmin','123456') (注:切換到admin用戶進行授權驗證) 1
> show dbs                      (注:驗證完成后,就可以讀寫等操作)
admin    0.000GB
local    0.000GB
test100  0.000GB
test2    0.000GB
> use test2
switched to db test2
> show tables
test
test2
> db.test2.find()
{ "_id" : ObjectId("5632cf116207909a76446af7"), "name" : "1" }
> db.test2.drop()
true
> db.dropDatabase()
{ "dropped" : "test2", "ok" : 1 }
> show dbs
admin    0.000GB
local    0.000GB
test100  0.000GB
> use test100
switched to db test100
> db.test111.insert({"test":"test"})
WriteResult({ "nInserted" : 1 })
> db.test111.find()
{ "_id" : ObjectId("56332db373f771b3d95638bb"), "test" : "test" }
> use admin
switched to db admin
> show users
{
        "_id" : "admin.ljaiadmin",
        "user" : "ljaiadmin",
        "db" : "admin",
        "roles" : [
                {
                        "role" : "root",
                        "db" : "admin"
                }
        ]
}
> 
  • 創建普通用戶

用可以對test123數據庫讀寫的rwtest123用戶為例:

> use test123
switched to db test123
> db.createUser(
...    {
...      user: "rwtest123",
...      pwd: "123456",
...     roles: [ { role: "readWrite", db: "test123" } ] ... } ... )
Successfully added user: {
        "user" : "rwtest123",
        "roles" : [
                {
                        "role" : "readWrite",
                        "db" : "test123"
                }
        ]
}

#所建的rwtest123用戶可以在test123數據庫中進行增刪改查操作,但是其他操作就不行了
>db.auth('rwtest123','123456') switched to db test123 > db.test123.insert({"test":"test"}) WriteResult({ "nInserted" : 1 }) > db.test123.find() { "_id" : ObjectId("563332ebc8a59ae4fe96bbf5"), "test" : "test" } > db.test123.drop() true > use test100 switched to db test100 > db.test100.find() Error: error: { "$err" : "not authorized for query on test100.test100", "code" : 13 } >
  • 配置參考:

 MongoDB數據庫的用戶權限控制權限還是比較多的,有系統自帶的,已經定義好的角色,也可以自己定義角色權限,需要根據業務需要進行權限分配:

自帶角色的說明(一般內置的角色基本上就可以滿足生產環境需求了):

https://docs.mongodb.org/manual/core/security-built-in-roles/

用戶自行定義角色的說明:

https://docs.mongodb.org/manual/core/security-user-defined-roles/

用戶管理配置的說明

https://docs.mongodb.org/manual/reference/method/#user-management-methods


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM