有没有一种方法可以将Oracle数据库导出到CREATE DATABASE命令?


9

我有一个现有的Oracle 11实例,我想将数据库(通过DBCA创建)导出到等效的“ CREATE DATABASE”命令,处理字符集等。我还想获取数据文件的列表,并创建同一数据库所需的表空间。我对保留单个架构对象不感兴趣。

我问是因为有DBMS_DDL包,它将为您提供DDL来重新创建任何表等。想知道在数据库级别是否存在类似的东西。

谢谢!

Answers:


9

我建议不要从nls_database_parameters视图中获取字符集信息,然后使用它DBCA来创建新的数据库以及dbms_metadata.get_ddl处理表空间的创建,而不是这样做。-更容易,更不容易出错。字符集和国家字符集实际上是在创建数据库后就很难更改的唯一内容,并且DBCA您可以在UI中选择它们。我想您需要打包一些脚本来自动执行创建工作?

无论如何,如果您确实必须继续进行操作,则不能完全按照开箱即用的要求进行操作,但是可以转储控制文件,该文件将为您提供构造CREATE DATABASE语句所需的大部分信息,包括大多数pfile参数(在发出CREATE DATABASE语句之前,需要pfile来启动新实例),重做日志和字符集等。

例如:

SQL> alter database backup controlfile to trace as '/tmp/db.sql';

Database altered.

SQL> !cat /tmp/db.sql
-- The following are current System-scope REDO Log Archival related
-- parameters and can be included in the database initialization file.
--
-- LOG_ARCHIVE_DEST=''
-- LOG_ARCHIVE_DUPLEX_DEST=''
--
-- LOG_ARCHIVE_FORMAT=%t_%s_%r.dbf
--
-- DB_UNIQUE_NAME="PHIL112"
--
-- LOG_ARCHIVE_CONFIG='SEND, RECEIVE, NODG_CONFIG'
-- LOG_ARCHIVE_MAX_PROCESSES=4
-- STANDBY_FILE_MANAGEMENT=MANUAL
-- STANDBY_ARCHIVE_DEST=?/dbs/arch
-- FAL_CLIENT=''
-- FAL_SERVER=''
--
-- LOG_ARCHIVE_DEST_1='LOCATION=USE_DB_RECOVERY_FILE_DEST'
-- LOG_ARCHIVE_DEST_1='MANDATORY NOREOPEN NODELAY'
-- LOG_ARCHIVE_DEST_1='ARCH NOAFFIRM EXPEDITE NOVERIFY SYNC'
-- LOG_ARCHIVE_DEST_1='NOREGISTER NOALTERNATE NODEPENDENCY'
-- LOG_ARCHIVE_DEST_1='NOMAX_FAILURE NOQUOTA_SIZE NOQUOTA_USED NODB_UNIQUE_NAME'
-- LOG_ARCHIVE_DEST_1='VALID_FOR=(PRIMARY_ROLE,ONLINE_LOGFILES)'
-- LOG_ARCHIVE_DEST_STATE_1=ENABLE

--
-- Below are two sets of SQL statements, each of which creates a new
-- control file and uses it to open the database. The first set opens
-- the database with the NORESETLOGS option and should be used only if
-- the current versions of all online logs are available. The second
-- set opens the database with the RESETLOGS option and should be used
-- if online logs are unavailable.
-- The appropriate set of statements can be copied from the trace into
-- a script file, edited as necessary, and executed when there is a
-- need to re-create the control file.
--
--     Set #1. NORESETLOGS case
--
-- The following commands will create a new control file and use it
-- to open the database.
-- Data used by Recovery Manager will be lost.
-- Additional logs may be required for media recovery of offline
-- Use this only if the current versions of all online logs are
-- available.

-- After mounting the created controlfile, the following SQL
-- statement will place the database in the appropriate
-- protection mode:
--  ALTER DATABASE SET STANDBY DATABASE TO MAXIMIZE PERFORMANCE

STARTUP NOMOUNT
CREATE CONTROLFILE REUSE DATABASE "PHIL112" NORESETLOGS  ARCHIVELOG
    MAXLOGFILES 16
    MAXLOGMEMBERS 3
    MAXDATAFILES 100
    MAXINSTANCES 8
    MAXLOGHISTORY 292
LOGFILE
  GROUP 1 '/u01/app/oracle/oradata/PHIL112/redo01.log'  SIZE 50M BLOCKSIZE 512,
  GROUP 2 '/u01/app/oracle/oradata/PHIL112/redo02.log'  SIZE 50M BLOCKSIZE 512,
  GROUP 3 '/u01/app/oracle/oradata/PHIL112/redo03.log'  SIZE 50M BLOCKSIZE 512
-- STANDBY LOGFILE
DATAFILE
  '/u01/app/oracle/oradata/PHIL112/system01.dbf',
  '/u01/app/oracle/oradata/PHIL112/sysaux01.dbf',
  '/u01/app/oracle/oradata/PHIL112/undotbs01.dbf',
  '/u01/app/oracle/oradata/PHIL112/users01.dbf'
CHARACTER SET WE8MSWIN1252
;

-- Commands to re-create incarnation table
-- Below log names MUST be changed to existing filenames on
-- disk. Any one log file from each branch can be used to
-- re-create incarnation records.
-- ALTER DATABASE REGISTER LOGFILE '/u01/app/oracle/fast_recovery_area/PHIL112/archivelog/2013_01_10/o1_mf_1_1_%u_.arc';
-- ALTER DATABASE REGISTER LOGFILE '/u01/app/oracle/fast_recovery_area/PHIL112/archivelog/2013_01_10/o1_mf_1_1_%u_.arc';
-- Recovery is required if any of the datafiles are restored backups,
-- or if the last shutdown was not normal or immediate.
RECOVER DATABASE

-- All logs need archiving and a log switch is needed.
ALTER SYSTEM ARCHIVE LOG ALL;

-- Database can now be opened normally.
ALTER DATABASE OPEN;

-- Commands to add tempfiles to temporary tablespaces.
-- Online tempfiles have complete space information.
-- Other tempfiles may require adjustment.
ALTER TABLESPACE TEMP ADD TEMPFILE '/u01/app/oracle/oradata/PHIL112/temp01.dbf'
     SIZE 937426944  REUSE AUTOEXTEND ON NEXT 655360  MAXSIZE 32767M;
-- End of tempfile additions.
--
--     Set #2. RESETLOGS case
--
-- The following commands will create a new control file and use it
-- to open the database.
-- Data used by Recovery Manager will be lost.
-- The contents of online logs will be lost and all backups will
-- be invalidated. Use this only if online logs are damaged.

-- After mounting the created controlfile, the following SQL
-- statement will place the database in the appropriate
-- protection mode:
--  ALTER DATABASE SET STANDBY DATABASE TO MAXIMIZE PERFORMANCE

STARTUP NOMOUNT
CREATE CONTROLFILE REUSE DATABASE "PHIL112" RESETLOGS  ARCHIVELOG
    MAXLOGFILES 16
    MAXLOGMEMBERS 3
    MAXDATAFILES 100
    MAXINSTANCES 8
    MAXLOGHISTORY 292
LOGFILE
  GROUP 1 '/u01/app/oracle/oradata/PHIL112/redo01.log'  SIZE 50M BLOCKSIZE 512,
  GROUP 2 '/u01/app/oracle/oradata/PHIL112/redo02.log'  SIZE 50M BLOCKSIZE 512,
  GROUP 3 '/u01/app/oracle/oradata/PHIL112/redo03.log'  SIZE 50M BLOCKSIZE 512
-- STANDBY LOGFILE
DATAFILE
  '/u01/app/oracle/oradata/PHIL112/system01.dbf',
  '/u01/app/oracle/oradata/PHIL112/sysaux01.dbf',
  '/u01/app/oracle/oradata/PHIL112/undotbs01.dbf',
  '/u01/app/oracle/oradata/PHIL112/users01.dbf'
CHARACTER SET WE8MSWIN1252
;

-- Commands to re-create incarnation table
-- Below log names MUST be changed to existing filenames on
-- disk. Any one log file from each branch can be used to
-- re-create incarnation records.
-- ALTER DATABASE REGISTER LOGFILE '/u01/app/oracle/fast_recovery_area/PHIL112/archivelog/2013_01_10/o1_mf_1_1_%u_.arc';
-- ALTER DATABASE REGISTER LOGFILE '/u01/app/oracle/fast_recovery_area/PHIL112/archivelog/2013_01_10/o1_mf_1_1_%u_.arc';
-- Recovery is required if any of the datafiles are restored backups,
-- or if the last shutdown was not normal or immediate.
RECOVER DATABASE USING BACKUP CONTROLFILE

-- Database can now be opened zeroing the online logs.
ALTER DATABASE OPEN RESETLOGS;

-- Commands to add tempfiles to temporary tablespaces.
-- Online tempfiles have complete space information.
-- Other tempfiles may require adjustment.
ALTER TABLESPACE TEMP ADD TEMPFILE '/u01/app/oracle/oradata/PHIL112/temp01.dbf'
     SIZE 937426944  REUSE AUTOEXTEND ON NEXT 655360  MAXSIZE 32767M;
-- End of tempfile additions.
--

SQL>

然后,可以使用以下命令提取表空间创建DDL:

select dbms_metadata.get_ddl('TABLESPACE', tablespace_name) as ts_ddl
from dba_tablespaces;

如果要我进一步介绍手动创建数据库所需的所有步骤,请询问(如果您还不知道)。


谢谢!出色的答案和良好的猜测,我们已经安装了可在用户不允许或不允许DBCA的位置进行部署的安装。如果您不介意,能否请您进一步介绍手动创建数据库的步骤?再次感谢!
基兰(Kieran)2013年

您是否考虑过仅在本地创建一个Shell数据库,将其压缩并压缩,然后为客户提供?这样会容易得多,并且出错的可能性也较小。
菲尔ᵀᴹ

不幸的是,我认为他们不会这么做。他们需要能够手动查看与数据库相关的任何内容。
基兰(Kieran)2013年

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.