剖析Oracle中oerr命令

來源:互聯網
上載者:User

剖析Oracle中oerr命令

Oralce中的命令非常豐富,oerr命令是一個不錯的協助工具輔助,很多看起來沒有眉目的錯誤碼,可以讓DBA很快定位問題的緣由,我們根本不需要去記有哪些ORA錯誤,除非那些錯誤已經完全和你的工作分不開。

絕大多數的命令都是二進位的形式,比如sqlplus我們可一窺其中的奧妙,oerr是一個shell指令碼,而且實現原理也不難,我們來剖析一下,看看這個工具的設計思想。

首先這個工具位於$Oracle_HOME/bin下,直接看還看不出是個shell指令碼。

[oracle@db117 ~]$ ll $ORACLE_HOME/bin/oerr
-rwxr-xr-x 1 oracle oinstall 2567 Apr 23  2014 /U01/app/oracle/product/11.2.0.4/bin/oerr
但是我就是喜歡折騰,所以無意中就開啟了這個檔案。可以看到這個指令碼的注釋如下:
# Usage: oerr facility error
#
# This shell script is used to get the description and the cause and action
# of an error from a message text file when a list of error numbers are passed
# to it.  It supports different language environments and errors from different
# facilities.
當然指令碼的完整內容也沒有多少行,我們來簡單看看,有一個設定檔facility.lis
Facilities_File=$ORACLE_HOME/lib/facility.lis
需要從這個設定檔中取得相應的component,比如我們使用命令oerr ora 12345,則ora就是第一個參數,根據這個參數可以得到接下來需要使用的component值。
指令碼裡面有如下一行類似的內容,我們帶入參數ora,當然還可以輸入tns等等。
Fac_Info=`grep -i "^${Facility}:" $Facilities_File 2> /dev/null`
我們轉換一下,可以看到得到的component是rdbms
[oracle@db117 ~]$ grep -i "^ora:" $ORACLE_HOME/lib/facility.lis
ora:rdbms:*:
至於這一行內容的解釋,在檔案中也可以輕鬆找到。
#
# The entries in this file are colon separated defining, for each
# facility (field 1), the component name (field 2), the "real" facility
# name for facilities with aliases (field 3) with a value of "*" for
# facilities without renamings and an optional facility description
# (field 4)
#
#      facility:component:rename:description
#

裡面的內容如下:
acfs:usm:*:
acfsk:usm:*:
advm:usm:acfs:
advmk:usm:*:
amd:cwmlite:*:
amdu:rdbms:*:
當然這隻是開始,錯誤資訊的檔案在一個指定的檔案中,比如component是rdbms,則錯誤資訊的檔案為$ORACLE_HOME/rdbms/mesg
[oracle@db117 ~]$ cd $ORACLE_HOME/rdbms/mesg
而錯誤資訊的檔案也是有規律的,和ora相關的一個檔案,oraus.msg也是大體這樣的格式。
[oracle@db117 mesg]$ ll ora*us*msg
-rw-r--r-- 1 oracle oinstall 4976647 Apr 23  2014 oraus.msg

比方這個時候我們移步到錯誤資訊的檔案中,找到了下面兩行錯誤資訊。
64477, 00000, "Multiple token tables are not supported."
// *Cause:  An attempt was made to create a new token table. If encountered
//          during an import, a critical patch was possibly missing on the
//          export database.
// *Action:  Use the default token table. If encountered during an import,
//          apply the appropriate patch on the export database and try the
//          export and import again.

64621, 00000, "SQL statement length (%s) exceeds maximum allowed length (%s)"
// *Cause:  An attempt was made to issue a SQL statement that exceeded the
//          maximum allowed length of a statement.
// *Action: Modify the SQL statement or the views to which it refers to fit
//          within the maximum length or split the SQL statement.
//
這個時候怎麼去解析讀取這個檔案的呢,
首先就是解析錯誤編碼,比如輸入錯誤編碼64621,我們也可以輸入064621等,這個地方都會使用sed來先轉換一下,保證能夠找到完全符合的內容。
Code=`echo 64621|/bin/sed 's/^[0]*//'`
[oracle@db117 bin]$ echo 64621|/bin/sed 's/^[0]*//'
64621
[oracle@db117 bin]$ echo 064621|/bin/sed 's/^[0]*//'
64621
假設這個時候我們就看64621的錯誤資訊,這個時候就可以使用awk來讀取檔案的內容。Oracle的調用方式類似下面的形式。
Code=64621
[oracle@db117 bin]$ awk "BEGIN { found = 0; }
      /^[0]*$Code/    { found = 1; print ; next;}
      /^\/\//        { if (found) { print; } next; }
                      { if (found) { exit; } }" $ORACLE_HOME/rdbms/mesg/oraus.msg
64621, 00000, "SQL statement length (%s) exceeds maximum allowed length (%s)"
// *Cause:  An attempt was made to issue a SQL statement that exceeded the
//          maximum allowed length of a statement.
// *Action: Modify the SQL statement or the views to which it refers to fit
//          within the maximum length or split the SQL statement.
//
所以學以致用,我們也可以仿照這種方式簡單定製符合自己需求的內容。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.