11G新功能的官方文档https://docs.oracle.com/cd/E11882_01/server.112/e41360/toc.htm
段的延迟分配
11G之前的版本中,当创建一张表时,会自动分配段空间,这样做有几个弊端:
1. 初始创建表时就需要分配空间,自然会占用一些时间,如果初始化多张表,这种影响就被放大。
2. 如果很多表开始的一段时间都不需要,那么就会浪费这些空间。
为此,从11gR2开始,有一种新特性,叫延迟段,即延迟分配段空间。简单讲,默认将表(以及索引、LOB)的物理空间分配推迟到第一条记录插入到表中时。即有实际的数据插入表中时,再为每个对象初始化空间分配。
参数:DEFERRED_SEGMENT_CREATION
注意:SYS的表是不能使用延迟段的,因此创建时还是立即分配段空间。
SQL> show parameter DEFERRED_SEGMENT_CREATION
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
deferred_segment_creation boolean TRUE
SQL> show user
USER is "SYS"
SQL> create table seg_sys (hid number);
SQL> create table hr.seg_hr(hid number);
SQL> select owner,segment_name,bytes from dba_segments where segment_name in ('SEG_SYS','SEG_HR');
SQL> insert into hr.seg_hr values(1);
SQL> select owner,segment_name,bytes from dba_segments where segment_name in ('SEG_SYS','SEG_HR');
SQL> alter system set deferred_segment_creation=false;
SQL> create table hr.seg_hr2(hid number);
SQL> select owner,segment_name,bytes from dba_segments where segment_name in ('SEG_SYS','SEG_HR','SEG_HR2');
Scheduler
官方课件《管理II_2.pdf》P202
Scheduler的官方文档https://docs.oracle.com/cd/E11882_01/server.112/e25494/schedover.htm#ADMIN033
10G之前创建定时作业是使用dbms_job包,10G之后建议使用dbms_scheduler包,Scheduler的功能大大强于普通job,scheduler可以记录作业运行的历史记录,可以使用操作系统命令,可以使用调度窗口等
create table tab_jobs as select * from dba_objects where rownum<11;
create or replace procedure pro_job as
begin
insert into tab_jobs select * from tab_jobs where rownum<2;
commit;
end;
select * from user_jobs
BEGIN
DBMS_JOB.SUBMIT(
job => :job,
what => 'pro_job;',
next_date => sysdate,
interval => 'sysdate+1/1440'
);
END;
Commit;
select * from user_jobs
create table tab_schedulers as select * from dba_objects where rownum<11;
create or replace procedure pro_scheduler as
begin
insert into tab_schedulers select * from tab_schedulers where rownum<2;
commit;
end;
select * from user_scheduler_jobs where job_name='TESTJOB123'
BEGIN
DBMS_SCHEDULER.CREATE_JOB(
job_name =>'testjob123',
job_type =>'STORED_PROCEDURE',
job_action =>'pro_scheduler', --没有分号
start_date=>SYSTIMESTAMP,
repeat_interval =>'FREQ=MINUTELY;INTERVAL=1',
enabled=>true
);
END;
select * from user_scheduler_jobs where job_name='TESTJOB123'
select * from USER_SCHEDULER_JOB_RUN_DETAILS where job_name='TESTJOB123'
可以查看到每次运行的记录,普通job没有这样的视图
EM来操作
Server-Oracle Scheduler
不管program、schedule、job_class、window、window group怎么去定义,最后都是为create_job服务的,即这些东西功能都是DBMS_SCHEDULER.CREATE_JOB所用到的一部分
job:Command+schedule
Command:Program|PL/SQL|Procedure|Executable|Chain
schedule:schedule|window|window group
window:schedule
window group:window
Sqlloader
Sqlloader官方文档https://docs.oracle.com/cd/E11882_01/server.112/e22490/ldr_concepts.htm#SUTIL003
SQL*Loader loads data from external files into tables of an Oracle database. It has a powerful data parsing engine that puts little limitation on the format of the data in the data file.
SQL*Loader可以将外部文件的数据加载到Oracle数据库的表中
Sqlloader的步骤
1) Oracle 数据库端建好需要导入数据的表的结构
2) 确定要导入的外部文件的位置和文件名
3) 手工编辑一个XXX.CTL 的控制文件
4) 命令行加载数据
SQL*Loader的难点主要是sqlldr控制文件的写法。
[oracle@ocp ~]$ cat /home/oracle/test_sqlloader.txt
1,liming,2011-01-02 11:46:26
2,wangfeng,2012-02-02 12:46:26
SQL> create table testsqlloader(hid number,hname varchar2(20),htime date);
[oracle@ocp ~]$ vi sqlloader.ctl
load data
CHARACTERSET AL32UTF8
infile '/home/oracle/test_sqlloader.txt'
into table sys.testsqlloader
fields terminated by whitespace
TRAILING NULLCOLS
(hid terminated by ',',
hname terminated by ',',
htime date "yyyy-mm-dd hh24:mi:ss"
)
--into table sys.testsqlloader
插入到表中,如果表有记录,则要修改为append into
sqlldr "'sys/oracle as sysdba'" control=sqlloader.ctl log=sqlloader.log
可以使用EM图形界面一步步操作,期间会自动生成sqlloader控制文件,步骤如下
Data Movement->Load Data from User Files->Automatically Generate Control File->
Load Data: Data Files->Load Data: Table and File Format->Load Data: File Format Attributes->Load Data: Load Method->Load Data: Options->Load Data: Schedule->Load Data: Review(这一步先不执行,Control File Contents可以直接看到控制文件内容,File Options可以不勾选导入完数据后删除控制文件,默认是勾选的)
审计
官方文档https://docs.oracle.com/cd/E11882_01/network.112/e36292/auditing.htm#DBSEG006
审计分类
强制审计、标准审计、细粒度审计
强制审计:就算关闭了标准审计,startup、shutdown、以as sysdba权限连接DB的操作,无论如何都会记录,记录存放在audit_file_dest参数对应目录的XX.aud文件中
标准审计
日常所说的审计默认就是指标准审计,使用audit、noaudit命令来操作的审计操作
开启标准审计
设置audit_trail参数不等于none即开启了标准审计
标准审计已开启,默认就是就有30项左右标准审计功能开始进行审计,即不用使用audit命令就有的,见如下sql
select * from DBA_STMT_AUDIT_OPTS
union
select * from DBA_PRIV_AUDIT_OPTS;
--如果生产环境开启了标准审计,根据实际情况关闭上诉sql的一些你认为不需要的审计项,比如noaudit create session
audit_trail参数见https://docs.oracle.com/cd/E11882_01/server.112/e40402/initparams017.htm#REFRN10006
none:Disables standard auditing
os:Directs all audit records to an operating system file
db:Directs audit records to the database audit trail (the SYS.AUD$ table)
--标准审计,如果审计数据存放在DB中,那么是存放在sys.aud$表中,其他DBA_AUDIT_开头的视图里的数据都是由sys.aud$所得,比如DBA_AUDIT_TRAIL、DBA_AUDIT_EXISTS、DBA_AUDIT_OBJECT、DBA_AUDIT_SESSION等。
audit_file_dest参数见https://docs.oracle.com/cd/E11882_01/server.112/e40402/initparams014.htm#REFRN10004
AUDIT_FILE_DEST参数设置审计记录存放的OS路径
AUDIT_FILE_DEST specifies the operating system directory into which the audit trail is written when the AUDIT_TRAIL initialization parameter is set to os, xml, or xml,extended.
默认值ORACLE_BASE/admin/ORACLE_SID/adump
标准审计级别
语句级审计:
权限级审计:
对象级审计:
SQL> audit table by hr;--语句级审计
Audit succeeded.
SQL> audit create view by hr;--权限级审计
Audit succeeded.
SQL> audit select on scott.emp by hr; --对象级审计
audit select on scott.emp by hr
*
ERROR at line 1:
ORA-01708: ACCESS or SESSION expected
--其中对象级审计对所有用户进行审计,加上by user会报错
SQL> audit select on scott.emp;--对象级审计
Audit succeeded.
SQL> audit select,update,insert,delete on scott.salgrade; --审计所有对salgrade工资表的操作
默认情况下,特权用户和SYS 用户的操作不会生成审计记录。如果需要审计,则设置AUDIT_SYS_OPERATIONS参数值为true
AUDIT_SYS_OPERATIONS参数见https://docs.oracle.com/cd/E11882_01/server.112/e40402/initparams015.htm#REFRN10005
查询标准审计结果
select * from DBA_AUDIT_TRAIL;
select * from sys.aud$;
细粒度审计
调用DBMS_FGA包,细粒度审计结果存放在sys.fga_log$
begin
DBMS_FGA.ADD_POLICY (
object_schema => 'scott',
object_name => 'emp',
policy_name => 'mypolicy1',
audit_condition => 'sal < 1000',
audit_column => 'comm,sal',
handler_schema => NULL,
handler_module => NULL,
enable => TRUE,
statement_types => 'select',
audit_trail=>DBMS_FGA.DB,
audit_column_opts => DBMS_FGA.ANY_COLUMNS);
end;
scott用户执行查询
select * from emp where sal < 1000
select * from sys.fga_log$--有结果
细粒度审计涉及的SQL
select * from DBA_AUDIT_POLICIES; --查询设置了哪些细粒度审计
select * from dba_fga_audit_trail; --查询细粒度审计结果
ASM
官方课件《管理I_1.pdf》P201
ASM的官方文档https://docs.oracle.com/cd/E11882_01/server.112/e18951/toc.htm
磁盘组的三种冗余:
external 外部冗余,不提供任何镜像,两块5G磁盘,总容量10G
normal 正常冗余,提供双向镜像,两块5G磁盘,总容量5G
high 高冗余,提供三向镜像,三块5G磁盘,总容量5G
查看asmdiskgroup的信息(磁盘组的可用空间等)
[grid@OCPASM]$asmcmd lsdg --discovery -g
SQL> select name,total_mb,free_mb,round((free_mb/total_mb)*100) "canuse%" from v$asm_diskgroup;
查看asmdisk对应的物理磁盘信息
[grid@OCPASM]$ asmcmd lsdsk
Path
/dev/oracleasm/disks/OCPDATA1
[grid@OCPASM]$ll /dev/oracleasm/disks/OCPDATA1
brw-rw---- 1 grid dba 8, 17 Jun 6 15:57 /dev/oracleasm/disks/OCPDATA1
[grid@OCPASM]$ ll /dev|grep 8|grep 17
brw-r----- 1 root disk 8, 17 Oct 14 09:58 sdb1
asmcmd命令大全可使用help查看
[grid@OCPASM]$ asmcmd
ASMCMD> help
ASM_POWER_LIMIT指定用于磁盘再平衡的自动存储管理实例的最大功耗。上限越高,再平衡的速度就越快。较低的值需要更长的时间,但是消耗更少的处理和I/O资源。
ASM_POWER_LIMIT设置为0时,不会自动重平衡,但是可以使用下面的语句来手动重平衡
SQL> alter diskgroup DATA rebalance power 10;--此语句只能在ASM实例上运行,且连接方式为as sysasm
是否重平衡完毕,运行如下语句,没有结果就表示重平衡完毕
SQL> select * from v$asm_operation; --此语句在ASM实例上执行才能看到准确信息
复制数据库
见官方课件《管理II_2.pdf》P309
实验步骤
本实验,源端和目标端都在同一台服务器内,源端ocp,目标端ocpdup。
1、目标端的操作步骤
目标端有静态监听文件、密码文件、参数文件,能启动到nomount状态
ORACLE_SID=ocpdup
cd $ORACLE_HOME/dbs
orapwd file=orapwocpdup password=oracle
vi initocpdup.ora
audit_file_dest='/u01/app/oracle/admin/ocpdup/adump'
db_name='ocpdup'
control_files='/u01/app/oracle/oradata/ocpdup/control01.ctl','/u01/app/oracle/oradata/ocpdup/control02.ctl'
db_file_name_convert='/u01/app/oracle/oradata/ocp','/u01/app/oracle/oradata/ocpdup'
log_file_name_convert='/u01/app/oracle/oradata/ocp','/u01/app/oracle/oradata/ocpdup'
vi $ORACLE_HOME/network/admin/listener.ora
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = ocpdup)
(ORACLE_HOME = /u01/app/oracle/product/11.2.0/db_1)
(SID_NAME = ocpdup)
)
)
lsnrctl reload
lsnrctl status
mkdir -p /u01/app/oracle/oradata/ocpdup
mkdir -p /u01/app/oracle/admin/ocpdup/adump
sqlplus / as sysdba
SQL> create spfile from pfile;
SQL> startup nomount
ll /u01/app/oracle/oradata/ocpdup/
--没有数据文件
2、源端的操作
ORACLE_SID=ocp
SQL> archive log list
SQL> shutdown immediate
SQL> startup mount
SQL> alter database archivelog;
SQL> alter database open;
SQL> alter system archive log current;
vi $ORACLE_HOME/network/admin/tnsnames.ora
ocpdup =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = ocp)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = ocpdup)
)
)
rman target sys/oracle@ocp auxiliary sys/oracle@ocpdup
RMAN> duplicate target database to ocpdup from active database;
3、目标端验证
ORACLE_SID=ocpdup
SQL> select instance_name,status from v$instance;
ll /u01/app/oracle/oradata/ocpdup/
--数据文件也有了