search

大家对trigger可能比较熟悉,但Oracle还有一个叫FGA的功能,它的作用和trigger类似,但功能更强大.它的全称是Fine-Grained Audit ,是

大家对trigger可能比较熟悉,但Oracle还有一个叫FGA的功能,它的作用和trigger类似,但功能更强大.它的全称是Fine-Grained Audit ,是Audit的一种特殊方式.使用FGA只要调用Oracle的包DBMS_FGA.ADD_POLICY创建一些policy(审计策略)就行.每个policy只能针对一个表或视图.建好策略后所以对表或视图的DML操作(select,insert,update,delete都可以记录到,当然也可以添加一些筛选条件只监测某些特殊的操作.

补充:所谓审计就是记录你的任意操作,假如你的操作(执行DML语句)符合指定的条件,则你执行的sql语句,将被记录到sys用户下的一些表中,还会将你的其他信息,比如执行时间,用户名,通过什么工具,机器名等.FGA在oracle 9i中就有了,但在9i中只能审计select语句.从10g开始才能审计所有的DML操作

FGA与Triger的明显区别:

1.FGA使用自治的事务,即使DML操作被rollback了,它仍然照样执行不会rollback.而trigger是会被rollback的.

2.做更新操作时trigger可以记录更新前的旧值和更新后的新值,而FGA是不记录旧值.

1.包DBMS_FGA.ADD_POLICY的用法

创建审计策略的语法

DBMS_FGA.ADD_POLICY (

object_schema VARCHAR2, --schema的名字,表或视图的拥有者

object_name VARCHAR2, --对象名,表或视图的名字

policy_name VARCHAR2, --审计策略名字,它和数据库中其他对象一样,需要有一个不重复,唯一的名字

audit_condition VARCHAR2, --筛选条件比如可以选择哪些符合条件的操作被记录

audit_column VARCHAR2, --表中的某一列,可以只记录对表中某一列的操作.如果不指定表示审计所有的列

handler_schema VARCHAR2, --是下面的handler_module的拥有者,其实也只能是创建policy的用户,而上面的object_schema可以是任意用户

handler_module VARCHAR2,--可以是一个一个存储过程或函数,但监测到任何一条符合条件的操作时执行它.

enable BOOLEAN, --true 或false表示policy是开启或关闭状态,如果是false表示不进行审计

statement_types VARCHAR2, --表示哪些操作将被审计,可以填上select,insert,update,delete中的一个或几个

audit_trail BINARY_INTEGER IN DEFAULT,--有参数db,xml表示审计到的信息保存到数据库中或是以xml文件形式保存到磁盘上

audit_column_opts BINARY_INTEGER IN DEFAULT); --这个选项其实只有在audt_column中指定了某列时才起作用.它有any_columns,all_columns两个选项假如表中有eno,ename两列,并在audit_column中指定了这两列,那么选any_columns表示只要操作其中的任意一列都将被记录,而这里指定all_columns的话是说只有一个sql语句同时操作了这两列才被记录

 

举例,假如创建表:create table temp(eno int,ename varchar2(30));针对这个表创建policy

每个策略只能针对一个表或视图,而一个表或视图可能对应多个策略.当表被删除时策略也随之默认被删除

BEGIN
SYS.DBMS_FGA.ADD_POLICY (
object_schema => 'ARWEN'
,object_name => 'TEMP'
,policy_name => 'FGA_TEMP'

,audit_condition => NULL
,audit_column => eno,ename
,handler_schema => null
,handler_module => null

,enable => TRUE
,statement_types =>'SELECT,INSERT,UPDATE,DELETE'
,audit_trail => SYS.DBMS_FGA.DB+SYS.DBMS_FGA.EXTENDED

--DBMS_FGA.DB表示记录将被保存到数据库中,DBMS_FGA.EXTENDED表示如果sql语句中带有绑定变量也会被记录下来.

--如果是这样选audit_trail => SYS.DBMS_FGA.DB表示不会记录绑定变量

--SYS.DBMS_FGA.DB+SYS.DBMS_FGA.EXTENDED改成SYS.DBMS_FGA.XML+SYS.DBMS_FGA.EXTENDED表示记录保存成xml文件

--xml文件所在目录可以通过SHOW PARAMETER AUDIT_FILE_DEST查看,如果要更改目录ALTER SYSTEM SET AUDIT_FILE_DEST = directory_path DEFERRED;

,audit_column_opts => SYS.DBMS_FGA.ALL_COLUMNS)
END;

 

查看创建好的policy对象和符合审计条件时被记录的操作

我们可以通过select * from dba_objects查看table,view等对象,类似的policy创建好后我们可以通过SELECT * FROM DBA_AUDIT_POLICIES来查看.

如果我们对表temp执行了DML操作,那些信息将会被操作到sys用户下的表中,Select* from sys.dba_fga_audit_trail可以查找到.(注意这只有前面设置将记录信息

保存到数据库才能这样查,如果保存到xml文件中可以Select *from V$XML_AUDIT_TRAIL)

 

2.删除审计策略,假如删除上面创建的策略

begin

SYS.DBMS_FGA.DROP_POLICY (

object_schema => 'ARWEN'

,object_name => 'TEMP'

,policy_name => 'FGA_TEMP'

);

end;

3.使用handler_module

 

假如你想在审计到某些操作时还进行进一步的处理,比如把信息写到自己创建的日志中,或者发送邮件通知相关人员.就要在创建policy时使用handler_module的功能,指定一个存储过程去做相应的处理.假如我创建一个存储过程temp_handler

CREATE OR REPLACE PROCEDURE temp_handler

( v_object_schema VARCHAR2

, v_object_name VARCHAR2

, v_policy_name VARCHAR2

)

IS

v_temp varchar2(30);

begin

null;

end temp_handler;

--这里的存储过程有点特殊,它必须带v_object_schema VARCHAR2, v_object_name VARCHAR2, v_policy_name VARCHAR2这三个参数才行,如果直接写一个一般的存储过程就会出错的.在policy中调用存储过程就这样 写

 

BEGIN
SYS.DBMS_FGA.ADD_POLICY (
object_schema => 'ARWEN'
,object_name => 'TEMP'
,policy_name => 'FGA_TEMP'

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Reduce the use of MySQL memory in DockerReduce the use of MySQL memory in DockerMar 04, 2025 pm 03:52 PM

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

How to solve the problem of mysql cannot open shared libraryHow to solve the problem of mysql cannot open shared libraryMar 04, 2025 pm 04:01 PM

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

How do you alter a table in MySQL using the ALTER TABLE statement?How do you alter a table in MySQL using the ALTER TABLE statement?Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Run MySQl in Linux (with/without podman container with phpmyadmin)Run MySQl in Linux (with/without podman container with phpmyadmin)Mar 04, 2025 pm 03:54 PM

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

What is SQLite? Comprehensive overviewWhat is SQLite? Comprehensive overviewMar 04, 2025 pm 03:55 PM

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

How do I configure SSL/TLS encryption for MySQL connections?How do I configure SSL/TLS encryption for MySQL connections?Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Running multiple MySQL versions on MacOS: A step-by-step guideRunning multiple MySQL versions on MacOS: A step-by-step guideMar 04, 2025 pm 03:49 PM

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),