Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 知乎專欄 | 視頻教程 | About

第 12 章 MongoDB 管理

目錄

12.1. Security and Authentication
12.1.1. 超級管理員
12.1.2. 資料庫訪問用戶
12.1.3. 資料庫監控用戶
12.1.4. 刪除用戶
12.1.5. 更新角色

12.1. Security and Authentication

開啟認證

# vim /etc/mongodb.conf
auth = true
		

重載配置檔案

# /etc/init.d/mongod reload
Stopping mongod:                                           [  OK  ]
Starting mongod:                                           [  OK  ]
		

12.1.1. 超級管理員

Database Administration Roles
			
use admin;
db.createUser(
   {
     user: "admin",
     pwd: "WkAFdmfVQpP1oAEkz4YVlMCDxkG36TAi",
     roles: [ "dbAdmin", "dbOwner", "userAdmin" ]
   }
);
			
			

12.1.2. 資料庫訪問用戶

注意,只有創建了超級管理後,下面的操作才會生效

MongoDB

			
use products
db.createUser(
   {
     user: "accountUser",
     pwd: "password",
     roles: [ "readWrite", "dbAdmin" ]
   }
)
			
			

早期版本

			
> use neo
switched to db neo
> db.addUser('neo','chen')
{
        "user" : "neo",
        "readOnly" : false,
        "pwd" : "68ace374737253d87e0ec91d4fcb673d"
}

> db.system.users.find()
{ "_id" : ObjectId("4c481404b9db6474d2fcb76f"), "user" : "neo", "readOnly" : false, "pwd" : "68ace374737253d87e0ec91d4fcb673d" }

> db.auth('neo','chen')
1
			
			

12.1.3. 資料庫監控用戶

			
db.createUser(
   {
     user: "monitor",
     pwd: "netkiller",
     roles: [ "clusterMonitor"]
   }
)			
			
			

12.1.4. 刪除用戶

Deleting Users 刪除用戶

To delete a user:

			
> db.getUsers();
[
	{
		"_id" : "test.monitor",
		"user" : "monitor",
		"db" : "test",
		"roles" : [
			{
				"role" : "dbOwner",
				"db" : "test"
			}
		]
	}
]
			
> db.dropUser('monitor')
ture

> db.getUsers();
[ ]
						
			

早期版本

			
db.system.users.remove( { user: username } )
			
			

12.1.5. 更新角色

			

db.updateUser( "monitor",
   {
     roles: [ "read", "clusterMonitor" ]
   }
)