修改oracle实例名(sid)和数据库名(db_name)

有时我们需要修改数据库的sid和dbname,除了使用rman进行备份恢复之外,也可以通过手工方式修改,主要由两个主要过程完成:
1、修改实例名(SID)
2、修改数据库名(dbname)

下面演示将数据库sid和dbname由orcl修改为cnhtm的过程:

 


1、修改实例名(sid)

1.1、检查原来的数据库实例名(sid)

oracle@oracle[/home/oracle]> echo $ORACLE_SID
orcl
oracle@oracle[/home/oracle]> sqlplus / as sysdba

SQL*Plus: Release 10.2.0.1.0 - Production on Sun Dec 20 11:14:49 2009

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

sys@ORCL> select instance from v$thread;

INSTANCE
--------------------------------------------------------------------------------
orcl

1.2、关闭数据库

注意不能用shutdown abort,只能是shutdown immediate或shutdown normal

sys@ORCL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
sys@ORCL> exit
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

1.3、修改oracle用户的ORACLE_SID环境变量,如由orcl修改为cnhtm

oracle@oracle[/home/oracle]> cat ~/.bash_profile|grep -i sid
ORACLE_SID=cnhtm
export ORACLE_BASE ORACLE_HOME ORACLE_SID PATH

1.4、修改/etc/oratab文件,将sid名由旧的修改为新的,如从orcl修改为cnhtm

oracle@oracle[/home/oracle]> cat /etc/oratab

......

cnhtm:/oracle/app/10.1:Y
+ASM:/oracle/app/10.1:Y

1.5、进入到$ORACLE_HOME/dbs目录

将所有文件名中包含原来的sid的修改为对应的新sid的
如我对如下文件修改为其后对应的文件

hc_orcl.dat->hc_cnhtm.dat
lkORCL->lkCNHTM
orapworcl->orapwcnhtm
snapcf_orcl.f->snapcf_cnhtm.f
spfileorcl.ora->spfilecnhtm.ora

1.6、使新修改的ORACLE_SID环境变量生效

oracle@oracle[/oracle/app/10.1/dbs]> . ~/.bash_profile
oracle@oracle[/oracle/app/10.1/dbs]> echo $ORACLE_SID
cnhtm

1.7、重建口令文件

因为口令文件改名后不能在新实例中使用,所以重建

oracle@oracle[/oracle/app/10.1/dbs]> orapwd file=$ORACLE_HOME/dbs/orapw$ORACLE_SID password=oracle entries=5 force=y
oracle@oracle[/oracle/app/10.1/dbs]> ls -lrt orapw*
-rw-r----- 1 oracle oinstall 2048 Dec 20 11:27 orapwcnhtm

1.8、启动数据库

oracle@oracle[/oracle/app/10.1/dbs]> sqlplus / as sysdba

SQL*Plus: Release 10.2.0.1.0 - Production on Sun Dec 20 11:29:53 2009

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

Connected to an idle instance.

idle> startup
ORACLE instance started.

Total System Global Area 167772160 bytes
Fixed Size 1218292 bytes
Variable Size 62916876 bytes
Database Buffers 96468992 bytes
Redo Buffers 7168000 bytes
Database mounted.
Database opened.

1.9、检查数据库实例名

通过如下语句检查数据库实例名,发现实例名已经由orcl变成cnhtm

idle> select instance from v$thread;

INSTANCE
--------------------------------------------------------------------------------
cnhtm

2、修改数据库名(dbname)

虽然已经修改过了实例名(sid),但是数据库的名称(dbname还是原来的名称orcl)

idle> conn / as sysdba
Connected.
sys@ORCL> show parameter name

NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_file_name_convert string
db_name string orcl
db_unique_name string orcl
global_names boolean FALSE
instance_name string cnhtm
lock_name_space string
log_file_name_convert string
service_names string orcl

可以通过如下步骤修改数据库名(dbname)

2.1、首先切换一下在线日志,使数据库做checkpoint

sys@ORCL> alter system archive log current;

System altered.

2.2、生成重建控制文件的脚本

sys@ORCL> alter database backup controlfile to trace resetlogs;

Database altered.

2.3、关闭数据库,需要干净关闭,不能shutdown abort

sys@ORCL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
sys@ORCL> exit
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

2.4、进入$ORACLE_BASE/admin/<sid>/udump目录中,找到最新生成的trc文件,这就是重建控制文件的脚本

oracle@oracle[/oracle/admin/orcl/udump]> ls -lrt
total 2608
-rw-r----- 1 oracle oinstall 577 Nov 7 13:37 orcl_ora_12020.trc

......

-rw-r----- 1 oracle oinstall 4407 Dec 20 11:36 cnhtm_ora_7789.trc

2.5、将找到的trc文件复制一份,并命名为ccf.sql

oracle@oracle[/oracle/admin/orcl/udump]> cp cnhtm_ora_7789.trc ccf.sql

2.6、修改ccf.sql

查找STARTUP NOMOUNT语句,将这一行上面的所有行都删除
查找所有以--开始的行,把这些行删除
查找所有的orcl修改为cnhtm,所有的ORCL修改为CNHTM
找到CREATE CONTROLFILE REUSE DATABASE...语句,将其中的REUSE修改为SET
找到RECOVER DATABASE USING BACKUP CONTROLFILE语句,将其用双横线(--)注释掉
如果有精力,可以修改这个脚本中的datafile和logfile部分使用新的文件名称,其实这部分不修改也可以,我为了测试的目的进行了修改,修改后要记得去重命名数据文件和log文件,将对应的数据文件和log文件与这里的名称相对应

我修改后的ccf.sql文件内容如下

STARTUP NOMOUNT
CREATE CONTROLFILE set DATABASE "cnhtm" RESETLOGS ARCHIVELOG
MAXLOGFILES 16
MAXLOGMEMBERS 3
MAXDATAFILES 100
MAXINSTANCES 8
MAXLOGHISTORY 292
LOGFILE
GROUP 1 '+DATA/cnhtm/onlinelog/group_1.LOG' SIZE 100M,
GROUP 2 '+DATA/cnhtm/onlinelog/group_2.LOG' SIZE 100M,
GROUP 3 '+DATA/cnhtm/onlinelog/group_3.LOG' SIZE 100M,
GROUP 4 '+DATA/cnhtm/onlinelog/group_4.LOG' SIZE 100M
DATAFILE
'+DATA/cnhtm/datafile/system01.DBF',
'+DATA/cnhtm/datafile/undotbs101.DBF',
'+DATA/cnhtm/datafile/sysaux01.DBF',
'+DATA/cnhtm/datafile/users01.DBF',
'+DATA/cnhtm/datafile/example01.DBF',
'+DATA/cnhtm/datafile/tbs_lmt01.DBF',
'+DATA/cnhtm/datafile/tbs_lmt_201.DBF',
'+DATA/cnhtm/datafile/tbs_lmt_301.DBF'
CHARACTER SET ZHS16GBK
;
--RECOVER DATABASE USING BACKUP CONTROLFILE;
ALTER DATABASE OPEN RESETLOGS;
ALTER TABLESPACE TEMP ADD TEMPFILE '+DATA/cnhtm/tempfile/temp.269.705923003'
SIZE 104857600 REUSE AUTOEXTEND ON NEXT 104857600 MAXSIZE 32767M;

2.7、修改数据文件、在线日志文件名

如果上一步修改了ccf.sql文件中的datafile和logfile段的文件名,这里要将这些文件名重命令为与其一致。
因为我的实验环境使用了ASM,在Oracle 10.2中ASM中不能重命名和复制文件,我采用了创建别名的方式,操作如下:

oracle@oracle[/oracle/admin/cnhtm/udump]> export ORACLE_SID=+ASM
oracle@oracle[/oracle/admin/cnhtm/udump]> sqlplus / as sysdba

SQL*Plus: Release 10.2.0.1.0 - Production on Sun Dec 20 12:08:52 2009

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

idle> alter diskgroup data add directory '+data/CNHTM';

Diskgroup altered.

idle> alter diskgroup data add directory '+data/CNHTM/DATAFILE';

Diskgroup altered.

idle> alter diskgroup data add alias
2 '+data/CNHTM/DATAFILE/EXAMPLE01.DBF'
3 for
4 '+data/ORCL/DATAFILE/EXAMPLE.261.705922745';

Diskgroup altered.

......

idle> alter diskgroup data add alias
2 '+data/CNHTM/DATAFILE/USERS01.DBF'
3 for
4 '+data/ORCL/DATAFILE/USERS.266.705922777';

Diskgroup altered.

idle> alter diskgroup data add directory '+data/CNHTM/ONLINELOG';

Diskgroup altered.

......

idle> alter diskgroup data add alias
2 '+data/CNHTM/ONLINELOG/group_4.LOG'
3 for
4 '+data/ORCL/ONLINELOG/group_7.273.705923695';

Diskgroup altered.

2.8、如果归档日志目录名中包含sid,那么修改归档目录名

我的测试环境,归档目录使用的是flash_recovery_area,所以需要将这个目录中的ORCL目录重命名为CNHTM

2.9、使用spfile生成pfile

注意这里没有启动数据库,只是链接到idle状态

oracle@oracle[/home/oracle]> export ORACLE_SID=cnhtm
oracle@oracle[/home/oracle]> sqlplus / as sysdba

SQL*Plus: Release 10.2.0.1.0 - Production on Sun Dec 20 11:49:20 2009

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

Connected to an idle instance.

idle> create pfile='?/dbs/initcnhtm.ora' from spfile;

File created.

idle> exit
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

2.10、进入$ORACLE_HOME/dbs目录

2.11、编辑initcnhtm.ora文件

搜索所有的orcl,重命令为cnhtm,搜索所有的ORCL,重命名为CNHTM

2.12、删除控制文件

将原来的控制文件删除或重命名
控制文件的位置名称可以通过查看2.11步骤中的*.control_files来确定
如果控制文件在ASM中,可以进入asmcmd命令,然后用rm命令删除

2.13、进入$ORACLE_BASE/admin目录

将orcl目录重命名为cnhtm

2.14、使用修改过的pfile生成spfile

oracle@oracle[/oracle/admin/cnhtm/udump]> sqlplus / as sysdba

SQL*Plus: Release 10.2.0.1.0 - Production on Sun Dec 20 11:59:56 2009

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

Connected to an idle instance.

idle> create spfile from pfile='?/dbs/initcnhtm.ora';

File created.

2.15、调用2.6步骤修改好的ccf.sql

idle> @/oracle/admin/cnhtm/udump/ccf.sql
ORACLE instance started.

Total System Global Area 167772160 bytes
Fixed Size 1218292 bytes
Variable Size 67111180 bytes
Database Buffers 92274688 bytes
Redo Buffers 7168000 bytes

Control file created.


Database altered.


Database altered.


Tablespace altered.

2.16、检查数据库状态

idle> conn / as sysdba
Connected.
sys@CNHTM>select open_mode from v$database;
OPEN_MODE
----------
READ WRITE

sys@CNHTM> show parameter name

NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_file_name_convert string
db_name string cnhtm
db_unique_name string cnhtm
global_names boolean FALSE
instance_name string cnhtm
lock_name_space string
log_file_name_convert string
service_names string cnhtm

发现数据库名(db_name)已经修改为cnhtm

当前是第一页