Docs 主页 → 开发应用程序 → MongoDB Manual
setAuditConfig
重要
在版本 7.1 中已弃用:请改用 auditConfig
集群参数。
定义
setAuditConfig
版本 5.0 中的新增功能。
setAuditConfig
是一条管理命令,用于在运行时为mongod
和mongos
服务器实例设置新的审核配置。使用
db.adminCommand( { command } )
方法对admin
数据库运行setAuditConfig
。
语法
该命令具有以下语法:
db.adminCommand( { setAuditConfig: 1, filter: <Filter Document>, auditAuthorizationSuccess: <Boolean> } )
命令字段
该命令具有以下字段:
字段 | 类型 | 说明 |
---|---|---|
setAuditConfig | 整型 | |
filter | 文档 | |
auditAuthorizationSuccess | 布尔 | 记录全部或仅记录失败的访问授权 |
行为
启用审核以在运行时使用setAuditConfig
。
auditAuthorizationSuccess
启用对authCheck操作授权成功的审核。该参数值必须为true
才能审核读取和写入操作。但是,当auditAuthorizationSuccess
为false
时,审核对性能的影响较小,因为审核系统仅记录授权失败。
配置更新通过 oplog mongod
机制分发,这意味着 节点上的更新会非常快速地分发到从节点。 节点上有不同的分发机制。mongos
mongos
poll
节点必须定期 主服务器以进行配置更新。在分片setAuditConfig
轮询 主服务器以获取更新的配置详细信息之前,如果您在主服务器上运行 并在分片上运行getAuditConfig
{ },则可能会由于轮询延迟而看到过时数据。
举例
在这些示例中,审核消息已重新格式化。它们在日志文件中显示在一行上。
审核集合的创建和删除
在创建或删除集合时启用审核。
db.admin.runCommand( { setAuditConfig: 1, filter: { atype: { $in: [ "createCollection", "dropCollection" ] } }, auditAuthorizationSuccess: false } )
在sales
数据库中创建inventory
集合时,审核系统将记录如下消息:
{ "atype" : "createCollection", "ts" : { "$date" : "2021-08-09T13:45:05.372+00:00" }, "uuid" : { "$binary" : "RKU/YLizS6K9se2GUU7ZVQ==", "$type" : "04" }, "local" : { "ip" : "127.0.0.1", "port" : 27502 }, "remote" : { "ip" : "127.0.0.1", "port" : 51918 }, "users" : [], "roles" : [], "param" : { "ns" : "sales.inventory" }, "result" : 0 }
从sales
数据库删除inventory
集合时,审核系统将记录如下消息:
{ "atype" : "dropCollection", "ts" : { "$date" : "2021-08-09T13:45:00.661+00:00" }, "uuid" : { "$binary" : "0gle4/pSQli+LUcz43ykag==", "$type" : "04" }, "local" : { "ip" : "127.0.0.1", "port" : 27502 }, "remote" : { "ip" : "127.0.0.1", "port" : 51928 }, "users" : [], "roles" : [], "param" : { "ns" : "sales.inventory" }, "result" : 0 }
审核文档交互
将auditAuthorizationSuccess
设置为true
并创建一个筛选器,其中包含审核读写操作感兴趣的操作。
db.admin.runCommand( { setAuditConfig: 1, filter: { atype: "authCheck", "param.command": { $in: [ "find", "insert", "delete", "update", "findandmodify" ] } }, auditAuthorizationSuccess: true } )
使用find
命令在sales
数据库中搜索inventory
集合,创建如下审核日志条目:
{ "atype" : "authCheck", "ts" : { "$date" : "2021-08-09T15:28:10.788+00:00" }, "uuid" : { "$binary" : "ngwRt5CRTZqgE4TsfleoqQ==", "$type" : "04" }, "local" : { "ip" : "127.0.0.1", "port" : 27502 }, "remote" : { "ip" : "127.0.0.1", "port" : 51930 }, "users" : [], "roles" : [], "param" : { "command" : "find", "ns" : "sales.inventory", "args" : { "find" : "inventory", "filter" : { "widget" : 1 }, "lsid" : { "id" : { "$binary" : "FNWNxiitQ8GHKrHx8eJSbg==", "$type" : "04" } }, "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1628521381, "i" : 1 } }, "signature" : { "hash" : { "$binary" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "$type" : "00" }, "keyId" : { "$numberLong" : "0" } } }, "$db" : "sales" } }, "result" : 0 }