2014-04-01
盖国强http://www.eygle.com/
国外网站:http://asktom.oracle.com
1.oracle监听
vim /u01/app/oracle/11.2.0/db_1/network/admin/listener.ora
=========================
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.10.54.243)(PORT = 1521))
)
)
ADR_BASE_LISTENER = /u01/app/oracle
==================================
lsnrctl stop/start --停止/启动监听
lsnrctl status --监听状态
2.配置连接标识符
vim /u01/app/oracle/11.2.0/db_1/network/admin/tnsnames.ora
=================================
ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.10.54.53)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
##指定远程数据库
orcl243 =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.10.54.243)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
=======================================
shell>sql sys/oracle@orcl243 as sysdba
##登录
测试:shell>tnsping orcl243
shell>sql scott/tiger@orcl243
3.sqlplus连接语法
{<username>[/<password>][@<connect_identifier>] | / }
[AS {SYSDBA | SYSOPER | SYSASM}] [EDITION=value]
4.常用命令
登录:sql / as sysdba
查看当前用户:show user;
切换远程用户:conn socott/tiger@orcl243;
切换本机用户:conn socott/tiger
5.常用变量
##设置每行显示字符数
SQL> set linesize 100;
##设置每页显示行数
SQL> set pagesize 20;
SQL> select * from dba_objects where rownum<21;
6.开启屏幕输出
##开启前
SQL> BEGIN
BEGIN
2 dbms_output.put_line('This is the first line');
dbms_output.put_line('This is the first line');
3 END;
END;
4 /
/
PL/SQL procedure successfully completed.
SQL> set serverout on --开启
##开启后
SQL> BEGIN
BEGIN
2 dbms_output.put_line('This is the first line');
dbms_output.put_line('This is the first line');
3 END;
END;
4 /
/
This is the first line
PL/SQL procedure successfully completed.
##改变SQL样式
[oracle@oracle53 ~]$ vim /u01/app/oracle/11.2.0/db_1/sqlplus/admin/glogin.sql
===========================================
ALTER SESSION SET nls_date_format = 'HH:MM:SS';
SET SQLPROMPT "_USER'@'_CONNECT_IDENTIFIER _DATE> "
SET PAGESIZE 24
SET LINESIZE 78
SET NUMFORMAT $99,999
============================================
7.回收站
SQL>show recyclebin;
SQL>purge recyclebin; --清空
##还原回收站
SQL>flashback table yangry to before drop;
##查看参数
show parameters;
select file#,name from v$datafile;
##显示任意指定列的属性
cloumn name format a50;
col name for a50; --简写
8.将SQL缓冲区内容保存到文件中
SQL>select username,process,sid,serial# from v$session where status='ACTIVE'
2 .
SQL> save status.sql
Created file status.sql
##执行脚本
SQL>@status.sql
SQL>/
##查看归档
archive log list;
9.oracle 逻辑体系结构:block(块)---extent(区)---segment(段)----tablespace(表空间)
block为8KB(8192字节),为oracle最小的逻辑单位
oracle最小扩展单位为区,默认为1MB
SYS@orcl 10:55:02>show parameter db_block_size;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_block_size integer 8192
##查看表空间
SYS@orcl 11:04:10> desc dba_tablespaces;
SYS@orcl 11:11:35>select tablespace_name,block_size from dba_tablespaces;
10.为用户创建表空间
(1)创建表空间
SYS@orcl 11:17:14>create tablespace tbs_01 datafile '/u01/app/oracle/oradata/orcl/tbs_01.dbf'
2 size 100m extent management local segment space management auto;
(2)创建用户
SYS@orcl 11:19:06>create user xiaoq identified by xiaoq default tablespace tbs_01 temporary tablespace temp;
SYS@orcl 11:20:29>grant dba to xiaoq;
(3)切换用户
SYS@orcl 11:04:38> conn xiaoq/xiaoq;
XIAOQ@orcl 11:04:52> create table ssr(id int) tablespace tbs_01;
create table ssr(id int) tablespace tbs_01;
(4)插入数据,其中dual是虚表
XIAOQ@orcl 11:04:32> insert into ssr select rownum from dual connect by level <=1000;
XIAOQ@orcl 11:04:22> commit;
(5)查看表空间
XIAOQ@orcl 11:04:16> select segment_name,extent_id,tablespace_name,bytes/1024/1024 bytes_mb,blocks from user_extents where segment_name='SSR';
11.删除表空间重新创建
XIAOQ@orcl 11:04:03> drop table ssr purge;
XIAOQ@orcl 11:04:46> col segment_name for a20;
XIAOQ@orcl 11:04:43> col tablespace_name for a20;
XIAOQ@orcl 11:04:28> set linesize 300;
XIAOQ@orcl 11:04:01> insert into ssr select rownum from dual connect by level <=1000;
XIAOQ@orcl 11:04:58> select segment_name,segment_type,tablespace_name,blocks,extents,bytes/1024/1024 bytes_mb from user_segments where segment_name='SSR';
##创建索引
XIAOQ@orcl 11:04:30> create index idx_id on ssr(id);
XIAOQ@orcl 11:04:14> select count(*) from user_exents where segment_name='IDX_ID';
12.调整blocksize
XIAOQ@orcl 11:04:23> show parameter cache_size;
XIAOQ@orcl 11:04:28> alter system set db_16k_cache_size=100m;
XIAOQ@orcl 11:04:31> create tablespace tbs_16k blocksize 16k datafile '/u01/app/oracle/oradata/orcl/tbs_16k_01.dbf' size 100m autoextend on extent management local segment space management auto;
XIAOQ@orcl 11:04:53> select tablespace_name,block_size from dba_tablespaces;
13.系统默认表空间
YANGRY@orcl 19:39:56>select tablespace_name,block_size from dba_tablespaces;
TABLESPACE_NAME BLOCK_SIZE
-------------------- ----------
SYSTEM 8192
SYSAUX 8192
UNDOTBS1 8192
TEMP 8192
USERS 8192
14.查看oracle系统后台进程
shell> ps -ef|grep orcl
SQL>select paddr,description from v$bgprocess where paddr !='00' order by paddr desc;
##进程解析
日志写入器进程:LGWR
归档进程:ARCN
检查点进程:CKPT
进程监控器进程:PMONC
诊断性进程:DIAO,在oracle11g之前的版本,该进程用于rac环境。负责监视实例的整体情况,而且会捕获实例失败的所需信息。
vktm:虚拟时间管理器,为oracle实现一个一致的,细粒度的时钟。
gen0:通用任务执行进程,分担另外某个进程的阻塞处理,并在后台完成所阻塞的操作。
dbrm:数据库资源管理进程,实现一个数据库实例配置的资源计划。
psp0:进程生气器,负责生成各个后台进程/线程,会在实例启动时完成它的大部分工作
15.修改oracle归档模式
(1)连接系统用户
YANGRY@orcl 19:57:29>conn sys/oracle as sysdba
(2)查看归档状态
SYS@orcl 20:15:22>archive log list;
Database log mode No Archive Mode
Automatic archival Disabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 22
Current log sequence 24
(3)关闭
SYS@orcl 20:15:26>shutdown immediate;
(4)启动
SYS@orcl 20:15:50>startup mount;
(5)归档
SYS@orcl >alter database archivelog;
(6)打开
SYS@orcl >alter database open;
(7)查看归档
SYS@orcl >select name,created,log_mode from v$database;
NAME CREATED LOG_MODE
-------------------------------------------------- --------- ------------
ORCL 31-MAR-14 ARCHIVELOG
SYS@orcl >show parameter db_recovery_file_dest;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_recovery_file_dest string /u01/app/oracle/flash_recovery
_area
db_recovery_file_dest_size big integer 3882M
SYS@orcl >alter system set db_recovery_file_dest_size=2g;
System altered.
SYS@orcl >show parameter db_recovery_file_dest;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_recovery_file_dest string /u01/app/oracle/flash_recovery
_area
db_recovery_file_dest_size big integer 2G
16.oracle启动3个过程:nomount---->mount---->open
(1)nomount状态,启动实例
SYS@orcl >startup nomount;
---oracle参数文件(spfile/pfile)
SYS@orcl >show parameter spfile;
NAME TYPE VALUE
------------ ---------- -----------
spfile string /u01/app/oracle/11.2.0/db_1/dbs/spfileorcl.ora
---创建pfile --init$ORACLE_SID.ora 命令方式
SYS@orcl >create pfile from spfile;
File created.
(2)mount状态,打开控制文件
SYS@orcl >startup mount;
ORA-01081: cannot start already-running ORACLE - shut it down first
SYS@orcl >select * from v$controlfile;
no rows selected
SYS@orcl >select * from v$controlfile;
STATUS NAME IS_ BLOCK_SIZE
------- -------------------------------------------------- --- ----------
FILE_SIZE_BLKS
--------------
/u01/app/oracle/oradata/orcl/control01.ctl NO 16384
594
/u01/app/oracle/flash_recovery_area/orcl/control02 NO 16384
.ctl
594
##获取控制文件内容
SYS@orcl >alter database backup controlfile to trace;
Database altered.
SYS@orcl >select * from v$diag_info where name='Default Trace File';
INST_ID NAME
---------- --------------------------------------------------
VALUE
--------------------------------------------------------------------------------
1 Default Trace File
/u01/app/oracle/diag/rdbms/orcl/orcl/trace/orcl_ora_30452.trc
(3)open状态,打开控制文件中描述的文件
SYS@orcl >alter database open;
Database altered.