今天针对如何在Linux操作系统上利用命令行创建Oracle Database进行一个总结及实践。
下面是官方文档“Database Administrator's Guide”中所讲的步骤。Step5针对的是Windows操作系统,本次实践不会涉及。Creating a Database with the CREATE DATABASE Statement
Step 1: Specify an Instance Identifier (SID)
Step 2: Ensure That the Required Environment Variables Are Set
Step 3: Choose a Database Administrator Authentication Method
Step 4: Create the Initialization Parameter File
Step 5: (Windows Only) Create an Instance
Step 6: Connect to the Instance
Step 7: Create a Server Parameter File
Step 8: Start the Instance
Step 9: Issue the CREATE DATABASE Statement
Step 10: Create Additional Tablespaces
Step 11: Run Scripts to Build Data Dictionary Views
现在按照上面的步骤进行创建数据库。Step 1: Specify an Instance Identifier (SID)
[oracle@TaylonMeng ~]$ export ORACLE_SID=ZYX
[oracle@TaylonMeng ~]$ echo $ORACLE_SID
ZYX
注:此处ORACLE_SID应该与后续口令文件、参数文件的命名中的SID大小写保持一致,否则会在Step7报ORA-01078的错误。
SQL> create spfile from pfile;
create spfile from pfile
*
ERROR at line 1:
ORA-01078: failure in processing system parameters
LRM-00109: could not open parameter file
'/oracle/product/11.2.0/dbs/initzyx.ora'
Step 2: Ensure That the Required Environment Variables Are Set
必须设定的几个变量有ORACLE_SID、ORACLE_HOME、PATH变量中包含ORACLE_HOME/bin目录。
[oracle@TaylonMeng ~]$ echo $ORACLE_SID
ZYX
[oracle@TaylonMeng ~]$ echo $ORACLE_HOME
/oracle/product/11.2.0
[oracle@TaylonMeng ~]$ echo $PATH
/oracle/product/11.2.0/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/oracle/bin
Step 3: Choose a Database Administrator Authentication Method
With a password file
With operating system authentication
这里我选择口令文件验证。所以要建立口令文件。口令文件的默认位置为$ORACLE_HOME/dbs,命名规则为ORAPW
[oracle@TaylonMeng ~]$ orapwd file=$ORACLE_HOME/dbs/orapwZYX password=oracle entries=30
[oracle@TaylonMeng ~]$ cd $ORACLE_HOME/dbs
[oracle@TaylonMeng dbs]$ ls
hc_MTH.dat initMTH.ora lkMTH orapwMTH spfileMTH.ora
hc_ORCL.dat init.ora lkORCL orapwORCL spfileORCL.ora
initDBUA4624925.ora initORCL.ora orapwDBUA4624925 orapwZYX
Step 4: Create the Initialization Parameter File
在$ORACLE_HOME/dbs下,init.ora文件是pfile的模板,可以通过修改该文件的内容,生成所需要的pfile。文本参数文件的默认位置为$ORACLE_HOME/dbs,命名规则为init.ora
[oracle@TaylonMeng dbs]$ cat init.ora | grep -v ^# | grep -v ^$ > initZYX.ora
[oracle@TaylonMeng dbs]$ vi initZYX.ora
db_name='ORCL'
memory_target=1G
processes = 150
audit_file_dest='/admin/orcl/adump'
audit_trail ='db'
db_block_size=8192
db_domain=''
db_recovery_file_dest='/flash_recovery_area'
db_recovery_file_dest_size=2G
diagnostic_dest=''
dispatchers='(PROTOCOL=TCP) (SERVICE=ORCLXDB)'
open_cursors=300
remote_login_passwordfile='EXCLUSIVE'
undo_tablespace='UNDOTBS1'
control_files = (ora_control1, ora_control2)
compatible ='11.2.0'
(1)使用‘zyx'替换’orcl',大小写都替换。
:%s@orcl@zyx@gi
(2)用目录替换
:%s@@/oracle/product@g
(3)修改control_files变量
control_files = (/orasys/ZYX/control01.ctl,/oraidx/ZYX/control02.ctl )
(4)创建参数文件中所涉及到的目录
[oracle@TaylonMeng dbs]$ mkdir -p /oracle/product/admin/zyx/adump
[oracle@TaylonMeng dbs]$ mkdir -p /oracle/product/flash_recovery_area
[oracle@TaylonMeng dbs]$ mkdir -p /orasys/ZYX/
[oracle@TaylonMeng dbs]$ mkdir -p /oraidx/ZYX/
最后,经过修改的initZYX.ora的内容如下:
[oracle@TaylonMeng dbs]$ more initZYX.ora
db_name='zyx'
memory_target=1G
processes = 150
audit_file_dest='/oracle/product/admin/zyx/adump'
audit_trail ='db'
db_block_size=8192
db_domain=''
db_recovery_file_dest='/oracle/product/flash_recovery_area'
db_recovery_file_dest_size=2G
diagnostic_dest='/oracle/product'
dispatchers='(PROTOCOL=TCP) (SERVICE=zyxXDB)'
open_cursors=300
remote_login_passwordfile='EXCLUSIVE'
undo_tablespace='UNDOTBS1'
control_files = (/orasys/ZYX/control01.ctl,/oraidx/ZYX/control02.ctl )
compatible ='11.2.0'
Step 6: Connect to the Instance
[oracle@TaylonMeng ~]$ sqlplus sys as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Sun May 1 18:53:30 2016
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Enter password:
Connected to an idle instance.
Step 7: Create a Server Parameter File
SQL> create spfile from pfile;
File created.
可以看到在$ORACLE_HOME/dbs下多了spfileZYX.ora这样一个文件。
[oracle@TaylonMeng dbs]$ ls
hc_MTH.dat init.ora lkORCL orapwZYX
hc_ORCL.dat initORCL.ora orapwDBUA4624925 spfileMTH.ora
initDBUA4624925.ora initZYX.ora orapwMTH spfileORCL.ora
initMTH.ora lkMTH orapwORCL spfileZYX.ora
Step 8: Start the Instance
在启动实例的过程中,报MEMORY_TARGET设定的错误。我将MEMORY_TARGET的值由1G改为了700M,重新生成spfileZYX.ora,之后启动到nomount状态。
SQL> startup nomount
ORA-00845: MEMORY_TARGET not supported on this system
[oracle@TaylonMeng dbs]$ vi initZYX.ora
db_name='ZYX'
memory_target=700M
processes = 150
audit_file_dest='/oracle/product/admin/zyx/adump'
audit_trail ='db'
db_block_size=8192
db_domain=''
db_recovery_file_dest='/oracle/product/flash_recovery_area'
db_recovery_file_dest_size=2G
diagnostic_dest='/oracle/product'
dispatchers='(PROTOCOL=TCP) (SERVICE=zyxXDB)'
open_cursors=300
remote_login_passwordfile='EXCLUSIVE'
undo_tablespace='UNDOTBS1'
control_files = (/orasys/ZYX/control01.ctl,/oraidx/ZYX/control02.ctl )
compatible ='11.2.0'
[oracle@TaylonMeng dbs]$ sqlplus sys as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Sun May 1 19:17:19 2016
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Enter password:
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> create spfile from pfile;
File created.
SQL> startup nomount;
ORACLE instance started.
Total System Global Area 730714112 bytes
Fixed Size 2256832 bytes
Variable Size 478150720 bytes
Database Buffers 243269632 bytes
Redo Buffers 7036928 bytes
Step 9: Issue the CREATE DATABASE Statement
编辑一个CreateDB.sql文件,copy官方文档中的CREATE DATABASE语句,并做修改。
[oracle@TaylonMeng ~]$ vi CreateDB.sql
CREATE DATABASE ZYX
USER SYS IDENTIFIED BY sys_password
USER SYSTEM IDENTIFIED BY system_password
LOGFILE GROUP 1 ('/oraidx/ZYX/redo01a.log','/oralog/ZYX/redo01b.log') SIZE 100M BLOCKSIZE 512,
GROUP 2 ('/oraidx/ZYX/redo02a.log','/oralog/ZYX/redo02b.log') SIZE 100M BLOCKSIZE 512,
GROUP 3 ('/oraidx/ZYX/redo03a.log','/oralog/ZYX/redo03b.log') SIZE 100M BLOCKSIZE 512
MAXLOGFILES 5
MAXLOGMEMBERS 5
MAXLOGHISTORY 1
MAXDATAFILES 100
CHARACTER SET AL32UTF8
NATIONAL CHARACTER SET AL16UTF16
EXTENT MANAGEMENT LOCAL
DATAFILE '/orasys/ZYX/system01.dbf' SIZE 325M REUSE
SYSAUX DATAFILE '/orasys/ZYX/sysaux01.dbf' SIZE 325M REUSE
DEFAULT TABLESPACE users
DATAFILE '/orasys/ZYX/users01.dbf'
SIZE 500M REUSE AUTOEXTEND ON MAXSIZE UNLIMITED
DEFAULT TEMPORARY TABLESPACE tempts1
TEMPFILE '/orasys/ZYX/temp01.dbf'
SIZE 20M REUSE
UNDO TABLESPACE undotbs1
DATAFILE '/orasys/ZYX/undotbs01.dbf'
SIZE 200M REUSE AUTOEXTEND ON MAXSIZE UNLIMITED;
(1)用‘ZYX’替换原本的‘mynewdb'
(2)修改SYS和SYSTEM用户的密码
(3)修改logfile、SYSTEM、SYSAUX、users、tempts1、undotbs数据文件的路径
(4)修改undo表空间的名称为undotbs1,与之前参数文件中的undo表空间名称一致
(5)创建所涉及的目录
(6)根据自己实际情况,修改语句的其他内容
执行CreateDB.sql文件
SQL> @/home/oracle/CreateDB.sql
Database created.
此时数据库已经是open状态了。
SQL> select open_mode,name from v$database;
OPEN_MODE NAME
-------------------- ---------
READ WRITE ZYX
Step 11: Run Scripts to Build Data Dictionary Views
In SQL*Plus, connect to your Oracle Database instance with theSYSDBAadministrative privilege:
@?/rdbms/admin/catalog.sql
@?/rdbms/admin/catproc.sql
@?/rdbms/admin/utlrp.sql
In SQL*Plus, connect to your Oracle Database instance asSYSTEMuser:
@?/sqlplus/admin/pupbld.sql
下面是四个脚本各自的作用。
Script
Description
CATALOG.SQL
Creates the views of the data dictionary tables, the dynamic performance views, and public synonyms for many of the views. GrantsPUBLIC access to the synonyms.
CATPROC.SQL
Runs all scripts required for or used with PL/SQL.
UTLRP.SQL
Recompiles all PL/SQL modules that are in an invalid state, including packages, procedures, and types.
PUPBLD.SQL
Required for SQL*Plus. Enables SQL*Plus to disable commands by user.
自此,就成功使用命令行创建好了Oracle数据库。