Oracle經常使用SQL命令 使用觸發器實現記錄oracle用戶登陸失敗信息到alert.log日誌文件

1. Oracle查看錶空間使用率(包含臨時表空間)  

set pagesize 500
set lin 120
col TABLESPACE_NAME for a30
col SUM_SPACE(M) for a15
col SUM_BLOCKS for a35
col USED_SPACE(M) for a15
col USED_RATE(%) for a15
col FREE_SPACE(M) for a15
SELECT D.TABLESPACE_NAME,  
       SPACE || 'M' "SUM_SPACE(M)",  
       SPACE - NVL (FREE_SPACE, 0) || 'M' "USED_SPACE(M)",  
       ROUND ( (1 - NVL (FREE_SPACE, 0) / SPACE) * 100, 2) || '%'  
          "USED_RATE(%)",  
       FREE_SPACE || 'M' "FREE_SPACE(M)"  
  FROM (  SELECT TABLESPACE_NAME,  
                 ROUND (SUM (BYTES) / (1024 * 1024), 2) SPACE,  
                 SUM (BLOCKS) BLOCKS  
            FROM DBA_DATA_FILES  
        GROUP BY TABLESPACE_NAME) D,  
       (  SELECT TABLESPACE_NAME,  
                 ROUND (SUM (BYTES) / (1024 * 1024), 2) FREE_SPACE  
            FROM DBA_FREE_SPACE  
        GROUP BY TABLESPACE_NAME) F  
 WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME(+)  
UNION ALL                                                          
SELECT D.TABLESPACE_NAME,  
       SPACE || 'M' "SUM_SPACE(M)",  
       USED_SPACE || 'M' "USED_SPACE(M)",  
       ROUND (NVL (USED_SPACE, 0) / SPACE * 100, 2) || '%' "USED_RATE(%)",  
       NVL (FREE_SPACE, 0) || 'M' "FREE_SPACE(M)"  
  FROM (  SELECT TABLESPACE_NAME,  
                 ROUND (SUM (BYTES) / (1024 * 1024), 2) SPACE,  
                 SUM (BLOCKS) BLOCKS  
            FROM DBA_TEMP_FILES  
        GROUP BY TABLESPACE_NAME) D,  
       (  SELECT TABLESPACE_NAME,  
                 ROUND (SUM (BYTES_USED) / (1024 * 1024), 2) USED_SPACE,  
                 ROUND (SUM (BYTES_FREE) / (1024 * 1024), 2) FREE_SPACE  
            FROM V$TEMP_SPACE_HEADER  
        GROUP BY TABLESPACE_NAME) F  
 WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME(+)  
ORDER BY 1;  

  

2. 查看錶空間的名字和所屬文件

 

set line 200;
col tablespace_name format a30;
col file_id format 999;
col file_name format a70;
col total_space format 999999;
 
select tablespace_name, file_id, file_name,
round(bytes/(1024*1024),0) total_space
from dba_data_files
order by tablespace_name;
 
##查看用戶和默認的表空間
select username,default_tablespace from dba_users order by 1;

##查看物理文件使用率
select   b.file_name 物理文件名,
         b.tablespace_name 表空間,
         b.bytes / 1024 / 1024 大小M,
         (b.bytes - sum(nvl(a.bytes, 0))) / 1024 / 1024 已使用M,
         substr((b.bytes - sum(nvl(a.bytes, 0))) / (b.bytes) * 100, 1, 5) 利用率   from dba_free_space a,
       dba_data_files b   where a.file_id = b.file_id   group by b.tablespace_name,
       b.file_name,
       b.bytes   order by b.tablespace_name;

 

 

 

 3. 將一個用戶的全部對象受權給另外一個用戶:

--首先是CPR帳號
--受權表上的讀寫權限
select 'grant all on ' || owner || '.' || table_name || ' to hisuser;'
  from dba_tables
 where owner = 'CPR';

--受權視圖上的讀寫權限
select 'grant all on ' || owner || '.' || view_name || ' to hisuser;'
  from dba_views
 where owner = 'CPR';

--受權函數和存儲過程的讀寫權限
select 'grant execute on ' || owner || '.' || name || ' to hisuser;'
  from dba_source
 where owner = 'CPR'
   and type in ('PROCEDURE',
                'FUNCTION',
                'PACKAGE',
                'PACKAGE BODY',
                'TYPE BODY',
                'TRIGGER',
                'TYPE');

--受權序列的讀寫權限
select 'grant all on ' || sequence_owner || '.' || sequence_name ||
       ' to hisuser;'
  from dba_sequences
 where sequence_owner = 'CPR';

--建立同義詞
select 'create or replace public synonym ' || synonym_name || ' for ' ||
       table_owner || '.' || table_name || ' ;'
  from dba_synonyms
 where table_owner = 'CPR';
select 'create or replace public synonym ' || view_name || ' for ' || owner || '.' ||
       view_name || ' ;'
  from dba_views
 where owner = 'CPR'
   and (owner NOT LIKE '%$%' OR view_name NOT LIKE '%$%');

--而後是system帳號
--受權表上的讀寫權限
select 'grant all on ' || owner || '.' || table_name || ' to hisuser;'
  from dba_tables
 where owner = 'SYSTEM'
   and table_name NOT LIKE '%$%';

--受權視圖上的讀寫權限
select 'grant all on ' || owner || '.' || view_name || ' to hisuser;'
  from dba_views
 where owner = 'SYS';

--受權函數和存儲過程的讀寫權限
select DISTINCT 'grant execute on ' || owner || '.' || name ||
                ' to hisuser;'
  from dba_source
 where owner = 'SYS'
   and type in ('PROCEDURE',
                'FUNCTION',
                'PACKAGE',
                'PACKAGE BODY',
                'TYPE BODY',
                'TRIGGER',
                'TYPE')
   AND name NOT LIKE '%$%';

--受權序列的讀寫權限
select 'grant all on ' || sequence_owner || '.' || sequence_name ||
       ' to hisuser;'
  from dba_sequences
 where sequence_owner = 'SYSTEM'
   AND sequence_name NOT LIKE '%$%';

--建立同義詞
select 'create or replace public synonym ' || synonym_name || ' for ' ||
       table_owner || '.' || table_name || ' ;'
  from dba_synonyms
 where table_owner = 'SYS'
   and synonym_name NOT LIKE '%$%';

select 'create or replace public synonym ' || view_name || ' for ' || owner || '.' ||
       view_name || ' ;'
  from dba_views
 where owner = 'SYS'
   and (owner NOT LIKE '%$%' OR view_name NOT LIKE '%$%');

 4.擴大表空間

--1.增長數據文件
ALTER TABLESPACE game ADD DATAFILE '/oracle/oradata/db/GAME02.dbf' SIZE 1000M;

--2.手動增長數據文件尺寸
ALTER DATABASE DATAFILE '/oracle/oradata/db/GAME.dbf' RESIZE 4000M;

--RAC自動管理的表空間增長數據文件的方法:
數據文件:
alter tablespace TEMP add datafile size 30g;
臨時文件:
alter tablespace temp add tempfile size 30g;

5. 經過RMAN刪除過時的歸檔日誌

DELETE  force noprompt ARCHIVELOG ALL COMPLETED BEFORE 'SYSDATE-5';

 6. 密碼默認180天過時修改:

--1
SELECT  *  FROM  dba_profiles  WHERE  profile='DEFAULT'  AND  resource_name='PASSWORD_LIFE_TIME';
--180 天到期
 
更改密碼爲無期
--2
ALTER  PROFILE  DEFAULT  LIMIT  PASSWORD_LIFE_TIME  UNLIMITED;

7.ASM磁盤剩餘空間查看:

select group_number,name,total_mb,free_mb from v$asm_diskgroup;

8.歷史歸檔日誌容量查看:

select trunc(COMPLETION_TIME) TIME, SUM(BLOCKS * BLOCK_SIZE)/1024/1024 SIZE_MB from V$ARCHIVED_LOG group by trunc (COMPLETION_TIME) order by 1;

09.驗證RMAN備份信息:

SELECT START_TIME,
      END_TIME,
      OUTPUT_DEVICE_TYPE,
      STATUS,
      ELAPSED_SECONDS,
      COMPRESSION_RATIO,
      INPUT_BYTES_DISPLAY,
      OUTPUT_BYTES_DISPLAY
FROM V$RMAN_BACKUP_JOB_DETAILS
ORDER BY START_TIME DESC ;

10. Oracle 殺掉當前用戶的進程

select ' alter system kill session '''|| sid ||','||serial# || ''';' from v$SESSION where username='UserName';

 

11. Oracle歸檔日誌清理腳本 

##腳本方法1:
#!/bin/bash
if [ -f ~/.bash_profile ]; then
    . ~/.bash_profile
fi

#set env
echo "Oracle home:"$ORACLE_HOME
echo "Oracle SID:"$ORACLE_SID

$ORACLE_HOME/bin/rman target sys/SysPassword@Oracle_SID log=/tmp/rman.log <<EOF
crosscheck archivelog all;
delete noprompt expired archivelog all;
delete noprompt archivelog all completed before 'sysdate - 5';
exit;
EOF


####腳本方法2:
#!/bin/bash
if [ -f ~/.bash_profile ]; then
    . ~/.bash_profile
fi

#set env
echo "Oracle home:"$ORACLE_HOME
echo "Oracle SID:"$ORACLE_SID

$ORACLE_HOME/bin/rman log=/tmp/rman.log <<EOF       
connect target sys/SysPassword@Oracle_SID
run{
crosscheck archivelog all;    
delete noprompt expired archivelog all;    
delete noprompt archivelog all completed before 'sysdate - 5';    
}  


##設置定時任務:
crontab -l
0 1 * * * /home/oracle/scripts/clear_archivelog.sh > /tmp/clear_archivelog.log

##備註:
1. 必須在Oracle用戶下編輯執行腳本
2. 必須給/home/oracle/scripts/clear_archivelog.sh執行權限
3. 必須驗證

 

 12. Oracle鎖管理

 

##1. 肯定鎖進程的sid
SELECT
    t2.username,
    t2.sid,
    t2.serial#,
    t2.logon_time
FROM
    v$locked_object t1,
    v$session t2
WHERE
    t1.session_id = t2.sid
ORDER BY
    t2.logon_time;
    
##2. 經過sid得到它的sql,看是哪一條sql致使鎖的佔用    
SELECT
    sql_text
FROM
    v$sql t1,
    v$session t2
WHERE
    t1.address = t2.sql_address
    AND t2.sid = & sid; --&sid 就是上一條sql中查到的sid

##3.1 若有記錄則表示有lock,記錄下SID和serial# ,執行下面的sql,便可解除鎖    
ALTER system kill SESSION 'SID,serial#';


##3.2 批量刪除腳本:
SELECT
    'alter system kill session ''' || sid || ',' || serial# || ''';' "deadlock"
FROM
    v$session
WHERE
    sid IN (
    SELECT
        sid
    FROM
        v$lock
    WHERE
        block = 1);
##注意:應當注意對於 sid 在 100 如下的應當謹慎,可能該進程對應某個application,如對應某個事務,能夠 kill.

 

13. 長時間運行SQL語句處理:

##Oracle中查詢某個sql是哪一個用戶發出可用以下語句:
SELECT
    a.sql_text,
    b.username
FROM
    v$sql a,
    v$session b
WHERE
    a.hash_value = b.sql_hash_value;
    
    

##查詢執行時間長的SQL語句
SELECT
    sid,
    serial#,
    sql_text,
    executions
FROM
    v$sql
JOIN v$session ON
    v$sql.sql_id = v$session.sql_id
WHERE
    cpu_time > 20000;
    
##殺掉SQL語句:
alter system kill session 'sid,serial#';


##本身編輯的SQL語句,待完善
SELECT
    b.username ,
    b.sid,
    b.serial#,
    a.sql_text
FROM
    v$sql a,
    v$session b
WHERE
    (a.sql_id = b.sql_id)
    AND ( a.hash_value = b.sql_hash_value)
    AND cpu_time > 20000
ORDER BY
    1;

--經過sql_id查找sid,serial#

select sid,serial#,username ,osuser from v$session where (sql_hash_value,sql_address)=(select hash_value,address from v$sqlarea where sql_id='f7285r39svxr6' );

html

 14.  使用觸發器實現記錄oracle用戶登陸失敗信息到alert.log日誌文件

轉載自:https://www.cnblogs.com/xinxin1994/p/6078107.html


CREATE OR REPLACE TRIGGER logon_denied_to_alert
  AFTER servererror ON DATABASE
DECLARE
  message   VARCHAR2(168);
  ip        VARCHAR2(15);
  v_os_user VARCHAR2(80);
  v_module  VARCHAR2(50);
  v_action  VARCHAR2(50);
  v_pid     VARCHAR2(10);
  v_sid     NUMBER;
  v_program VARCHAR2(48);
BEGIN
  IF (ora_is_servererror(1017)) THEN
  
    -- get ip FOR remote connections :
    IF upper(sys_context('userenv', 'network_protocol')) = 'TCP' THEN
      ip := sys_context('userenv', 'ip_address');
    END IF;
  
    SELECT sid INTO v_sid FROM sys.v_$mystat WHERE rownum < 2;
    SELECT p.spid, v.program
      INTO v_pid, v_program
      FROM v$process p, v$session v
     WHERE p.addr = v.paddr
       AND v.sid = v_sid;
  
    v_os_user := sys_context('userenv', 'os_user');
    dbms_application_info.read_module(v_module, v_action);
  
    message := to_char(SYSDATE, 'YYYYMMDD HH24MISS') ||
               ' logon denied from ' || nvl(ip, 'localhost') || ' ' ||
               v_pid || ' ' || v_os_user || ' with ' || v_program || '' ||
               v_module || ' ' || v_action;
  
    sys.dbms_system.ksdwrt(2, message);
  
  END IF;
END;

 

15.  解決Oracle 11gR2 空閒鏈接過多,致使鏈接數滿的問題

 

轉載自:http://blog.itpub.net/28998293/viewspace-1153969/

CREATE
PROFILE KILLIDLE LIMIT IDLE_TIME 30; SELECT * FROM dba_profiles WHERE PROFILE='KILLIDLE'; ALTER USER TEST_USER PROFILE KILLIDLE; SELECT username,PROFILE FROM dba_users WHERE username='TEST_USER'; ALTER SYSTEM SET resource_limit=TRUE; CREATE OR REPLACE PROCEDURE sp_kill_idlesession /********************************** 清除idle超時的會話進程 **********************************/ AS CURSOR c_kill_sqls IS SELECT 'alter system kill session ''' || s.sid || ',' || s.SERIAL# || ''' immediate' sqlstr FROM v$session s WHERE s.STATUS = 'SNIPED'; BEGIN FOR v_sql IN c_kill_sqls LOOP EXECUTE IMMEDIATE v_sql.sqlstr; END LOOP; END; --添加JOB,定時清理過時會話 DECLARE jobnum NUMBER := 661; BEGIN dbms_job.submit(job => jobnum, what => 'sp_kill_idlesession;', next_date => to_date('30-04-2014 18:00:00', 'dd-mm-yyyy hh24:mi:ss'), INTERVAL => 'SYSDATE + 1/144'); COMMIT; END; --若是30分鐘過時時間過短,對數據庫訪問性能產生了影響,能夠調整 ALTER PROFILE KILLIDLE LIMIT IDLE_TIME 30;
相關文章
相關標籤/搜索