2020年8月6日 星期四

2.5 建立資料庫 (使用 SQL PLUS)

Oracle 從 9i 版本之後提供了 DBCA 這個工具方便我們建立資料庫,除了使用 DBCA ,直接透過 SQL*PLUS 也是可以將資料庫建立起來。

 

在使用 SQL*PLUS 建立資料庫之前,首先必須編輯資料庫的參數檔 init<ORACLE_SID>.ora,起始的資料庫參數檔只需賦予幾個參數如下:

——————————————————————————————-

control_files=’/opt/oradata/orcl/control01.ctl’,’/opt/oradata/orcl/control02.ctl’
audit_file_dest=’/opt/app/oracle/product/admin/orcl/adump’
db_block_size=8192
db_name=orcl
open_cursors=300
processes=300
memory_target=472M
undo_tablespace=UNDOTBS1
# For oracle 10g (若為 oracle 10g 之前的版本,則需設定下列參數)
# sga_target = 270M
# pga_aggreate_target = 150M
# background_dump_dest = /opt/app/oracle/product/admin/orcl/bdump
# core_dump_dest = /opt/app/oracle/product/admin/orcl/cdump
# user_dump_dest = /opt/app/oracle/product/admin/orcl/udump

——————————————————————————————-

 

其中記憶體參數於 Oracle 11g 之後的版本可以設定 “memory_target" ,於 Oracle 10g 的版本則更改為 “sga_target" ,9i 之前的版本則需個別設定 shared_pool_size…等記憶體參數。undo_tablespace 參數於 8i 或之前的版本需調整為rollback_segments參數。

 

建立完參數檔之後,接下來需要建立放置 dump 檔的相關目錄,其中 adump 的目錄位置是一定要建立的,因為 Oracle 預設會將 sys 登入或登出的紀錄寫入 adump ,若adump 目錄不存在,很有可能造成資料庫在啟動的過程中就出現了問題。

 

接下來我們可以編輯一個建立資料庫的 SQL Script — create_db.sql 如下:

——————————————————————————————-

create database orcl
user sys identified by oracle
user system identified by oracle
logfile group 1 (‘/opt/oradata/orcl/redo01.log’) size 50M,
group 2 (‘/opt/oradata/orcl/redo02.log’) size 50M,
group 3 (‘/opt/oradata/orcl/redo03.log’) size 50M
maxlogfiles 5
maxlogmembers 5
maxloghistory 1
maxdatafiles 100
maxinstances 2
character set UTF8
national character set AL16UTF16
datafile ‘/opt/oradata/orcl/system01.dbf’
size 325M reuse autoextend on
extent management local
sysaux datafile ‘/opt/oradata/orcl/sysaux01.dbf’
size 325M reuse autoextend on
default tablespace users
datafile ‘/opt/oradata/orcl/users01.dbf’
size 5M reuse autoextend on
default temporary tablespace temp
tempfile ‘/opt/oradata/orcl/temp01.dbf’
size 20M reuse autoextend on
undo tablespace undotbs1
datafile ‘/opt/oradata/orcl/undotbs01.dbf’
size 200M reuse autoextend on;

——————————————————————————————-

 

其中若為 Oracle 10g 之後的版本,在創見資料庫的過程當中需建立 sysaux 這個 tablespace。

做好了準備工作,我們便可以開始來建立資料庫了,首先使用參數檔將資料庫開啟至nomount 狀態:

——————————————————————————————-

[oracle@linuxdb dbs]$ export ORACLE_SID=orcl

[oracle@linuxdb dbs]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.1.0 Production on Thu May 2 01:45:24 2013

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

Connected to an idle instance.

SQL> startup nomount pfile=’/opt/app/oracle/product/11.2.0/dbhome_1/dbs/initorcl.ora’
ORACLE instance started.

Total System Global Area  493813760 bytes
Fixed Size            1337436 bytes
Variable Size          301991844 bytes
Database Buffers      184549376 bytes
Redo Buffers            5935104 bytes
SQL>

——————————————————————————————-

 

資料庫 Instance 以參數檔起動之後,便可以使用 create_db.sql 來建立資料庫:

——————————————————————————————-

SQL> @/opt/app/oracle/product/11.2.0/dbhome_1/dbs/create_db.sql

Database created.

——————————————————————————————-

 

資料庫建立完成後,必須執行 $ORACLE_HOME/rdbms/admin 目錄底下的 catalog.sql、catproc.sql 建立系統的資料字典,以及 $ORACLE_HOME/sqlplus/admin 底下的 pupbld.sql 建立 product_user_profile :

——————————————————————————————-

SQL> @?/rdbms/admin/catalog.sql
DOC>######################################################################
DOC>######################################################################
DOC>    The following statement will cause an “ORA-01722: invalid number"
DOC>    error and terminate the SQLPLUS session if the user is not SYS.
DOC>    Disconnect and reconnect with AS SYSDBA.
DOC>######################################################################
DOC>######################################################################
DOC>#

no rows selected

Session altered.

TIMESTAMP
————————————————————
COMP_TIMESTAMP CATALG_BGN 2013-05-02 01:51:49 2456415 06709

Package created.

Package body created.

Grant succeeded.

Package created.

Synonym created.

……以下略

SQL> @ ?/rdbms/admin/catproc.sql
DOC>######################################################################
DOC>######################################################################
DOC>    The following PL/SQL block will cause an ORA-20000 error and
DOC>    terminate the current SQLPLUS session if the user is not SYS.
DOC>    Disconnect and reconnect with AS SYSDBA.
DOC>######################################################################
DOC>######################################################################
DOC>#

PL/SQL procedure successfully completed.

Session altered.

PL/SQL procedure successfully completed.

View created.

View created.

Comment created.

Comment created.

Comment created.

……以下略

SQL> @?/sqlplus/admin/pupbld.sql

……略

——————————————————————————————-

 

執行完畢之後便完成了資料庫的建立。



沒有留言:

張貼留言