今天和大家分享下数据库的创建和删除的步骤,里面有很多细节需要大家考虑。创建数据库不只是一个create database语句。删除数据库 drop database也不是随时都能执行的。
--创建数据库的步骤。
1.用户,文件系统,挂载点和网络的配置,内核参数配置
这个需要提前准备好。
2.ORACLE_HOME的安装和PSU
oracle_home的安装,这个也可以静默安装,不过有条件还是图形界面装,也省事。
最好打上最新的psu。省去不少潜在的问题。
3.初始化参数的考虑
有4个必备的参数。
db_name
undo_management=auto
control_files
sga_target
在这个基础上,需要考虑到一些性能参数.隐含参数
在此贴一个示例,可以在这个基础上进行修改。
*._db_block_numa=1
*._enable_NUMA_optimization=FALSE
*._kghdsidx_count=1
*._ksmg_granule_size=16777216
*._optimizer_cost_based_transformation='OFF'
*._optimizer_cost_model='IO'
*._optimizer_skip_scan_enabled=FALSE
*.aq_tm_processes=0
*.audit_file_dest='/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/oradmp/audit/'
*.compatible='11.2.0.2'
*.control_file_record_keep_time=30
*.control_files='/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/control/cntrl_1.dbf','/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/control/cntrl_2.dbf','/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/control/cntrl_3.dbf'
*.core_dump_dest='/dbtestPT1/oracle/TEST/oradmp/cdump/'
*.db_block_size=8192
*.db_cache_size=4096M
*.db_domain=''
*.db_file_multiblock_read_count=128
*.db_name='TEST'
*.db_writer_processes=4
*.deferred_segment_creation=FALSE
*.diagnostic_dest='/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/oradmp'
*.java_pool_size=500M
*.job_queue_processes=10
*.large_pool_size=500M
*.log_archive_dest='/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/arc'
*.log_archive_format='archTEST_%s_%t_%r.dbf'
*.log_buffer=10485760
*.nls_length_semantics='BYTE'
*.nls_territory='AMERICA'
*.open_cursors=3000
*.optimizer_dynamic_sampling=0
*.optimizer_index_caching=90
*.optimizer_index_cost_adj=10
*.parallel_max_servers=64
*.pga_aggregate_target=2147483648
*.processes=5000
*.recyclebin='OFF'
*.remote_login_passwordfile='EXCLUSIVE'
*.resource_manager_plan=''
*.sec_case_sensitive_logon=FALSE
*.session_cached_cursors=150
*.session_max_open_files=20
*.sessions=1330
*.sga_max_size=8G
*.sga_target=0
*.shared_pool_size=2147483648
*.transactions=1500
*.undo_management='AUTO'
*.undo_retention=3600
*.undo_tablespace='UNDOTBS1'
4.创建数据库
可以使用如下的脚本来创建。
我这个因为环境限制,把文件都争取放在一个目录下,好做测试。
connect / as SYSDBA
set echo on
set timing on
spool create_db.log
startup nomount pfile=/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/initTEST.ora
create database TEST
maxinstances 1
maxlogfiles 20
maxlogmembers 5
maxdatafiles 999
character set AL32UTF8
noarchivelog
DATAFILE
'/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/SYSTEM_001.dbf' SIZE 5000M
SYSAUX DATAFILE
'/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/SYSAUX_001.dbf' SIZE 5000M
DEFAULT TEMPORARY TABLESPACE TEMP
TEMPFILE
'/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/TEMP_001.dbf' SIZE 5000M
UNDO TABLESPACE UNDOTBS1
DATAFILE
'/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/UNDOTBS_001.dbf' SIZE 5000M
LOGFILE
group 1
('/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/redo_g1_m1.dbf',
'/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/redo_g1_m2.dbf') SIZE 2048M,
group 2
('/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/redo_g2_m1.dbf',
'/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/redo_g2_m2.dbf') SIZE 2048M,
group 3
('/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/redo_g3_m1.dbf',
'/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/redo_g3_m2.dbf') SIZE 2048M
;
spool off
创建的时候,可以观察到,文件的建立顺序不是按照语句的顺序来的。
基本是按照下面的顺序创建的。
create database TEST
*
ERROR at line 1:
ORA-01092: ORACLE instance terminated. Disconnection forced
ORA-01501: CREATE DATABASE failed
ORA-01519: error while processing file '?/rdbms/admin/dtxnspc.bsq' near line 5
ORA-00604: error occurred at recursive SQL level 1
ORA-30012: undo tablespace 'UNDOTBS1' does not exist or of wrong type
Process ID: 13885
Session ID: 3781 Serial number: 3
查找了半天才发现自己在initTEST.ora的undo_tablespace指定成了‘UNDOTBS',结果创建的时候就找不到了。
5.数据字典初始化
先说一下sys和system的默认密码,sys的默认密码是change_on_install,system的是manager
sqlplus sys/change_on_install as sysdba
SQL*Plus: Release 11.2.0.2.0 Production on Fri Mar 7 17:09:01 2014
Copyright (c) 1982, 2010, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL>
脚本如下。
前两个sql脚本是必须的,
dbmspool.sql在安装gc的时候也需要,平时也可以用。
catblock.sql是和锁相关的脚本可以查看锁的一些明细。
脚本内容如下:
connect / as SYSDBA
set echo off
spool create2.log
@?/rdbms/admin/catalog.sql;
@?/rdbms/admin/catproc.sql;
@?/rdbms/admin/dbmspool.sql
@?/rdbms/admin/catblock.sql;
@?/rdbms/admin/caths.sql;
@?/rdbms/admin/owminst.plb;
@?/sqlplus/admin/plustrce.sql;
connect SYSTEM/manager
@?/sqlplus/admin/pupbld.sql;
@?/sqlplus/admin/help/hlpbld.sql helpus.sql;
spool off
6.创建额外的组件
安装JVM,XDB,statspack
--Install JVM
@?/javavm/install/initjvm.sql;
@?/xdk/admin/initxml.sql;
@?/xdk/admin/xmlja.sql;
@?/rdbms/admin/catjava.sql;
--Install XMLDB
@?/rdbms/admin/catqm.sql XDB SYSAUX TEMP
@?/rdbms/admin/catxdbj.sql;
--Install statspack
define default_tablespace='USERS' --这个表空间可以根据需要来创建
define temporary_tablespace='temp'
define perfstat_password='perfstat'
@?/rdbms/admin/spcreate
7.创建相应的表空间和数据文件
这个部分,如果已经有类似的环境,需要和其他环境的配置保持一致,可以这样做。
select dbms_metadata.get_ddl('TABLESPACE', ts.tablespace_name) from dba_tablespaces ts;
直接生成创建语句,自己简单改一下路径就可以了。
生成的语句如下:
CREATE TABLESPACE "TEST_DATA" DATAFILE
'/dbtestPT1/oracle/TEST/oracnt02/redolog_A2/redo/redo04A.log' SIZE 10737418
24,
'/dbtestPT1/oracle/TEST/oracnt04/redolog_B2/redo/redo04B.log' SIZE 10737418
24,
'/dbtestPT1/oracle/TEST/oradata01/ggs_data01.dbf' SIZE 1073741824
LOGGING ONLINE PERMANENT BLOCKSIZE 8192
EXTENT MANAGEMENT LOCAL AUTOALLOCATE DEFAULT NOCOMPRESS SEGMENT SPACE MANAGEM
ENT AUTO
8.数据库用户的创建
这个部分也可以省事些,参考其他的环境,生成一些脚本。
select dbms_metadata.get_ddl('USER',u.username) from dba_users u WHERE USERNAME like '%TEST%';
select dbms_metadata.get_granted_ddl('SYSTEM_GRANT',u.username) from dba_users u WHERE USERNAME like '%TEST%';
select dbms_metadata.get_granted_ddl('ROLE_GRANT',u.username) from dba_users u WHERE USERNAME like '%TEST%';
如果要得到比较完整的脚本,可以参考这个,也是别人分享的。贴在这。
set serveroutput on size 1000000
set verify off
undefine user_name
declare
v_name varchar2(30) := upper('&user_name');
no_grant exception;
pragma exception_init( no_grant, -31608 );
begin
dbms_metadata.set_transform_param(dbms_metadata.SESSION_TRANSFORM, 'SQLTERMINATOR', TRUE);
dbms_output.enable(1000000);
dbms_output.put_line(dbms_metadata.get_ddl('USER',v_name));
begin
dbms_output.put_line(dbms_metadata.get_granted_ddl('SYSTEM_GRANT',v_name));
exception
when no_grant then dbms_output.put_line('– No system privs granted');
end;
begin
dbms_output.put_line(dbms_metadata.get_granted_ddl('ROLE_GRANT',v_name));
exception
when no_grant then dbms_output.put_line('– No role privs granted');
end;
begin
dbms_output.put_line(dbms_metadata.get_granted_ddl('OBJECT_GRANT',v_name));
exception
when no_grant then dbms_output.put_line('– No object privs granted');
end;
begin
dbms_output.put_line(dbms_metadata.get_granted_ddl('TABLESPACE_QUOTA',v_name));
exception
when no_grant then dbms_output.put_line('– No tablespace quota specified');
end;
dbms_output.put_line(dbms_metadata.get_granted_ddl('DEFAULT_ROLE', v_name ));
exception
when others then
if SQLCODE = -31603 then dbms_output.put_line('– User does not exists');
else raise;
end if;
end;
/
9.网络,tns配置
这个部分比较常规,可以使用netca,netmgr来创建,也可以参考文档修改tnsnames.ora listener.ora
10.检查
--删除数据库
可能创建数据库的大家都接触过,删除的工作其实也不是一帆风顺的。
drop database这个命令老是看到,就是没有环境敢这么干。测试一把。
ORACLE instance started.
Total System Global Area 8551575552 bytes
Fixed Size 2243608 bytes
Variable Size 4227859432 bytes
Database Buffers 4294967296 bytes
Redo Buffers 26505216 bytes
Database mounted.
SQL> drop database TEST;
drop database TEST
*
ERROR at line 1:
ORA-00933: SQL command not properly ended
重启到mount阶段。
SQL> alter database mount exclusive;
Database altered.
SQL> drop database TEST;
drop database TEST
*
ERROR at line 1:
ORA-00933: SQL command not properly ended
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> drop database;
Database dropped.
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL>
SQL>
SQL>
SQL>
SQL> show parameter insta
SP2-0640: Not connected
SQL> exit
--创建数据库的步骤。
1.用户,文件系统,挂载点和网络的配置,内核参数配置
这个需要提前准备好。
2.ORACLE_HOME的安装和PSU
oracle_home的安装,这个也可以静默安装,不过有条件还是图形界面装,也省事。
最好打上最新的psu。省去不少潜在的问题。
3.初始化参数的考虑
有4个必备的参数。
db_name
undo_management=auto
control_files
sga_target
在这个基础上,需要考虑到一些性能参数.隐含参数
在此贴一个示例,可以在这个基础上进行修改。
*._db_block_numa=1
*._enable_NUMA_optimization=FALSE
*._kghdsidx_count=1
*._ksmg_granule_size=16777216
*._optimizer_cost_based_transformation='OFF'
*._optimizer_cost_model='IO'
*._optimizer_skip_scan_enabled=FALSE
*.aq_tm_processes=0
*.audit_file_dest='/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/oradmp/audit/'
*.compatible='11.2.0.2'
*.control_file_record_keep_time=30
*.control_files='/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/control/cntrl_1.dbf','/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/control/cntrl_2.dbf','/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/control/cntrl_3.dbf'
*.core_dump_dest='/dbtestPT1/oracle/TEST/oradmp/cdump/'
*.db_block_size=8192
*.db_cache_size=4096M
*.db_domain=''
*.db_file_multiblock_read_count=128
*.db_name='TEST'
*.db_writer_processes=4
*.deferred_segment_creation=FALSE
*.diagnostic_dest='/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/oradmp'
*.java_pool_size=500M
*.job_queue_processes=10
*.large_pool_size=500M
*.log_archive_dest='/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/arc'
*.log_archive_format='archTEST_%s_%t_%r.dbf'
*.log_buffer=10485760
*.nls_length_semantics='BYTE'
*.nls_territory='AMERICA'
*.open_cursors=3000
*.optimizer_dynamic_sampling=0
*.optimizer_index_caching=90
*.optimizer_index_cost_adj=10
*.parallel_max_servers=64
*.pga_aggregate_target=2147483648
*.processes=5000
*.recyclebin='OFF'
*.remote_login_passwordfile='EXCLUSIVE'
*.resource_manager_plan=''
*.sec_case_sensitive_logon=FALSE
*.session_cached_cursors=150
*.session_max_open_files=20
*.sessions=1330
*.sga_max_size=8G
*.sga_target=0
*.shared_pool_size=2147483648
*.transactions=1500
*.undo_management='AUTO'
*.undo_retention=3600
*.undo_tablespace='UNDOTBS1'
4.创建数据库
可以使用如下的脚本来创建。
我这个因为环境限制,把文件都争取放在一个目录下,好做测试。
connect / as SYSDBA
set echo on
set timing on
spool create_db.log
startup nomount pfile=/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/initTEST.ora
create database TEST
maxinstances 1
maxlogfiles 20
maxlogmembers 5
maxdatafiles 999
character set AL32UTF8
noarchivelog
DATAFILE
'/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/SYSTEM_001.dbf' SIZE 5000M
SYSAUX DATAFILE
'/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/SYSAUX_001.dbf' SIZE 5000M
DEFAULT TEMPORARY TABLESPACE TEMP
TEMPFILE
'/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/TEMP_001.dbf' SIZE 5000M
UNDO TABLESPACE UNDOTBS1
DATAFILE
'/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/UNDOTBS_001.dbf' SIZE 5000M
LOGFILE
group 1
('/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/redo_g1_m1.dbf',
'/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/redo_g1_m2.dbf') SIZE 2048M,
group 2
('/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/redo_g2_m1.dbf',
'/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/redo_g2_m2.dbf') SIZE 2048M,
group 3
('/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/redo_g3_m1.dbf',
'/dbtestPT1/oracle/TEST/oratmp01/temp/temp_db/redo_g3_m2.dbf') SIZE 2048M
;
spool off
创建的时候,可以观察到,文件的建立顺序不是按照语句的顺序来的。
基本是按照下面的顺序创建的。
-
- control files
- redo log files
- system tablespace
- sysaux tablespace
- undo tablespace
- temp tablespace
create database TEST
*
ERROR at line 1:
ORA-01092: ORACLE instance terminated. Disconnection forced
ORA-01501: CREATE DATABASE failed
ORA-01519: error while processing file '?/rdbms/admin/dtxnspc.bsq' near line 5
ORA-00604: error occurred at recursive SQL level 1
ORA-30012: undo tablespace 'UNDOTBS1' does not exist or of wrong type
Process ID: 13885
Session ID: 3781 Serial number: 3
查找了半天才发现自己在initTEST.ora的undo_tablespace指定成了‘UNDOTBS',结果创建的时候就找不到了。
5.数据字典初始化
先说一下sys和system的默认密码,sys的默认密码是change_on_install,system的是manager
sqlplus sys/change_on_install as sysdba
SQL*Plus: Release 11.2.0.2.0 Production on Fri Mar 7 17:09:01 2014
Copyright (c) 1982, 2010, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL>
脚本如下。
前两个sql脚本是必须的,
dbmspool.sql在安装gc的时候也需要,平时也可以用。
catblock.sql是和锁相关的脚本可以查看锁的一些明细。
脚本内容如下:
connect / as SYSDBA
set echo off
spool create2.log
@?/rdbms/admin/catalog.sql;
@?/rdbms/admin/catproc.sql;
@?/rdbms/admin/dbmspool.sql
@?/rdbms/admin/catblock.sql;
@?/rdbms/admin/caths.sql;
@?/rdbms/admin/owminst.plb;
@?/sqlplus/admin/plustrce.sql;
connect SYSTEM/manager
@?/sqlplus/admin/pupbld.sql;
@?/sqlplus/admin/help/hlpbld.sql helpus.sql;
spool off
6.创建额外的组件
安装JVM,XDB,statspack
--Install JVM
@?/javavm/install/initjvm.sql;
@?/xdk/admin/initxml.sql;
@?/xdk/admin/xmlja.sql;
@?/rdbms/admin/catjava.sql;
--Install XMLDB
@?/rdbms/admin/catqm.sql XDB SYSAUX TEMP
@?/rdbms/admin/catxdbj.sql;
--Install statspack
define default_tablespace='USERS' --这个表空间可以根据需要来创建
define temporary_tablespace='temp'
define perfstat_password='perfstat'
@?/rdbms/admin/spcreate
7.创建相应的表空间和数据文件
这个部分,如果已经有类似的环境,需要和其他环境的配置保持一致,可以这样做。
select dbms_metadata.get_ddl('TABLESPACE', ts.tablespace_name) from dba_tablespaces ts;
生成的语句如下:
CREATE TABLESPACE "TEST_DATA" DATAFILE
'/dbtestPT1/oracle/TEST/oracnt02/redolog_A2/redo/redo04A.log' SIZE 10737418
24,
'/dbtestPT1/oracle/TEST/oracnt04/redolog_B2/redo/redo04B.log' SIZE 10737418
24,
'/dbtestPT1/oracle/TEST/oradata01/ggs_data01.dbf' SIZE 1073741824
LOGGING ONLINE PERMANENT BLOCKSIZE 8192
EXTENT MANAGEMENT LOCAL AUTOALLOCATE DEFAULT NOCOMPRESS SEGMENT SPACE MANAGEM
ENT AUTO
8.数据库用户的创建
这个部分也可以省事些,参考其他的环境,生成一些脚本。
select dbms_metadata.get_ddl('USER',u.username) from dba_users u WHERE USERNAME like '%TEST%';
select dbms_metadata.get_granted_ddl('SYSTEM_GRANT',u.username) from dba_users u WHERE USERNAME like '%TEST%';
select dbms_metadata.get_granted_ddl('ROLE_GRANT',u.username) from dba_users u WHERE USERNAME like '%TEST%';
如果要得到比较完整的脚本,可以参考这个,也是别人分享的。贴在这。
set serveroutput on size 1000000
set verify off
undefine user_name
declare
v_name varchar2(30) := upper('&user_name');
no_grant exception;
pragma exception_init( no_grant, -31608 );
begin
dbms_metadata.set_transform_param(dbms_metadata.SESSION_TRANSFORM, 'SQLTERMINATOR', TRUE);
dbms_output.enable(1000000);
dbms_output.put_line(dbms_metadata.get_ddl('USER',v_name));
begin
dbms_output.put_line(dbms_metadata.get_granted_ddl('SYSTEM_GRANT',v_name));
exception
when no_grant then dbms_output.put_line('– No system privs granted');
end;
begin
dbms_output.put_line(dbms_metadata.get_granted_ddl('ROLE_GRANT',v_name));
exception
when no_grant then dbms_output.put_line('– No role privs granted');
end;
begin
dbms_output.put_line(dbms_metadata.get_granted_ddl('OBJECT_GRANT',v_name));
exception
when no_grant then dbms_output.put_line('– No object privs granted');
end;
begin
dbms_output.put_line(dbms_metadata.get_granted_ddl('TABLESPACE_QUOTA',v_name));
exception
when no_grant then dbms_output.put_line('– No tablespace quota specified');
end;
dbms_output.put_line(dbms_metadata.get_granted_ddl('DEFAULT_ROLE', v_name ));
exception
when others then
if SQLCODE = -31603 then dbms_output.put_line('– User does not exists');
else raise;
end if;
end;
/
9.网络,tns配置
这个部分比较常规,可以使用netca,netmgr来创建,也可以参考文档修改tnsnames.ora listener.ora
10.检查
--删除数据库
可能创建数据库的大家都接触过,删除的工作其实也不是一帆风顺的。
drop database这个命令老是看到,就是没有环境敢这么干。测试一把。
ORACLE instance started.
Total System Global Area 8551575552 bytes
Fixed Size 2243608 bytes
Variable Size 4227859432 bytes
Database Buffers 4294967296 bytes
Redo Buffers 26505216 bytes
Database mounted.
SQL> drop database TEST;
drop database TEST
*
ERROR at line 1:
ORA-00933: SQL command not properly ended
重启到mount阶段。
SQL> alter database mount exclusive;
Database altered.
SQL> drop database TEST;
drop database TEST
*
ERROR at line 1:
ORA-00933: SQL command not properly ended
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.
Database dropped.
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL>
SQL>
SQL>
SQL>
SQL> show parameter insta
SP2-0640: Not connected
SQL> exit
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/23718752/viewspace-1103306/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/23718752/viewspace-1103306/