Mongodb的實踐二:初識

Mongodb 系列教程

  1. Mongodb的實踐一:安裝
  2. Mongodb的實踐二:初識
  3. Mongodb的實踐三:
  4. Mongodb的實踐四:
  5. Mongodb的實踐五:
  6. Mongodb的實踐六:
  7. Mongodb的實踐七:
  8. Mongodb的實踐八:

術語

db->數據庫
collection->集合
document->文檔
index->索引
cluster->集羣
shard->分片

數據類型

MongoDB文檔存儲是使用BSON類型
類型的官方文檔:https://docs.mongodb.com/manu...
BSON官網:http://bsonspec.org/

Type Number Alias Notes
Double 1 “double”
String 2 “string”
Object 3 “object”
Array 4 “array”
Binary data 5 “binData”
Undefined 6 “undefined” Deprecated.
ObjectId 7 “objectId”
Boolean 8 “bool”
Date 9 “date”
Null 10 “null”
Regular Expression 11 “regex”
DBPointer 12 “dbPointer” Deprecated.
JavaScript 13 “javascript”
Symbol 14 “symbol” Deprecated.
JavaScript (with scope) 15 “javascriptWithScope”
32-bit integer 16 “int”
Timestamp 17 “timestamp”
64-bit integer 18 “long”
Decimal128 19 “decimal” New in version 3.4.
Min key -1 “minKey”
Max key 127 “maxKey”

常用數據類型

後續實例說明

內建的角色組

https://docs.mongodb.com/manu...

  1. Database User Roles 這個是針對非系統數據庫和部分系統表的角色組
  2. Database Administration Roles 可以操作所有數據庫
  3. Cluster Administration Roles 管理員族 針對整個系統進行管理
  4. Backup and Restoration Roles 備份還原角色組
  5. All-Database Roles 角色裏面有一些跟超管差不多了級別了,針對所有數據庫的
  6. Superuser Roles 超級管理員 不用多說了
  7. Internal Role 內部系統角色

MongoDB 通過角色基本權限控制授予(用戶)數據和命令的使用權,並且提供給內置角色數據系統一般需要的不同層次的權限。另外,你也可以創建用戶定義角色。

角色授予對定義的資源執行一組操作的權限。給定的角色應用於定義它的數據庫,並且可以授予對粒度集合級別的訪問權限

MongoDB的每個內置角色在數據庫級別爲角色數據庫中的所有非系統集合定義訪問權限,在收集級別爲所有系統集合定義訪問權限
MongoDB在每個數據庫上提供內置的數據庫用戶和數據庫管理角色。MongoDB僅在管理數據庫上提供所有其他內置角色。

本節描述每個內置角色的權限。您還可以隨時查看內置角色的權限,方法是發出rolesinfo命令,並將showprivileges和showbuiltinroles字段都設置爲true。

Database User Roles (數據庫用戶角色)

每個數據庫都包含以下客戶角色:

read(只讀)

提供讀取所有非系統集合和以下系統集合上的數據的能力:system.indexes, system.js, and system.namespaces集合

角色通過授予以下操作提供讀取訪問權限

  • changeStream
  • collStats
  • dbHash
  • dbStats
  • find
  • killCursors
  • listIndexes
  • listCollections

readWrite(讀和寫)

提供讀取角色的所有特權,以及修改所有非系統集合和system.js集合上的數據的能力

該角色對這些集合提供以下操作::

  • collStats
  • convertToCapped
  • createCollection
  • dbHash
  • dbStats
  • dropCollection
  • createIndex
  • dropIndex
  • find
  • insert
  • killCursors
  • listIndexes
  • listCollections
  • remove
  • renameCollectionSameDB
  • update

Database Administration Roles (數據庫管理角色)

每個數據庫都包含以下管理角色:

dbAdmin (數據庫管理員)

提供執行管理任務(如與架構相關的任務、索引和收集統計信息)的能力。此角色不授予用戶和角色管理權限。

在數據庫的system.indexessystem.namespacessystem.profile集合上提供以下操作:

  • collStats
  • dbHash
  • dbStats
  • find
  • killCursors
  • listIndexes
  • listCollections
  • dropCollection and createCollection on system.profile only

在版本2.6.4中進行了更改:dbAdminsystem.profile集合添加了createCollection操作。以前的版本對system.profile集合只有dropCollection操作。

對所有非系統集合提供以下操作。此角色不包括對非系統集合的完全讀取權限:

  • bypassDocumentValidation
  • collMod
  • collStats
  • compact
  • convertToCapped
  • createCollection
  • createIndex
  • dbStats
  • dropCollection
  • dropDatabase
  • dropIndex
  • enableProfiler
  • reIndex
  • renameCollectionSameDB
  • repairDatabase
  • storageDetails
  • validate

dbOwner (數據庫所有者)

數據庫所有者可以對數據庫執行任何管理操作。此角色組合了readWritedbAdminuserAdmin角色授予的權限。

userAdmin (用戶管理員)

提供在當前數據庫上創建和修改角色和用戶的功能。由於userAdmin角色允許用戶向任何用戶(包括用戶本身)授予任何權限,因此該角色還間接提供superuser對數據庫或集羣(如果其作用域是管理數據庫)的訪問。

userAdmin角色顯式提供以下操作:

  • changeCustomData
  • changePassword
  • createRole
  • createUser
  • dropRole
  • dropUser
  • grantRole
  • revokeRole
  • setAuthenticationRestriction
  • viewRole
  • viewUser
理解授予userAdmin角色的安全含義是很重要的:對於數據庫具有此角色的用戶可以爲自己分配該數據庫上的任何特權。在admin數據庫上授予userAdmin角色有進一步的安全隱患,因爲這間接地提供了superuser對集羣的訪問。在admin範圍內,具有userAdmin角色的用戶可以授予集羣範圍內的角色或權限,包括userAdminAnyDatabase

Cluster Administration Roles

The admin database includes the following roles for administering the whole system rather than just a single database. These roles include but are not limited to replica set and sharded cluster administrative functions.

clusterAdmin

Provides the greatest cluster-management access. This role combines the privileges granted by the clusterManager, clusterMonitor, and hostManager roles. Additionally, the role provides the dropDatabase action.

clusterManager

Changed in version 3.4.

Provides management and monitoring actions on the cluster. A user with this role can access the config and local databases, which are used in sharding and replication, respectively.

Provides the following actions on the cluster as a whole:

addShard
appendOplogNote
applicationMessage
cleanupOrphaned
flushRouterConfig
listSessions (New in version 3.6)
listShards
removeShard
replSetConfigure
replSetGetConfig
replSetGetStatus
replSetStateChange
resync
Provides the following actions on all databases in the cluster:

enableSharding
moveChunk
splitChunk
splitVector
On the config database, provides the following privileges:

Resource Actions
All collections in the config database
collStats
dbHash
dbStats
enableSharding
find
insert
killCursors
listCollections
listIndexes
moveChunk
planCacheRead
remove
splitChunk
splitVector
update
system.indexes,
system.js,
system.namespaces collections
collStats
dbHash
dbStats
find
killCursors
listCollections
listIndexes
planCacheRead
On the local database, provides the following privileges:

Resource Actions
All collections in the local database
enableSharding
insert
moveChunk
remove
splitChunk
splitVector
update
system.replset collection
collStats
dbHash
dbStats
find
killCursors
listCollections
listIndexes
planCacheRead

clusterMonitor

Changed in version 3.4.

Provides read-only access to monitoring tools, such as the MongoDB Cloud Manager and Ops Manager monitoring agent.

Provides the following actions on the cluster as a whole:

checkFreeMonitoringStatus (New in version 4.0)
connPoolStats
getCmdLineOpts
getLog
getParameter
getShardMap
hostInfo
inprog
listDatabases
listSessions (New in version 3.6)
listShards
netstat
replSetGetConfig
replSetGetStatus
serverStatus
setFreeMonitoring (New in version 4.0)
shardingState
top
Provides the following actions on all databases in the cluster:

collStats
dbStats
getShardVersion
indexStats
useUUID (New in version 3.6)
Provides the find action on all system.profile collections in the cluster.

On the config database, provides the following privileges:

Resource Actions
All collections in the config database
collStats
dbHash
dbStats
find
getShardVersion
indexStats
killCursors
listCollections
listIndexes
planCacheRead
system.indexes,
system.js,
system.namespaces collections
collStats
dbHash
dbStats
find
killCursors
listCollections
listIndexes
planCacheRead
On the local database, provides the following privileges:

Resource Actions
All collections in the local database
collStats
dbHash
dbStats
find
getShardVersion
indexStats
killCursors
listCollections
listIndexes
planCacheRead
system.indexes,
system.js,
system.namespaces collections
collStats
dbHash
dbStats
find
killCursors
listCollections
listIndexes
planCacheRead
system.replset,
system.profile,
find

hostManager

Provides the ability to monitor and manage servers.

Provides the following actions on the cluster as a whole:

applicationMessage
closeAllDatabases
connPoolSync
cpuProfiler
flushRouterConfig
fsync
invalidateUserCache
killAnyCursor (New in version 4.0)
killAnySession (New in version 3.6)
killop
logRotate
resync
setParameter
shutdown
touch
unlock
Provides the following actions on all databases in the cluster:

killCursors
repairDatabase

Backup and Restoration Roles

The admin database includes the following roles for backing up and restoring data:

backup

Changed in version 3.4.

Provides minimal privileges needed for backing up data. This role provides sufficient privileges to use the MongoDB Cloud Manager backup agent, Ops Manager backup agent, or to use mongodump to back up an entire mongod instance.

Provides the insert and update actions on the mms.backup collection in the admin database and on the settings collection in the config database.

On anyResource, provides the

listDatabases action
listCollections action
listIndexes action
On the cluster as a whole, provides the

appendOplogNote
getParameter
listDatabases
Provides the find action on the following:

all non-system collections in the cluster, including those in the config and local databases
The following system collections in the cluster: system.indexes, system.namespaces, system.js, and system.profile
the admin.system.users and admin.system.roles collections
the config.settings collection
legacy system.users collections from versions of MongoDB prior to 2.6
Provides the insert and update actions on the config.settings collection.

Changed in version 3.2.1: The backup role provides additional privileges to back up the system.profile collection that exists when running with database profiling. Previously, users required read access on this collection.

restore

Changed in version 3.6: Provides convertToCapped on non-system collections.

Provides privileges needed to restore data from backups that do not include system.profile collection data. This role is sufficient when restoring data with mongorestore without the --oplogReplay option.

If the backup data includes system.profile collection data and the target database does not contain the system.profile collection, mongorestore attempts to create the collection even though the program does not actually restore system.profile documents. As such, the user requires additional privileges to perform createCollection and convertToCapped actions on the system.profile collection for a database.

The built-in roles dbAdmin and dbAdminAnyDatabase provide the additional privileges.

If running mongorestore with --oplogReplay, the restore role is insufficient to replay the oplog. To replay the oplog, create a user-defined role that has anyAction on anyResource and grant only to users who must run mongorestore with --oplogReplay.

Provides the following action on the cluster as a whole:

getParameter
Provides the following actions on all non-system collections:

bypassDocumentValidation
changeCustomData
changePassword
collMod
convertToCapped
createCollection
createIndex
createRole
createUser
dropCollection
dropRole
dropUser
grantRole
insert
revokeRole
viewRole
viewUser
Provides the following actions on system.js collection:

bypassDocumentValidation
collMod
createCollection
createIndex
dropCollection
insert
Provides the following action on anyResource:

listCollections
Provides the find action on all the system.namespaces collections in the cluster.

Provides the following actions on all non-system collections on the config and the local databases:

bypassDocumentValidation
collMod
createCollection
createIndex
dropCollection
insert
Provides the following actions on admin.system.version

bypassDocumentValidation
collMod
createCollection
createIndex
dropCollection
find
insert
Provides the following action on admin.system.roles

createIndex
Provides the following actions on admin.system.users and legacy system.users collections:

bypassDocumentValidation
collMod
createCollection
createIndex
dropCollection
find
insert
remove
update
Although, restore includes the ability to modify the documents in the admin.system.users collection using normal modification operations, only modify these data using the user management methods.

All-Database Roles (所有數據庫角色)

以下角色在管理數據庫上可用,並提供適用於除本地和配置之外的所有數據庫的權限:

readAnyDatabase (讀取任意數據庫)

提供與在除本地和配置之外的所有數據庫上讀取相同的read權限。該角色還提供了集羣上的listDatabases 操作。

在3.4版本中進行了更改:在3.4之前,readAnyDatabase 包括localconfig數據庫。要在local數據庫上提供read權限,請在admin數據庫中創建一個在local數據庫中具有read角色的用戶。

另請參閱clusterManagerclusterMonitor角色以訪問配置和本地數據庫。

readWriteAnyDatabase (讀寫任意數據庫)

在除localconfig之外的所有數據庫上提供與readWrite相同的讀寫權限。該角色還提供了集羣上的listDatabases操作。

在3.4版本中進行了更改:在3.4之前,readWriteAnyDatabase包括localconfig數據庫。要在local數據庫上提供readWrite權限,請在admin數據庫中創建一個在local數據庫中具有readWrite角色的用戶。

另請參閱clusterManagerclusterMonitor角色以訪問localconfig數據庫

userAdminAnyDatabase (用戶管理數據庫)

在除localconfig之外的所有數據庫上,提供與userAdmin相同的用戶管理操作訪問權限。

UserAdminAnyDatabase還提供羣集上的以下權限操作:

  • authSchemaUpgrade
  • invalidateUserCache
  • listDatabases

該角色還提供以下權限操作:admin數據庫上的system.userssystem.roles集合,以及2.6之前版本的mongodb中的legacy system.users集合:

  • collStats
  • dbHash
  • dbStats
  • find
  • killCursors
  • planCacheRead

在版本2.6.4中進行了更改:userAdminAnyDatabaseadmin.system.usersadmin.system.roles集合中添加了以下權限操作:

  • createIndex
  • dropIndex

userAdminAnyDatabase角色不限制用戶可以授予的權限。因此,userAdminAnyDatabase用戶可以授予自己超過當前權限的權限,甚至可以授予自己所有權限,即使角色沒有明確授權超出用戶管理範圍的權限。此角色實際上是MongoDB系統superuser

在版本3.4中更改:UserAdminAnyDatabase不再適用於localconfig數據庫。

另請參閱clusterManagerclusterMonitor角色以訪問localconfig數據庫。

dbAdminAnyDatabase

在除localconfig之外的所有數據庫上提供與dbAdmin相同的權限。該角色還提供了集羣上的listDatabases 操作。

在3.4版中進行了更改:在3.4版之前,dbAdminAnyDatabase 包括localconfig數據庫。要在local數據庫上提供dbAdmin特權,請在local數據庫中創建具有dbAdmin角色的admin數據庫中的用戶。

另請參閱clusterManagerclusterMonitor角色以訪問localconfig數據庫。

Superuser Roles (超級用戶角色)

多個角色提供間接或直接的系統範圍超級用戶訪問。

以下角色提供了在任何數據庫上爲任何用戶分配任何權限的能力,這意味着具有這些角色之一的用戶可以在任何數據庫上爲自己分配任何權限:

以下角色對所有資源提供完全權限:
dbowner角色,當作用域爲管理數據庫時
當作用域爲admin數據庫時,userAdmin角色
userAdminAnyDatabase角色

以下角色對所有資源提供完全權限:

root

提供對readwriteanydatabasedbadminnanydatabaseuseradminanydatabaseclusteradminrestorebackup的操作和所有資源的訪問。

在版本3.4中更改:root角色包括來自backup角色的權限。

在版本3.0.7中更改:rootsystem上具有validate權限操作。收藏。以前,root不包括對以系統開始的集合的任何訪問。前綴不是system.indexessystem.namespaces

根角色包括還原角色的權限。的操作和所有資源的訪問。

Internal Role (內部角色)

__system

MongoDB將此角色分配給表示集羣成員的用戶對象,例如複製集成員和mongos實例。該角色使其持有者有權對數據庫中的任何對象採取任何操作。

除特殊情況外,不要將此角色分配給表示應用程序或人員管理員的用戶對象。

如果需要訪問所有資源上的所有操作,例如運行applyops命令,請不要分配此角色。相反,創建一個用戶定義的角色,授予對任何資源的任何操作,並確保只有需要訪問這些操作的用戶才具有此訪問權。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章