oracle 添加登陆数据库触发器--记录IP 地址
----触发器--- ---创建中间插入的表
create table session_history tablespace bap_data as (select sid,username,program,machine,'000.000.000.000'ipadd,sysdate moditime from v$session where 0=1);
---创建触发器---只要登陆数据库就触发
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;
---查询登陆信息
select * from session_history q where q.username not in ('SYS')
------------直接在数据库SYS用户层面修改----(实际操作是步骤3)
-- 1. 利用triger
begin
dbms_application_info.set_client_info(sys_context( 'userenv', 'ip_address' ) );
end;
2. 利用 DBMS_SESSION 过程包.
BEGIN
DBMS_SESSION.set_identifier(SYS_CONTEXT('USERENV', 'IP_ADDRESS'));
END;
3.我们可以通过触发器
create or replace trigger on_logon_trigger
after logon on database
begin
dbms_application_info.set_client_info(sys_context( 'userenv', 'ip_address' ) );
end;
/
4.验证 这样当客户端登陆后,在v$session的client_info列会记录其相应的IP信息。
这里才查询v$session ,你会发现 v$session 多了一列 记录客户端的IP.
select client_info from v$session;
select count(client_info) from v$session;
set pagesize 1000 line 1000 select a.description,a.trigger_body from dba_triggers a where a.owner='SYS' AND A.status='ENABLED';
select count(1) from dba_triggers a where a.owner='SYS' AND A.status='ENABLED';
select client_info from v$session where client_info is not null ;
----11G 直接添加就行;12C 需要给 CDB 和每个PDB 单独添加