Oracle 11g Administrator’s Guide笔记2:如何手工创建、删除11gR2数据库?

在前一篇文章中提到阅读Oracle 11gR2 Administrator‘s Guide(文档编号E25494-01)时,简单描述了DDL_LOCK_TIMEOUT参数。在Oracle 11gR2 Administrator‘s Guide文档的chapter 2 Creating and Configuring an Oracle Database章节时,提到手工建库,本文简单记录一下,如何在OEL 5.5 X86_64位环境下,手工创建及删除11.2.0.1.0的数据库,将文件放到文件系统上存放。当然,利用DBCA来建库、删库比较简单,就不再赘述了。具体操作步骤如下:

1 前提条件,操作系统上已经安装好Oracle 11gR2 的软件,其中环境变量ORACLE_BASE=/u01/app/oracleORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1

[root@OCM11g ~]# su - oracle
OCM11g-> env | grep ORA
ORACLE_BASE=/u01/app/oracle
ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1
OCM11g->

2 确认环境变量,本例中选择ORACLE_SID=manualdb,ORACLE_HOME选用原有的/u01/app/oracle/product/11.2.0/db_1

OCM11g-> id
uid=1101(oracle) gid=1000(oinstall) groups=1000(oinstall),1201(asmdba),1300(dba),1301(oper)
OCM11g-> uname -rm
2.6.18-194.el5 x86_64
OCM11g-> export ORACLE_SID=manualdb
OCM11g-> env | grep ORA
ORACLE_SID=manualdb
ORACLE_BASE=/u01/app/oracle
ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1
OCM11g->

3 创建初始化参数文件,这里将参数文件放到系统默认的路径下,即$ORACLE_HOME/dbs,命名为initmanualdb.ora,其内容如下:

OCM11g-> cat /u01/app/oracle/product/11.2.0/db_1/dbs/initmanualdb.ora 
db_name=manualdb
memory_target=600m
OCM11g->

简单起见,我们只在初始化参数文件中指定了db_name,memory_target这两个参数。我们知道,启动数据库实例的最少参数只需要一个db_name即可,这里选择db_name=manualdb,附加一个memory_target=600m,这个是11g的新参数,用于控制SGA+PGA的总大小。当然,也可以在参数文件中指定control_files,如果该参数被忽略的话,那么Oracle会自动在$ORACLE_HOME/dbs路径下创建一个名为cntrl$ORACLE_SID.dbf的控制文件。

4 准备将来存放数据文件、日志文件的路径。这两类文件的路径如下:

datafile:

OCM11g-> mkdir -p /u02/manualdb/oradata

online log:

OCM11g-> mkdir -p /u02/manualdb/onlinelog
OCM11g-> ll /u02/manualdb/
total 8
drwxr-xr-x 2 oracle oinstall 4096 Mar 30 10:56 onlinelog
drwxr-xr-x 2 oracle oinstall 4096 Mar 30 10:56 oradata
OCM11g->

5 启动manualdb实例到NOMOUNT状态,其实,在该环境下,我们的控制文件尚未生成,实例至多也只能加载到NOMOUNT状态:

OCM11g-> id
uid=1101(oracle) gid=1000(oinstall) groups=1000(oinstall),1201(asmdba),1300(dba),1301(oper)
OCM11g-> env | grep ORA
ORACLE_SID=manualdb
ORACLE_BASE=/u01/app/oracle
ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1
OCM11g-> sqlplus 

SQL*Plus: Release 11.2.0.1.0 Production on Fri Mar 30 10:59:32 2012

Copyright (c) 1982, 2009, Oracle.  All rights reserved.

Connected to an idle instance.

SQL> startup
ORACLE instance started.

Total System Global Area  626327552 bytes
Fixed Size                  2215944 bytes
Variable Size             373297144 bytes
Database Buffers          243269632 bytes
Redo Buffers                7544832 bytes
ORA-00205: error in identifying control file, check alert log for more info

SQL>

上述报错ORA-00205提示控制文件找不到,正是我们所预见的,因为当前控制文件还未生成,所以数据库没法MOUNT,说明数据库是NOMOUNT状态。这时,也可以看到相关的后台进程已经启动:

SQL> !
OCM11g-> ps -ef | grep ora_|grep -v grep
oracle   14885     1  0 11:02 ?        00:00:00 ora_pmon_manualdb
oracle   14887     1  0 11:02 ?        00:00:00 ora_vktm_manualdb
oracle   14891     1  0 11:02 ?        00:00:00 ora_gen0_manualdb
oracle   14893     1  0 11:02 ?        00:00:00 ora_diag_manualdb
oracle   14895     1  0 11:02 ?        00:00:00 ora_dbrm_manualdb
oracle   14897     1  0 11:02 ?        00:00:00 ora_psp0_manualdb
oracle   14899     1  0 11:02 ?        00:00:00 ora_dia0_manualdb
oracle   14903     1  6 11:02 ?        00:00:27 ora_mman_manualdb
oracle   14905     1  0 11:02 ?        00:00:01 ora_dbw0_manualdb
oracle   14907     1  0 11:02 ?        00:00:00 ora_lgwr_manualdb
oracle   14909     1  0 11:02 ?        00:00:00 ora_ckpt_manualdb
oracle   14911     1  0 11:02 ?        00:00:00 ora_smon_manualdb
oracle   14913     1  0 11:03 ?        00:00:00 ora_reco_manualdb
oracle   14915     1  0 11:03 ?        00:00:00 ora_mmon_manualdb
oracle   14917     1  0 11:03 ?        00:00:00 ora_mmnl_manualdb
OCM11g->

6 执行下述CREATE DATABASE的命令,开始手工创建Oracle 数据库:

SQL> conn / as sysdba;
Connected.
SQL> select open_mode from v$database;
select open_mode from v$database
                      *
ERROR at line 1:
ORA-01507: database not mounted

SQL> CREATE DATABASE manualdb
  2     USER SYS IDENTIFIED BY oracle
  3     USER SYSTEM IDENTIFIED BY oracle
  4     LOGFILE GROUP 1 ('/u02/manualdb/onlinelog/redo01a.log') SIZE 50M BLOCKSIZE 512,
  5             GROUP 2 ('/u02/manualdb/onlinelog/redo02a.log') SIZE 50M BLOCKSIZE 512
  6     MAXLOGFILES 5
  7     MAXLOGMEMBERS 5
  8     MAXLOGHISTORY 1
  9     MAXDATAFILES 100
 10     CHARACTER SET AL32UTF8
 11     NATIONAL CHARACTER SET AL16UTF16
 12     EXTENT MANAGEMENT LOCAL
 13     DATAFILE '/u02/manualdb/oradata/system01.dbf' SIZE 300M REUSE
   SYSAUX DATAFILE '/u02/manualdb/oradata/sysaux01.dbf' SIZE 300M REUSE
   DEFAULT TABLESPACE users
      DATAFILE '/u02/manualdb/oradata/users01.dbf'
 17        SIZE 5M REUSE AUTOEXTEND ON MAXSIZE UNLIMITED
 18     DEFAULT TEMPORARY TABLESPACE tempts1
 19        TEMPFILE '/u02/manualdb/oradata/temp01.dbf'
 20        SIZE 20M REUSE
 21     UNDO TABLESPACE undotbs
 22        DATAFILE '/u02/manualdb/oradata/undotbs01.dbf'
 23        SIZE 20M REUSE AUTOEXTEND ON MAXSIZE UNLIMITED;

Database created.

SQL> select name,open_mode from v$database;

NAME      OPEN_MODE
--------- --------------------
MANUALDB  READ WRITE

SQL>

7 创建数据字典视图,分别执行下述脚本来完成:

SQL> @?/rdbms/admin/catalog.sql
......
......
SQL> @?/rdbms/admin/catproc.sql
......
......
SQL> @?/sqlplus/admin/pupbld.sql
......
......

上述3个脚本的说明见下表:

The Scripts and descriptions

Script

Description

CATALOG.SQL

Creates the views of the data dictionary tables,

the dynamic performance views,and public synonyms

for many of the views.Grants PUBLIC access to the synonyms.

CATPROC.SQL

Runs all scripts required for or used with PL/SQL.

PUPBLD.SQL

Required for SQL*Plus. Enables SQL*Plus to disable

commands by user.

8 至此,我们完成了手工创建Oracle 数据库。

SQL> select * from v$version;

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

5 rows selected.

SQL> select name,open_mode from v$database;

NAME      OPEN_MODE
--------- --------------------
MANUALDB  READ WRITE

1 row selected.

SQL> select name from v$datafile
  2  union
  3  select name from v$controlfile
  4  union
  5  select member from v$logfile;

NAME
--------------------------------------------------------------------------------
/u01/app/oracle/product/11.2.0/db_1/dbs/cntrlmanualdb.dbf
/u02/manualdb/onlinelog/redo01a.log
/u02/manualdb/onlinelog/redo02a.log
/u02/manualdb/oradata/sysaux01.dbf
/u02/manualdb/oradata/system01.dbf
/u02/manualdb/oradata/undotbs01.dbf
/u02/manualdb/oradata/users01.dbf

7 rows selected.

SQL>

9 最后,如果该数据库不需要的话。我们可以执行手工删除数据库,当然必须要将数据库启动到MOUNT RESTRICT状态来删除

如何确认实例是否是RESTRICTED MODE:

SQL> select instance_name,status,startup_time,logins from v$instance;

INSTANCE_NAME   STATUS       STARTUP_TIME        LOGINS
--------------- ------------ ------------------- ----------
manualdb        MOUNTED      2012/03/30 13:38:52 ALLOWED

1 row selected.

SQL> alter system enable restricted session;

System altered.

SQL> select instance_name,status,startup_time,logins from v$instance;

INSTANCE_NAME   STATUS       STARTUP_TIME        LOGINS
--------------- ------------ ------------------- ----------
manualdb        MOUNTED      2012/03/30 13:38:52 RESTRICTED

1 row selected.

SQL> alter system disable restricted session;

System altered.

SQL> select instance_name,status,startup_time,logins from v$instance;

INSTANCE_NAME   STATUS       STARTUP_TIME        LOGINS
--------------- ------------ ------------------- ----------
manualdb        MOUNTED      2012/03/30 13:38:52 ALLOWED

1 row selected.

SQL>

通过查看V$INSTANCE动态性能视图的LOGINS字段,如果该值为ALLOWED,说明实例是正常启动,并未进入RESTRICTED MODE,普通用户可以正常访问;如果该值为RESTRICTED说明是RESTRICTED MODE,即需要具有RESTRICTED SESSION系统权限的用户方可访问;

手工删除数据库:

SQL> select instance_name,status,startup_time,logins from v$instance;

INSTANCE_NAME   STATUS       STARTUP_TIME        LOGINS
--------------- ------------ ------------------- ----------
manualdb        MOUNTED      2012/03/30 13:38:52 ALLOWED

1 row selected.

SQL> drop database;
drop database
*
ERROR at line 1:
ORA-12719: operation requires database is in RESTRICTED mode

SQL> alter system enable restricted session;

System altered.

SQL> select instance_name,status,startup_time,logins from v$instance;

INSTANCE_NAME   STATUS       STARTUP_TIME        LOGINS
--------------- ------------ ------------------- ----------
manualdb        MOUNTED      2012/03/30 13:38:52 RESTRICTED

1 row selected.

SQL> drop database;

Database dropped.

Disconnected from 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>

此时,Oracle自动删除该数据库的所有数据文件、联机重做日志文件、控制文件以及初始化参数文件。从alert日志文件里可以看到下述信息:

Fri Mar 30 13:45:33 2012
drop database
Deleted file /u02/manualdb/oradata/system01.dbf
Deleted file /u02/manualdb/oradata/sysaux01.dbf
Deleted file /u02/manualdb/oradata/undotbs01.dbf
Deleted file /u02/manualdb/oradata/users01.dbf
Deleted file /u02/manualdb/onlinelog/redo01a.log
Deleted file /u02/manualdb/onlinelog/redo02a.log
Deleted file /u02/manualdb/oradata/temp01.dbf
Deleted file /u01/app/oracle/product/11.2.0/db_1/dbs/snapcf_manualdb.f

10 简单总结,可见手工创建、删除数据库也很简单。当然,在第6步中执行手工建库的命令中还有诸多其它选项,比如选择OMF,这时,只要在参数文件中指定DB_CREATE_FILE_DEST参数,那么建库的命令将更加简单。

如何禁用Oracle 11g口令大小写?

        我们知道,从Oracle 11g开始,默认情况下,数据库用户的口令严格区分大小写,这有别于以前版本的口令不区分大小写。

        11g:

SQL> show user;
USER is "SYS"
SQL> select * from v$version;

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

SQL> conn HR/hr;
Connected.
SQL> conn HR/HR;
ERROR:
ORA-01017: invalid username/password; logon denied


Warning: You are no longer connected to ORACLE.
SQL> 

        10g:

SQL> show user;
USER is "SYS"
SQL> select * from v$version;

BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bi
PL/SQL Release 10.2.0.5.0 - Production
CORE    10.2.0.5.0      Production
TNS for Linux: Version 10.2.0.5.0 - Production
NLSRTL Version 10.2.0.5.0 - Production

SQL> conn HUMAN/hr;
Connected.
SQL> conn HUMAN/HR;
Connected.
SQL> 

        而且,在DBA_USERS数据字典表的PASSWORD列中已经不再存储加密的口令,在11g以前版本的数据库中可以直接从DBA_USERS数据字典表中获取用户的加密口令,那么在11g数据库里如何查看用户加密后的口令呢?答案是需要查看查看USER$字典表,当然在10g版本的数据库中,也可以从USER$字典表中查看用户的加密口令:

        11g:

SQL> show user;
USER is "SYS"
SQL> select * from v$version;

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

SQL> select username,password from dba_users where username='HR';

USERNAME                       PASSWORD
------------------------------ ------------------------------
HR

SQL> select name,password from user$ where name='HR';

NAME                           PASSWORD
------------------------------ ------------------------------
HR                             4C6D73C3E8B0F0DA

SQL> 

        10g:

SQL> show user;
USER is "SYS"
SQL> select * from v$version;

BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bi
PL/SQL Release 10.2.0.5.0 - Production
CORE    10.2.0.5.0      Production
TNS for Linux: Version 10.2.0.5.0 - Production
NLSRTL Version 10.2.0.5.0 - Production

SQL> select username,password from dba_users where username='HUMAN';

USERNAME                       PASSWORD
------------------------------ ------------------------------
HUMAN                          CBFA7677C00F9AE1

SQL> select name,password from user$ where name='HUMAN';

NAME                           PASSWORD
------------------------------ ------------------------------
HUMAN                          CBFA7677C00F9AE1

SQL> 

        Oracle为什么会这么做呢?应该是为了保护数据库的安全性,才这么做的吧。在11g中,需要拥有SELECT ANY DICTIONARY的角色才可用查看USER$字典表。当然,我们不建议将SYS.USER$表的访问权限给普通用户,也不建议将SELECT ANY DICTIONARY的角色给普通用户。

        那么,在11g数据库中如何禁用口令大小写呢?

SQL> show parameter sensitive

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
sec_case_sensitive_logon             boolean     TRUE
SQL> conn HR/hr;
Connected.
SQL> conn HR/HR;
ERROR:
ORA-01017: invalid username/password; logon denied


Warning: You are no longer connected to ORACLE.
SQL> conn / as sysdba;
Connected.
SQL> alter system set sec_case_sensitive_logon=false;

System altered.

SQL> show parameter sensitive

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
sec_case_sensitive_logon             boolean     FALSE
SQL> conn HR/hr;
Connected.
SQL> conn HR/HR;
Connected.
SQL> 

        最后,从下,我们可以看到虽然用户的口令是大小写区分的,但是存放用户口令的密文却是一样的

SQL> select name,password from user$ where name='HR';

NAME                           PASSWORD
------------------------------ ------------------------------
HR                             4C6D73C3E8B0F0DA

SQL> alter user hr identified by Hr;

User altered.

SQL> select name,password from user$ where name='HR';

NAME                           PASSWORD
------------------------------ ------------------------------
HR                             4C6D73C3E8B0F0DA

SQL> alter user hr identified by hR;

User altered.

SQL> select name,password from user$ where name='HR';

NAME                           PASSWORD
------------------------------ ------------------------------
HR                             4C6D73C3E8B0F0DA

SQL> 

          从上,我们可以看到,在11g中,不管用户的口令是大写还是小写,最终存放在数据库中的密文口令都是相同的。应该是,Oracle在对口令加密之前,统一转换为大写或者是小写后,然后开始对口令加密,最后形成加密口令。那么究竟是大写还是小写呢,或者是什么其他手段就不得而知了,还有就是Oracle采用的是什么加密算法,我想这些oracle是绝对不会对外公布的^_^

Oracle数据字典表和动态性能视图学习之1:V$DATAGUARD_STATS

在管理和维护Oracle数据库的工作中,DBA不得不通过查询数据库的数据字典表、动态性能视图来获取数据库的相关信息。

本文通过介绍V$DATAGUARD_STATS这一动态性能视图来获取关于Physical standby database的相关信息。在一套Dataguard环境下,如果需要做failover Role Transition的话,建议先在备库上通过查询V$DATAGUARD_STATS视图来估算failover切换需要的时间(failover time=apply finish time+estimated startup time)。

首先来看两个查询:

查询1,来源于10g Physical standby database :

SQL> select * from v$version;

BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bi
PL/SQL Release 10.2.0.5.0 - Production
CORE    10.2.0.5.0      Production
TNS for Linux: Version 10.2.0.5.0 - Production
NLSRTL Version 10.2.0.5.0 - Production

SQL> select protection_mode,database_role,open_mode from v$database;

PROTECTION_MODE     DATABASE_ROLE     OPEN_MODE
------------------- ----------------- ---------
MAXIMUM PERFORMANCE PHYSICAL STANDBY  MOUNTED

SQL> select * from v$dataguard_stats;

NAME                    VALUE          UNIT                           TIME_COMPUTED
----------------------- -------------- ------------------------------ --------------------
apply finish time       +00 00:00:00.1 day(2) to second(1) interval   20-FEB-2012 14:05:18
apply lag               +00 00:00:15   day(2) to second(0) interval   20-FEB-2012 14:05:18
estimated startup time  161            second                         20-FEB-2012 14:05:18
standby has been open   Y                                             20-FEB-2012 14:05:18
transport lag           +00 00:00:07   day(2) to second(0) interval   20-FEB-2012 14:05:18

SQL> 

查询2,来源于11g Physical standby database:

SQL> select * from v$version;

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

SQL> select protection_mode,database_role,open_mode from v$database;

PROTECTION_MODE      DATABASE_ROLE    OPEN_MODE
-------------------- ---------------- --------------------
MAXIMUM PERFORMANCE  PHYSICAL STANDBY READ ONLY WITH APPLY

SQL> select * from v$dataguard_stats;

NAME                    VALUE            UNIT                          TIME_COMPUTED        DATUM_TIME
----------------------- ---------------- ----------------------------- -------------------- -------------------
transport lag           +00 00:00:00     day(2) to second(0) interval  02/20/2012 14:07:37  02/20/2012 14:07:36
apply lag               +00 00:00:00     day(2) to second(0) interval  02/20/2012 14:07:37  02/20/2012 14:07:36
apply finish time       +00 00:00:00.000 day(2) to second(3) interval  02/20/2012 14:07:37
estimated startup time  36               second                        02/20/2012 14:07:37

SQL> 

然后,依据上面的输出结果来简单介绍一下V$DATAGUARD_STATS这一动态性能视图的相关信息:

在官方文档上,关于V$DATAGUARD_STATS是这样描述的:该动态性能视图显示出在主库上产生了多少重做日志数据,但是还没有被备库所应用。所以,通过查询该视图可以基本确定如果万一主库出现崩溃的话,备库上将丢失多少重做日志数据。我们可以在一套Dataguard环境下的任一备库的实例上从该视图里获取相关信息,然而,在主库的实例上查询该视图返回的信息都将是空。也就是说,只可以从备库的实例上查询V$DATAGUARD_STATS,从主库实例上是看不到任何有用信息的。

接下来,解释一下各个字段的值信息:

NAME

  • apply lag,该值表示在通过在备库上应用主库传递过来的重做日志与出库同步所延迟的时间。APPLY LAG: Amount of time that the application of redo data on the standby database lags behind the primary database.从查询中可以看到第1个延迟15秒,第2个延迟0秒。说明该11g的备库应用重做日志已经与该主库完全同步了。
  • transport lag,该值表示在单位时间内主库上产生的重做日志还没有传输到备库上,或者主库上产生的重做日志还没有被备库所应用。从查询中看到第1个10g备库上的日志传输延迟7秒,而第2个11g备库的日志传输延迟为0。
  • apply finish time,该值表示在备库上完成应用重做日志所需要的时间。从第1个查询中看到完成应用重做日志还需要0.1秒,第2个查询中则为0,因为已经完全同步。
  • estimated startup time,该值表示启动和打开物理备库所需要的时间,该字段不是适用于逻辑备库。 An estimate of the time needed to start and open the database.
  • standby has been open,该值表示物理备库自从上次启动以来,是否以OPEN READ ONLY方式打开过?该参数值如果是Y,现在需要做FAILOVER,那么就需要先将该物理备库shutdown然后以OPEN READ WRITE方式打开。从第1个查询中,看到该物理备库如果做FAILOVER,那么就需要shutdown—>startup open read write;第2个查询中则没有该记录,因为11g的dataguard可以一边OPEN READ ONLY,一边执行redo apply,也就是11g 的ACTIVE Dataguard。

VALUE:给出各个参数的值。如第1个查询中的,apply finish time值为+00 00:00:00.1,说明该物理备库需要0.1秒的时间来完成应用剩余的重做日志数据。

UNIT:各个参数的时间单元。

TIME_COMPUTED:物理备库上估算各个参数的本地时间。

DATUM_TIME:在物理备库上获取元数据来估算  APPLY LAG 和 TRANSPORT LAG 这两个参数值的本地时间。如果从多次查询中看到该时间值对应的APPLY LAG 和 TRANSPORT LAG 这两个参数值保持不变的话,那么就说明该物理备库已经停止从主库接收到重做数据!该字段是11g中新出现的。

最后,这是在学习dataguard时,新了解和学习的动态性能视图,个人觉得比较有用,就根据自己的理解简单记录之。