- Mastering MongoDB 3.x
- Alex Giamas
- 289字
- 2021-08-20 10:10:57
Authorization with MongoDB
MongoDB's most basic authorization relies on the username/password method. By default, MongoDB will not start with authorization enabled. To enable it, we need to start our server with the --auth parameter:
$ mongod --auth
To set up authorization, we need to start our server without authorization to set up a user. Setting up an admin user is as simple as follows:
> use admin
> db.createUser(
{
user: <adminUser>,
pwd: <password>,
roles: [ { role: <adminRole>, db: "admin" } ]
}
)
Here, <adminUser> is the name of the user we want to create, <password> is the password, and <adminRole> can be any of the following values ordered from more powerful to least:
- root
- dbAdminAnyDatabase
- userAdminAnyDatabase
- readWriteAnyDatabase
- readAnyDatabase
- dbOwner
- dbAdmin
- userAdmin
- readWrite
- read
Of these roles, root is the superuser allowed access to everything. This is not recommended to be used, except for special circumstances.
All the AnyDatabase roles provide access to all databases, of which dbAdminAnyDatabase combines the userAdminAnyDatabase and readWriteAnyDatabase scopes, being an admin again, in all databases.
The rest of the roles are defined in the database that we want them to apply, by changing the roles subdocument of the preceding db.createUser(). For example, to create a dbAdmin for our mongo_book database , we would use the following:
> db.createUser(
{
user: <adminUser>,
pwd: <password>,
roles: [ { role: "dbAdmin", db: "mongo_book" } ]
}
)
Cluster administration has even more roles, which we will cover in more depth in Chapter 10, Replication.
Finally, when we restart our database with the --auth flag set, we can use either the command line or the connection string (from any driver) to connect as admin and create new users with predefined or custom defined roles:
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
- Drupal 7 Multilingual Sites
- 返璞歸真:UNIX技術(shù)內(nèi)幕
- 流處理器研究與設(shè)計(jì)
- 完全掌握AutoCAD 2008中文版:綜合篇
- 變頻器、軟啟動器及PLC實(shí)用技術(shù)260問
- 工業(yè)機(jī)器人應(yīng)用案例集錦
- Blender 3D Printing by Example
- 過程控制系統(tǒng)
- Flink原理與實(shí)踐
- 基于RPA技術(shù)財(cái)務(wù)機(jī)器人的應(yīng)用與研究
- 計(jì)算機(jī)組裝與維修實(shí)訓(xùn)
- 網(wǎng)絡(luò)安全概論
- 軟件測試設(shè)計(jì)
- Java Deep Learning Projects
- Kibana 7 Quick Start Guide