分类: Oracle
2012-05-24 08:47:58
引题:在实际生产环境下我们可能需要快速的复制现有的数据库,又希望新库与原库不重名,或者我们需要复制生产库用来做研发测试,如果我们采用DBCA的方式,缺点是慢并且还没有数据,就需要从新导数据,费时又麻烦。下面我就详细介绍如何修改数据库名来完成使命,采用2种方法(1.重建控制文件修改数据库名2.使用nid工具修改数据库名)
一、重建控制文件修改数据库名
步骤概述
目标:我们用原库LEO复制出新库LEO2
1.生成controlfile创建脚本并写入trace文件
SYS@LEO> alter database backup controlfile to trace;
2.执行查找trace文件语句,获取当前trace文件路径
SYS@LEO> select c.value || '/' || d.instance_name || '_ora_' || to_char(a.spid,'fm99999') || '.trc' from v$process a, v$session b, v$parameter c, v$instance d where a.addr = b.paddr and b.audsid = userenv('sessionid') and c.name = 'user_dump_dest';
Trace文件路径
C.VALUE||'/'||D.INSTANCE_NAME||'_ORA_'||TO_CHAR(A.SPID,'FM99999')||'.TRC'
--------------------------------------------------------------------------------
/u01/app/oracle/admin/LEO/udump/LEO_ora_25621.trc
3.停掉数据库
SYS@LEO> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
4.只复制数据文件、redo日志(不需要复制控制文件哦!)
从原来的/u01/app/oracle/oradata/LEO/disk1复制到
/u01/app/oracle/oradata/LEO2/disk1
5.生成新的initLEO2.orac纯文本参数文件
export ORACLE_SID=LEO
sqlplus / as sysdba 进入原库执行命令
SYS@LEO> create pfile='/u01/app/oracle/product/10.2.0/db_1/dbs/initLEO2.ora' from spfile;
6.编辑新生成的initLEO2.ora纯文本参数文件
vim initLEO2.ora
需要修改的内容:
(1)路径
*.audit_file_dest='/u01/app/oracle/admin/LEO2/adump'
*.background_dump_dest='/u01/app/oracle/admin/LEO2/bdump'
*.control_files='/u01/app/oracle/oradata/LEO2/disk1/control01.ctl','/u01/app/oracle/oradata/LEO2/disk2/control02.ctl','/u01/app/oracle/oradata/LEO2/disk3/control03.ctl'
*.core_dump_dest='/u01/app/oracle/admin/LEO2/cdump'
*.user_dump_dest='/u01/app/oracle/admin/LEO2/udump'
(2)数据库名
*.db_name='LEO2'
7.利用initLEO2.ora纯文本参数文件创建spfileLEO2.ora二进制参数文件
export ORACLE_SID=LEO2
sqlplus / as sysdba
SYS@LEO2> create spfile from pfile; 创建spfile
8.数据库启动到nomount状态,加载参数文件
SYS@LEO2> startup nomount
ORACLE instance started.
Total System Global Area 314572800 bytes
Fixed Size 1219184 bytes
Variable Size 96470416 bytes
Database Buffers 213909504 bytes
Redo Buffers 2973696 bytes
SYS@LEO2> show parameter name
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_file_name_convert string
db_name string LEO2
db_unique_name string LEO2
global_names boolean FALSE
instance_name string LEO2
lock_name_space string
log_file_name_convert string
service_names string LEO2
SYS@LEO2> select instance_name from v$instance;
INSTANCE_NAME
----------------
LEO2
9.删除原库控制文件(如果拷贝的时候包括了控制文件的话)
路径:
/u01/app/oracle/oradata/LEO2/disk1/control01.ctl,
/u01/app/oracle/oradata/LEO2/disk2/control02.ctl,
/u01/app/oracle/oradata/LEO2/disk3/control03.ctl
10.修改/u01/app/oracle/admin/LEO/udump/leo_ora_25621.trc文件的内容
vim leo_ora_25621.trc
把内容拷贝到vim test.sql临时文件中修改
定位-- Set #2. RESETLOGS case 位置
(1)把CREATE CONTROLFILE REUSE DATABASE "LEO" RESETLOGS NOARCHIVELOG
修改成
CREATE CONTROLFILE SET DATABASE "LEO2" RESETLOGS NOARCHIVELOG
(2)把在线日志文件和数据文件的路径中的“PROD”替换成“LEO2”,修改路径
例子:/u01/app/oracle/oradata/PROD/disk1/ 替换
/u01/app/oracle/oradata/LEO2/disk1/
使用:%s#PROD#LEO2#方法替换所有“PROD”
最后的完成版并执行此脚本,就完成了控制文件的创建
CREATE CONTROLFILE SET DATABASE "LEO2" RESETLOGS NOARCHIVELOG
MAXLOGFILES 30
MAXLOGMEMBERS 5
MAXDATAFILES 100
MAXINSTANCES 1
MAXLOGHISTORY 292
LOGFILE
GROUP 1 (
'/u01/app/oracle/oradata/LEO2/disk1/redo01.log',
'/u01/app/oracle/oradata/LEO2/disk2/redo01_2.log'
) SIZE 100M,
GROUP 2 (
'/u01/app/oracle/oradata/LEO2/disk1/redo02.log',
'/u01/app/oracle/oradata/LEO2/disk2/redo02_2.log'
) SIZE 100M,
GROUP 3 (
'/u01/app/oracle/oradata/LEO2/disk1/redo03.log',
'/u01/app/oracle/oradata/LEO2/disk2/redo03_2.log'
) SIZE 100M,
GROUP 4 (
'/u01/app/oracle/oradata/LEO2/disk1/redo04_1.log',
'/u01/app/oracle/oradata/LEO2/disk2/redo04_2.log'
) SIZE 100M,
GROUP 5 (
'/u01/app/oracle/oradata/LEO2/disk1/redo05_1.log',
'/u01/app/oracle/oradata/LEO2/disk2/redo05_2.log'
) SIZE 100M
DATAFILE
'/u01/app/oracle/oradata/LEO2/disk1/system01.dbf',
'/u01/app/oracle/oradata/LEO2/disk1/undotbs01.dbf',
'/u01/app/oracle/oradata/LEO2/disk1/sysaux01.dbf',
'/u01/app/oracle/oradata/LEO2/disk1/example01.dbf',
'/u01/app/oracle/oradata/LEO2/disk1/indx01.dbf',
'/u01/app/oracle/oradata/LEO2/disk1/tools01.dbf',
'/u01/app/oracle/oradata/LEO2/disk1/user01.dbf',
'/u01/app/oracle/oradata/LEO2/disk1/oltp01.dbf'
CHARACTER SET US7ASCII
;
11.把数据库启动到mount状态,在启动到open状态
注:必须添加resetlogs选项打开数据库,resetlogs所有以前的备份和归档日志均不可用
注:当我们执行完控制文件创建脚本后,数据库就已经是mount状态了。
SYS@LEO2> select open_mode,log_mode,name,dbid,db_unique_name from v$database;
OPEN_MODE LOG_MODE NAME DBID DB_UNIQUE_NAME
---------- ------------ --------- ---------- -----------------------------------------------------------------------
MOUNTED NOARCHIVELOG LEO2 1558123773 LEO2
SYS@LEO2> alter database open resetlogs;
SYS@LEO2> select file_name,file_id,tablespace_name,status from dba_data_files;
FILE_NAME FILE_ID TABLESPACE_NAME STATUS
-------------------------------------------------- ---------- --------
/u01/app/oracle/oradata/LEO2/disk1/oltp01.dbf 8 OLTP AVAILABLE
/u01/app/oracle/oradata/LEO2/disk1/user01.dbf 7 USERS AVAILABLE
/u01/app/oracle/oradata/LEO2/disk1/tools01.dbf 6 TOOLS AVAILABLE
/u01/app/oracle/oradata/LEO2/disk1/indx01.dbf 5 INDX AVAILABLE
/u01/app/oracle/oradata/LEO2/disk1/example01.dbf 4 EXAMPLE AVAILABLE
/u01/app/oracle/oradata/LEO2/disk1/sysaux01.dbf 3 SYSAUX AVAILABLE
/u01/app/oracle/oradata/LEO2/disk1/undotbs01.dbf 2 UNDOTBS AVAILABLE
/u01/app/oracle/oradata/LEO2/disk1/system01.dbf 1 SYSTEM AVAILABLE
11.新建LEO2密码文件
orapwd file=orapwLEO2 password=oracle entries=30
二、使用nid工具修改数据库名
Nid是oracle软件提供的一个小工具,可以修改数据库名称,位置:$ORACLE_HOME/bin/
优点:可以不用重建控制文件就修改数据库名
步骤概述
目标:我们用原库LEO2修改成新库LEO3,还用原来的控制文件、数据文件、在线日志
1.验证当前数据库名,实例名,服务名参数
SYS@LEO2> show parameter name
NAME TYPE VALUE
------------------------------------ ----------- --------------------------------------------
db_file_name_convert string
db_name string LEO2
db_unique_name string LEO2
global_names boolean FALSE
instance_name string LEO2
lock_name_space string
log_file_name_convert string
service_names string LEO2
SYS@LEO2> select open_mode,log_mode,name,dbid,db_unique_name from v$database;
OPEN_MODE LOG_MODE NAME DBID DB_UNIQUE_NAME
---------- ------------ --------- ---------- ----------------------------------------------------------------------
READ WRITE NOARCHIVELOG LEO2 1558123773 LEO2
2.停掉数据库
SYS@LEO2> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
3.数据库启动到mount状态,nid工具在mount状态下才能用,直接修改控制文件内容
export ORACLE_SID=LEO2
sqlplus / as sysdba
SYS@LEO2> startup mount
ORACLE instance started.
Total System Global Area 314572800 bytes
Fixed Size 1219184 bytes
Variable Size 96470416 bytes
Database Buffers 213909504 bytes
Redo Buffers 2973696 bytes
Database mounted.
SYS@LEO2> exit
nid help=y
DBNEWID: Release 10.2.0.1.0 - Production on Mon May 21 16:57:06 2012
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Keyword Description (Default)
-----------------------------------------------------------------
TARGET Username/Password (NONE)
DBNAME New database name (NONE)
LOGFILE Output Log (NONE)
REVERT Revert failed change NO
SETNAME Set a new database name only NO
APPEND Append to output log NO
HELP Displays these messages NO
nid target=sys/oracle dbname=LEO3
DBNEWID: Release 10.2.0.1.0 - Production on Mon May 21 16:59:10 2012
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to database LEO2 (DBID=1558123773) --已连接当前数据库名
Connected to server version 10.2.0 --已连接服务器版本
Control Files in database: --控制文件路径
/u01/app/oracle/oradata/LEO2/disk1/control01.ctl
/u01/app/oracle/oradata/LEO2/disk2/control02.ctl
/u01/app/oracle/oradata/LEO2/disk3/control03.ctl
--是否同意修改数据库ID和数据库名
Change database ID and database name LEO2 to LEO3? (Y/[N]) =>y
Proceeding with operation
Changing database ID from 1558123773 to 1231291551 --修改数据库ID
Changing database name from LEO2 to LEO3 --修改数据库名
Control File /u01/app/oracle/oradata/LEO2/disk1/control01.ctl – modified--控制文件已修改
Control File /u01/app/oracle/oradata/LEO2/disk2/control02.ctl – modified
Control File /u01/app/oracle/oradata/LEO2/disk3/control03.ctl – modified
Datafile /u01/app/oracle/oradata/LEO2/disk1/system01.dbf - dbid changed, wrote new name
Datafile /u01/app/oracle/oradata/LEO2/disk1/undotbs01.dbf - dbid changed, wrote new name
Datafile /u01/app/oracle/oradata/LEO2/disk1/sysaux01.dbf - dbid changed, wrote new name
Datafile /u01/app/oracle/oradata/LEO2/disk1/example01.dbf - dbid changed, wrote new name
Datafile /u01/app/oracle/oradata/LEO2/disk1/indx01.dbf - dbid changed, wrote new name
Datafile /u01/app/oracle/oradata/LEO2/disk1/tools01.dbf - dbid changed, wrote new name
Datafile /u01/app/oracle/oradata/LEO2/disk1/user01.dbf - dbid changed, wrote new name
Datafile /u01/app/oracle/oradata/LEO2/disk1/oltp01.dbf - dbid changed, wrote new name
Control File /u01/app/oracle/oradata/LEO2/disk1/control01.ctl - dbid changed, wrote new name
Control File /u01/app/oracle/oradata/LEO2/disk2/control02.ctl - dbid changed, wrote new name
Control File /u01/app/oracle/oradata/LEO2/disk3/control03.ctl - dbid changed, wrote new name
Instance shut down --当前数据库已经关闭
Database name changed to LEO3. –数据库名已经修改完毕
Modify parameter file and generate a new password file before restarting.
--修改参数文件和在重启之前生成新的密码文件
Database ID for database LEO3 changed to 1231291551.
--数据库ID已经修改为:1231291551.
All previous backups and archived redo logs for this database are unusable.
--以前的备份和归档日志均不可用
Database has been shutdown, open database with RESETLOGS option.
--数据库已关闭,必须使用RESETLOGS选项打开数据库
Succesfully changed database name and ID.
--已成功修改数据库名和数据库ID
DBNEWID - Completed succesfully.
-- DBNEWID成功完成
4.生成新的initLEO3.orac纯文本参数文件并修改数据库名和路径
SYS@LEO2> create pfile='/u01/app/oracle/product/10.2.0/db_1/dbs/initLEO3.ora' from spfile;
vim initLEO3.ora
(1)路径
*.audit_file_dest='/u01/app/oracle/admin/LEO3/adump'
*.background_dump_dest='/u01/app/oracle/admin/LEO3/bdump'
*.control_files='/u01/app/oracle/oradata/LEO3/disk1/control01.ctl','/u01/app/oracle/oradata/LEO2/disk2/control02.ctl','/u01/app/oracle/oradata/LEO3/disk3/control03.ctl'
*.core_dump_dest='/u01/app/oracle/admin/LEO3/cdump'
*.user_dump_dest='/u01/app/oracle/admin/LEO3/udump'
(2)数据库名
*.db_name='LEO3'
5.利用initLEO3.ora纯文本参数文件创建spfileLEO3.ora二进制参数文件
SYS@LEO2> create spfile='/u01/app/oracle/product/10.2.0/db_1/dbs/spfileLEO3.ora' from pfile='/u01/app/oracle/product/10.2.0/db_1/dbs/initLEO3.ora';
6.创建转储目录
mkdir -p $ORACLE_BASE/admin/LEO3/adump
mkdir -p $ORACLE_BASE/admin/LEO3/bdump
mkdir -p $ORACLE_BASE/admin/LEO3/cdump
mkdir -p $ORACLE_BASE/admin/LEO3/udump
7.直接启动数据库到open状态
注:必须添加resetlogs选项打开数据库,resetlogs所有以前的备份和归档日志均不可用
export ORACLE_SID=LEO3
sqlplus / as sysdba
SYS@LEO3> startup
ORACLE instance started.
Total System Global Area 314572800 bytes
Fixed Size 1219184 bytes
Variable Size 96470416 bytes
Database Buffers 213909504 bytes
Redo Buffers 2973696 bytes
Database mounted.
ORA-01589: must use RESETLOGS or NORESETLOGS option for database open
必须使用RESETLOGS和NORESETLOGS选项打开数据库
SYS@LEO3> alter database open resetlogs;
8.检查当前数据库名,实例名,服务名参数
SYS@LEO3> show parameter name
NAME TYPE VALUE
------------------------------------ ----------- ----------------------------------------------------
db_file_name_convert string
db_name string LEO3
db_unique_name string LEO3
global_names boolean FALSE
instance_name string LEO3
lock_name_space string
log_file_name_convert string
service_names string LEO3
SYS@LEO3> select open_mode,log_mode,name,dbid,db_unique_name from v$database;
OPEN_MODE LOG_MODE NAME DBID DB_UNIQUE_NAME
---------- ------------ --------- ---------- ------------------------------
READ WRITE NOARCHIVELOG LEO3 1231291551 LEO3
SYS@LEO3> select instance_name,status from v$instance;
INSTANCE_NAME STATUS
---------------- ------------ ------------
LEO3 OPEN
9.新建LEO3密码文件
orapwd file=orapwLEO3 password=oracle entries=30
10.修改listener.ora和tnsnames.ora文件内容
cd /u01/app/oracle/product/10.2.0/db_1/network/admin
修改vim listener.ora
LISTENER=
(DESCRIPTION=
(ADDRESS_LIST=
(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.1.10)(PORT=1521))
(ADDRESS=(PROTOCOL=ipc)(KEY=extproc))))
SID_LIST_LISTENER=
(SID_LIST=
(SID_DESC=
(GLOBAL_DBNAME=LEO3)
(ORACLE_HOME=/u01/app/oracle/product/10.2.0/db_1)
(SID_NAME=LEO3))
(SID_DESC=
(SID_NAME=pl***tproc)
(ORACLE_HOME=/u01/app/oracle/product/10.2.0/db_1)
(PROGRAM=extproc)))
修改vim tnsnames.ora
LEO3=
(description =
(address=(protocol=tcp)(host=192.168.1.10)(port=1521))
(connect_data=
(server = dedicated)
(service_name = LEO3)
)
)
重启监听
lsnrctl reload listener
LSNRCTL for Linux: Version 10.2.0.1.0 - Production on 21-MAY-2012 17:34:29
Copyright (c) 1991, 2005, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=secdb1)(PORT=1521)))
The command completed successfully
测试监听
sqlplus system/oracle@LEO3
SQL*Plus: Release 10.2.0.1.0 - Production on Mon May 21 17:35:25 2012
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
SYSTEM@LEO3>
小结:到此所有的数据库名、实例名、服务名已经都被修改过来了,有了这2种方法我们可以快速的建立起带数据的新数据库。支持我们的研发和测试
注意:参数文件修改
控制文件修改
密码文件修改
监听文件修改
数据文件和日志文件根据需要进行创建
from