oracle限制某IP的连接数

在数据库管理中,会出现限制某个IP访问数据库的连接数或某个用户访问数据库的连接数的需求。

对于用户访问数据库的连接数限制,我们可以从数据库的profile上着手,使用profile的特性实现该需求。

对于IP访问数据库的连接数限制,从数据库上可以使用logon on database触发器来实现。

每一次新会话登录,都将IP记录在vrsession的client_info中,然后count出所有符合条件的会话数目,如果超过了,就直接断开会话连接。

但这个会话连接数据库如果限制了,也只能对非dba角色的用户生效。dba用户只会在alert.log中写一个警告信息而已。

(miki西游 @mikixiyou 原文链接: http://mikixiyou.iteye.com/blog/1705838 )

实现该功能的触发器代码如下:

create or replace trigger logon_audit
  after logon on database
declare
  /**********************************************************************************  
  NAME:       logon_audit   
  PURPOSE:    限制某个IP到数据库实例的连接数。如果超过限制会出错或报警
    
  NOTES:   1、使用sys提交到数据库  
           2、使用logon on database触发器实现  
    
  **********************************************************************************/
  /*按照规划,定义number,string,date三种类型变量名称*/

  i_sessions         number;
  i_sessions_limited number := 30;
  str_userip         varchar2(15) := '192.168.15.148';
  except_ip_limited exception;
begin

  dbms_application_info.set_client_info(sys_context('userenv',
                                                    'ip_address'));
  select count(*)
    into i_sessions
    from v$session
   where client_info = str_userip;

  if (i_sessions > i_sessions_limited) then
    raise except_ip_limited;
  end if;

exception
  when except_ip_limited then
    raise_application_error(-20003, 'ip:' || str_userip || ' 连接数受限!');
  
end logon_audit;

猜你喜欢

转载自mikixiyou.iteye.com/blog/1705838
今日推荐