---
title: "Oracle 日志监控"
author: "fsclh"
date: "2007-05-24T22:14:41Z"
tags: ["Oracle", "SQL", "Unix"]
slug: "oracle"
draft: false
lastmod: "2007-05-24T22:14:41Z"
id: 82694
url: "https://fsclh.iteye.com/blog/82694"
---

1\. 修改用户 sys/system/scott/sys\_man(oem使用)的密码;

2\. 进入dba studio看看系统有多少个用户，有没有可疑用户

3\. 不用随便给普通用户赋予DBA角色，普通用户赋予CONNECT或RESOURCE角色或者根据需要赋予相关的权限。

4\. 8i以上使用LogMiner工具，关于此工具的使用查阅精华区或faqs

5\. 经常查看Alert&lt;sid&gt;&lt;/sid&gt;.log文件，该文件路径: $ORACLE\_HOME/admin/sid/bdump.

    用LogMiner包看一下，可以看到他是用的那个用户修改的那些表修改了什么。  
    Example of Using LogMiner  
    Assume that the data dictionary extract was taken to a flat file named  
    '/usr/oracle/dbs/dict.txt'  
    The DBA first specifies five redo logs to be analyzed  
    execute dbms\_logmnr.add\_logfile (filename =&gt; '/usr/oracle/dbs/arch123.dbf',  
    options =&gt; dbms\_logmnr.NEW);  
    -- identifying this file starts a new list of files to analyze  
execute dbms\_logmnr.add\_logfile (filename =&gt; '/usr/oracle/dbs/arch124.dbf',  
options =&gt; dbms\_logmnr.ADDFILE);  
execute dbms\_logmnr.add\_logfile (filename =&gt; '/usr/oracle/dbs/arch125.dbf',  
options =&gt; dbms\_logmnr.ADDFILE);  
execute dbms\_logmnr.add\_logfile (filename =&gt; '/usr/oracle/dbs/arch126.dbf',  
options =&gt; dbms\_logmnr.ADDFILE);  
execute dbms\_logmnr.add\_logfile (filename =&gt; '/usr/oracle/dbs/arch127.dbf',  
options =&gt; dbms\_logmnr.ADDFILE);  
The DBA then specifies the location of the dictionary with,  
execute dbms\_logmnr.start\_logmnr( dictfilename =&gt; '/usr/oracle/dbs/dict.txt' );  
The DBA is now ready to issue a select against the v$logmnr\_contents view  
select operation, sql\_redo, sql\_undo  
from v$logmnr\_contents  
where seg\_owner = 'SCOTT' and seg\_name = 'ORDERS' and  
operation = 'DELETE' and username = 'RON';

listner.log 里面会有你想要的证据

查listener.log只能查到从哪里登录，查不到输入什么sql语句。  
保密有一点很关键但又很容易被忽略的，很多人都是直接输入  
sqlplus [username/password@connstring](mailto:username/password@connstring)   （一般服务器都安装在unix系统）  
登录的，如此一来，别人只要用ps -ef|grep sqlplus就可以看到你的用户名和密码了。  
应该用  
sqlplus  然后再输入用户名和密码。  
也可以先定义一个变量  
$STRI=username/password;export $STRI  
sqlplus [$STRI@connstring](mailto:$STRI@connstring)  
这样别人就看不到你的密码了。

注意查看system的视图v$sql，也许会有收获。

当然用审计功能 ：  
方法一:  
用以下的方式可以监控登入注销的用户:  
创建如下的两张表:  
create table login\_log   -- 登入注销信息表  
(  
    session\_id int not null, -- sessionid  
    login\_on\_time  date,  -- 登入时间  
    login\_off\_time  date,  -- 注销时间  
    user\_in\_db varchar2(30), -- 登入的db user  
    machine    varchar2(20),    -- 机器名  
    ip\_address varchar2(20), -- ip地址  
    run\_program varchar2(20)    -- 以何程序登入  
);

create table allow\_user   -- 网域用户表  
(  
    ip\_address varchar2(20),  -- ip地址  
    login\_user\_name nvarchar2(20)   -- 操作者姓名  
);

创建如下的两个触发器:  
create or replace trigger login\_on\_info  -- 纪录登入信息的触发器  
after logon on database  
Begin  
    insert into login\_log(session\_id,login\_on\_time,login\_off\_time,user\_in\_db,machine,ip\_address,run\_program)  
    select AUDSID,sysdate,null,sys.login\_user,machine,SYS\_CONTEXT('USERENV','IP\_ADDRESS'),program  
    from v$session where AUDSID = USERENV('SESSIONID');  --当前SESSION  
END;

create or replace trigger login\_off\_info -- 纪录注销信息的触发器  
before logoff on database  
Begin  
 update login\_log set  login\_off\_time = sysdate  
 where session\_id = USERENV('SESSIONID'); --当前SESSION  
exception  
    when others then  
     null;  
END;

查询语句:  
column a format a20  
column b format a20  
column IP\_ADDRESS format a30  
select to\_char(login\_on\_time,'yyyy-mm-dd hh24:mi:ss') as login,to\_char(login\_off\_time,'yyyy-mm-dd hh24:mi:ss') as logout,ip\_address from login\_log;

方法二:  
用如下的方式可以审计执行drop动作的事件:  
/\**  
 * drop语句的审计日志表  
 \*/  
create table drop\_log  
(  
    session\_id int not null,  -- sessionid  
    drop\_time  date,    -- drop的时间  
    ip\_address varchar2(20),  -- ip地址  
    object\_owner varchar2(30),  -- 对象的拥有者  
    object\_name varchar2(30),  -- 对象名称  
    object\_type varchar2(20),  -- 对象类型  
    drop\_by\_user varchar2(30) -- 执行drop语句的用户  
);

create or replace trigger drop\_info  
after drop on mfg0513user.schema   -- 在mfg0513user用户上创建审计drop的触发器  
begin  
    insert into drop\_log  
       (session\_id,  
       drop\_time,  
       ip\_address,  
       object\_owner,  
       object\_name,  
       object\_type,  
       drop\_by\_user)  
     values(USERENV('SESSIONID'),  
       sysdate,  
       SYS\_CONTEXT('USERENV','IP\_ADDRESS'),  
       sys.dictionary\_obj\_owner,  
       sys.dictionary\_obj\_name,  
       sys.dictionary\_obj\_type,  
       sys.login\_user);     
end;

To collect auditing results, you must set the initialization parameter AUDIT\_TRAIL to DB.  
To choose auditing for statements issued by the users aaa that query or update a table or view, issue the following statement:  
AUDIT SELECT TABLE, UPDATE TABLE  
   BY aaa;  
To obtaining information:  
   --DBA\_AUDIT\_TRAIL  
   --DBA\_AUDIT\_EXISTS  
   --DBA\_AUDIT\_OBJECT  
   --DBA\_AUDIT\_SESSION  
   --DBA\_AUDIT\_STATEMENT