DBNEWID是Oracle提供的一个用于修改数据库DBID和DBNAME的工具。
在引进该工具之前,如果我们想修改数据库的数据库名,必须重建控制文件。但即便如此,也无法修改该数据库的DBID。众所周知,DBID是一个数据库的唯一标识符。RMAN就是根据数据库的DBID来区分不同数据库的。
DBNEWID可解决这种问题,它提供了以下三种选择:
1> 只修改DBID
2> 只修改DBNAME
3> 同时修改DBID和DBNAME
注意:当一个数据库的DBID被修改后,它以前的备份和归档日志将不可用。并且在重新打开数据库时,需加上RESETLOGS选项,该选项将重新创建在线日志,并将它们的序列号重置为1。建议,修改完数据库的DBID后,对该库进行全备。
如果只是修改数据库的DBNAME,则以前的备份和归档日志不受影响。但是必须修改参数文件的db_name,否则,在重新打开数据库过程中,将报以下错误:ORA-01103: database name 'ORCL' in control file is not 'SZ'。表明控制文件的DBNAME和参数文件中的不匹配,需修改参数文件。同时,也需修改密码文件。
DBNEWID的使用语法如下:
nid TARGET = [username] / [password] [@service_name]
[REVERT = { YES | NO }
|DBNAME = new_db_name [SETNAME = { YES | NO }]]
[LOGFILE = logfile [APPEND = { YES | NO }] [HELP = { YES | NO }]]
其中,
TARGET输入用户名和密码,注意,该用户必须具有sysdba的权限。譬如target=sys/oracle,当然,如果我们是在操作系统本地操作的话,可用反斜杠连接,即target=/
REVERT指明一个失败的DBID操作是否需要回退。默认是NO。注意,如果数据库的DBID修改成功,该操作无法回退。该回退操作只针对失败的DBID修改操作。
DBNAME指定新的数据库名。
SETNAME默认是NO,则该数据库将同时修改DBID和DBNAME。如果指定为YES,则该数据库将只修改DBNAME
LOGFILE日志文件,记录操作信息,默认是覆盖上个日志文件。如果APPEND设为YES的话,则这次操作信息将被追加到上个日志文件中。APPEND默认是NO。
HELP用来提示DBNEWID的语法规则。默认为NO
下面,我们将演示如何修改DBID和DBNAME
一、关闭数据库并重启到mount状态
SQL> shutdown immediate --必须是一致性关闭
SQL> startup mount
二、 修改DBID和DBNAME
[oracle@node3 ~]$ nid target=sys/oracle dbname=victor --原来的dbname是orcl。
我们来看看输出
DBNEWID: Release 11.2.0.1.0 - Production on Thu Sep 25 05:00:34 2014 Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved. Connected to database ORCL (DBID=1386938297) Connected to server version 11.2.0 Control Files in database: /u01/app/oracle/oradata/sz/control01.ctl /u01/app/oracle/flash_recovery_area/sz/control02.ctl Change database ID and database name ORCL to VICTOR? (Y/[N]) => y Proceeding with operation Changing database ID from 1386938297 to 431720306 Changing database name from ORCL to VICTOR Control File /u01/app/oracle/oradata/sz/control01.ctl - modified Control File /u01/app/oracle/flash_recovery_area/sz/control02.ctl - modified Datafile /u01/app/oracle/oradata/sz/system01.db - dbid changed, wrote new name Datafile /u01/app/oracle/oradata/sz/sysaux01.db - dbid changed, wrote new name Datafile /u01/app/oracle/oradata/sz/undotbs01.db - dbid changed, wrote new name Datafile /u01/app/oracle/oradata/sz/users01.db - dbid changed, wrote new name Datafile /u01/app/oracle/oradata/sz/temp01.db - dbid changed, wrote new name Control File /u01/app/oracle/oradata/sz/control01.ctl - dbid changed, wrote new name Control File /u01/app/oracle/flash_recovery_area/sz/control02.ctl - dbid changed, wrote new name Instance shut down Database name changed to VICTOR. Modify parameter file and generate a new password file before restarting. Database ID for database VICTOR changed to 431720306. All previous backups and archived redo logs for this database are unusable. Database is not aware of previous backups and archived logs in Recovery Area. Database has been shutdown, open database with RESETLOGS option. Succesfully changed database name and ID. DBNEWID - Completed succesfully.
输出最后提示修改成功。
注意:在发出nid命令后,DBNEWID首先会校验数据文件、控制文件的有效性。如果校验通过,则DBNEWID会提示是否继续执行该操作,如上面输出中的Change database ID and database name ORCL to VICTOR? (Y/[N])。如果指明了日志文件,则不会有此提示。接着,DBNEWID会修改每个数据文件的DBID,包括离线数据文件和只读数据文件。接着关闭数据库,然后退出。
在10.1及以后版本中,该操作也会记录在告警日志中。如
*** DBNEWID utility started *** DBID will be changed from 1386938297 to new DBID of 431720306 for database ORCL DBNAME will be changed from ORCL to new DBNAME of VICTOR Starting datafile conversion Datafile conversion complete Database name changed to VICTOR. Modify parameter file and generate a new password file before restarting. Database ID for database VICTOR changed to 431720306. All previous backups and archived redo logs for this database are unusable. Database has been shutdown, open with RESETLOGS option. Succesfully changed database name and ID. *** DBNEWID utility finished succesfully ***
三、 修改参数文件
SQL> create pfile='/home/oracle/victor.pfile' from spfile;
[oracle@node3 trace]$ vim /home/oracle/victor.pfile
db_name='orcl' 修改为 db_name='victor'
四、重新启动数据库
SQL> create spfile from pfile='/home/oracle/victor.pfile';
SQL> startup mount
SQL> alter database open resetlogs;
SQL> show parameter name
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_file_name_convert string
db_name string victor
db_unique_name string victor
global_names boolean FALSE
instance_name string orcl
lock_name_space string
log_file_name_convert string
service_names string victor
可见,dbname修改成功。当然,实例名依旧是原来的orcl。
如果需要将实例名修改为victor,命令如下:
SQL> alter system set instance_name='victor' scope=spfile;
总结:该范例示范的是同时修改DBID和DBNAME。
1> 如果只是修改DBID,则上述第二步命令不用指定dbname参数,即$ nid target=sys/oracle即可。但重新开启数据库时,同样需要指定RESETLOGS子句。
2> 如何只是修改DBNAME,则上述第二步命令中需将setname指定为YES,即$ nid target=sys/oracle dbname=victor setname=yes。直接startup开库,不需指定RESETLOGS子句。
3> DBNEWID不会修改global database name。可通过以下命令修改:
SQL> alter database rename global_name to victor.being.com。其中being.com为域名。
官方文档:http://docs.oracle.com/cd/E11882_01/server.112/e22490/dbnewid.htm#SUTIL014