当前位置 博文首页 > LuciferLiu_DBA:Oracle记录登录用户IP(触发器)

    LuciferLiu_DBA:Oracle记录登录用户IP(触发器)

    作者:[db:作者] 时间:2021-06-15 21:41

    --创建单独表空间存放记录
    create tablespace setb;
    
    --通过ctas从v$session创建session历史记录表指定表空间setb
    create table session_history tablespace setb as (select sid,username,program,machine,'000.000.000.000'ipadd,sysdate moditime from v$session where 0=1);
    
    --创建触发器,当有用户登录时,将记录插入session历史记录表
    CREATE or replace trigger on_logon_trigger after logon
    ON database begin
    INSERT INTO session_history
    SELECT  sid
           ,username
           ,program
           ,machine
           ,sys_context('userenv','ip_address')
           ,sysdate
    FROM v$session
    WHERE audsid = userenv('sessionid'); end;
    /
    
    --查询非SYS用户的登录记录
    alter session set nls_date_format = 'yyyy-mm-dd hh24:mi:ss';
    select * from  session_history q where q.username not in ('SYS');

    参考自:oracle 添加登陆数据库触发器--记录IP 地址

    下一篇:没有了