Oracle 实用技巧

一、rlwrap
在 linux中使用sqlplus对数据库进行操作,常常由于敲错命令或需要反复执行某条语句,需要像linux本身有的历史回调的功能, rlwrap 可以用来支持oracle下sqlplus历史命令的回调功能,提高操作效率。
 
1、下载
目前最新版本 rlwrap-0.42.tar.gz (274 k)
 
2、上传到linux服务器上
使用各类文件传输工具将压缩包上传到服务器,如xmanager的xftp工具
 
3、解压缩
[root@OEL ~]# tar -zxvf rlwrap-0.42.tar.gz 
 
4、安装
解压后会产生一个 rlwrap-0.42 文件夹,运行文件夹中的configure文件进行安装
[root@OEL rlwrap-0.42]# ./configure
[root@OEL rlwrap-0.42]#make
[root@OEL rlwrap-0.42]#make install
 
5、安装rlwrap错误问题的解决
 
【You need the GNU readline library(ftp://ftp.gnu.org/gnu/readline/ ) to build this program.】
 
如果安装rlwrap的时候出现上述问题,那么你可能缺失两个包:
 
libtermcap-devel- 2.0.8-46.1.i386.rpm
readline-devel-5.1-1.1.i386.rpm
 
需要在系统光盘或或镜像文件中使用rpm进行安装缺失的软件包后再执行 rlwrap工具的安装
 
6、验证安装的结果
切换到oracle用户使用rlwrap方式登陆到数据库进行测试
[root@OEL ~]# su - oracle
[oracle@OEL ~]$ rlwrap sqlplus / as sysdba
SQL*Plus: Release 11.2.0.1.0 Production on Fri Dec 5 10:51:22 2014
Copyright (c) 1982, 2009, Oracle.  All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL>select status from v$instance;
SQL>select * from v$version;
 
在执行完语句后能使用上下箭头进行指令的回调,并且可以使用backspace退格键进行删除,大大提高了效率
 
7、写入配置文件,方便使用
[oracle@OEL ~]$ vi .bashrc
 
 # User specific aliases and functions
alias sqlplus='rlwrap sqlplus'
alias rman='rlwrap rman'
alias adrci='rlwrap adrci'

[oracle@OEL ~]$ source .bashrc
 
这样就能在sqlplus下也能有linux系统的查看历史命令的功能了
 
 
 
 
二、glogin.sql文件

oracle数据库安装好之后,默认在$ORACLE_HOME下面的sqlplus/admin/目录里面会有glogin.sql配置文件

这是一个全局的并且会自动运行的配置文件,在客户端使用sqlplus的时候会自动调用这个文件。

因此我们可以在glogin.sql文件里面加入我们经常使用的一些设置,比如要设置登录的用户和实例等。
 
1、设置在登陆sqlplus后显示用户名和实例名
在默认登陆到sqlplus后,只会显示一个SQL>作为的提示符,这样就需要通过指令来查询登陆的用户和实例名称【尤其是在需要切换到不同数据库时,容易造成误操作】
 
在glogin.sql文件中加入下列语句来实现用户名和实例名的显示
 
set sqlprompt "_user'@'_connect_identifier> "
 
 然后再登陆到sqlplus环境中,这样提示符就会变成对应的结果
[oracle@OEL ~]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.1.0 Production on Fri Dec 5 11:37:23 2014
Copyright (c) 1982, 2009, Oracle.  All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
sys@ORA11> 
 
2、设置页面的长宽显示比例
在进行系统维护时,由于部分列名过长,导致查询出来的结果往往显示十分杂乱,不便于查看,这时就需要对页面进行设置
 
在glogin.sql文件中加入两个设置
set linesize 200
set pagesize 9999
设置的值可根据页面的大小修改到合适的结果显示
 
sys@ORA11> set linesize 40
sys@ORA11> select * from v$version;
BANNER
----------------------------------------
Oracle Database 11g Enterprise Edition R
elease 11.2.0.1.0 - 64bit Production

PL/SQL Release 11.2.0.1.0 - Production
CORE     11.2.0.1.0     Production
TNS for Linux: Version 11.2.0.1.0 - Prod
uction

NLSRTL Version 11.2.0.1.0 - Production


sys@ORA11> set linesize 200
sys@ORA11> /

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE     11.2.0.1.0     Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production

 
3、其他一些常见的设置项
 
set serveroutput on  --设置默认打开dbms_output的输出
define _editor=/usr/local/bin/vim --定义使用的编辑器【定义编辑器为vim,windows平台可设置为notepad】
column object_name format a30  --设置object_name的列长尾30【字符列使用a+数字来表示长度】
column object_id format 999999   --设置object_id列长尾6【数字列需要使用999表示长度,显示多少位就用多少个9表示长度】
 
 
4、查看可设置的选项
可在sqlplus中执行show all,查看当前使用的设置项目
 
 
三、其他常用技巧
 
1、临时返回系统提示符
当在sqlplus中执行维护操作时,想返回到主机提示符下,但又不想退出当前会话,可采用!、ho、host 这3个命令中的任意一个
sys@ORA11> !  --临时返回操作系统提示符下
[oracle@OEL ~]$ ls
database_check.sql  database_info.txt  oradiag_oracle
database_info.sql   Desktop            plsql_code
[oracle@OEL ~]$ exit --返回sqlplus提示符下
 
----------------------------------------------------------------
sys@ORA11> ho --临时返回操作系统提示符下
[oracle@OEL ~]$ ls
database_check.sql  database_info.txt  oradiag_oracle
database_info.sql   Desktop            plsql_code
[oracle@OEL ~]$ exit --返回sqlplus提示符下
---------------------------------------------------------------
sys@ORA11> host --临时返回操作系统提示符下
[oracle@OEL ~]$ ls
database_check.sql  database_info.txt  oradiag_oracle
database_info.sql   Desktop            plsql_code
[oracle@OEL ~]$ exit --返回sqlplus提示符下
 
注:使用rman过程中也能临时返回系统提示符下,但只能使用host指令返回
 
2、oerr指令
oerr可以在Linux系统上查询简短的报错信息的含义,可以快速有效地辅助排查Oracle故障。oerr在oracle软件安装时会一并安装
 
a、oerr的位置
[oracle@OEL ~]$ which oerr
/u01/app/oracle/product/11.2.0/dbhome_1/bin/oerr   --oerr默认在$ORACLE_HOME/bin目录下
 
b、oerr的用法
oerr ora 【errorid】
 
例子:
sys@ORA11> select * from v$instance;
select * from v$instance
*
ERROR at line 1:
ORA-01034: ORACLE not available
 
[oracle@OEL ~]$ oerr ora 01034
01034, 00000, "ORACLE not available"
// *Cause: Oracle was not started up. Possible causes include the following:
//         - The SGA requires more space than was allocated for it.
//         - The operating-system variable pointing to the instance is
//           improperly defined.
// *Action: Refer to accompanying messages for possible causes and correct
//          the problem mentioned in the other messages.
//          If Oracle has been initialized, then on some operating systems,
//          verify that Oracle was linked correctly. See the platform
//          specific Oracle documentation.
 
通过使用oracle自带的oerr工具能快速了解错误原因并了解如果着手进行处理
 
 
 
 
 
 
 
转载请说明出处 |QQ:327488733@qq.com
原文地址:https://www.cnblogs.com/zhenxing/p/4150093.html