代码改变世界

8. DBNEWID 工具(使用nid命令修改db name及dbid)

2018-02-11 15:33  那个,我  阅读(764)  评论(0编辑  收藏  举报
 
官网:你可能手工copy一个数据库,并通过重建控制文件的方式指定了新的dbname,但是你却不能够修改dbid,dbid是一个数据库‘内部的’‘唯一标示’。因为Rman 通过dbid来区分数据库,所以你不能同时把Seed Database和Copied Database 同时放在同一个Rman Repository中,DBNEWID工具就可以解决此类问题。

1、修改dbid及dbname的影响


a、修改dbid

    等同于创建一个新的数据库,不同的是数据已经存在于数据文件。
    修改之后所有之前的备份与归档日志将不可用,因为dbid已经变更,与当前dbid不一致。
    修改之后需要使用open resetlogs打开数据库,这将会重建在线日志文件,并重置其Sequence为1.
    因此修改dbid后,你必须马上为数据库创建一份全备。

b、只修改dbname

    只修改dbname,不修改dbid,无需使用open resetlogs打开数据库。所以之前的备份和归档仍然可用。
    但是仍需要修改参数文件中的dbname、密码文件来对应新的数据库名
    如果需要恢复原先的控制文件,则需要使用原先的参数文件和密码文件。

c、同时修改dbid及dbname

    如果同时修改,则是上述两种情形的综合,修改完毕后需要open resetlogs以及修改pfile/spifle,密码文件,全备数据库。

d、关于global_names

    如果你正在一个分布式数据库系统中使用Oracle数据库,那么每个数据库都会有一个唯一的全局数据库名,DBNEWID工具不会修改
    全局库名,只能通过Alter Database 命令去修改。全局数据库名由两部分组成:db_name和db_domain,这两个参数是在数据库第     一次创建时指定的。通过如下示例修改
    
ALTER DATABASE RENAME GLOBAL_NAME TO sales.us.example.com

2、nid命令


[oracle@oracle1 ~]$ nid help=y
​
DBNEWID: Release 11.2.0.4.0 - Production on Sun Feb 11 13:42:34 2018
​
Copyright (c) 1982, 2011, Oracle and/or its affiliates.  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命令时,该程序会校验当前数据库所有数据文件以及控制文件的头部信息,校验成功后提示是否要修改。
    如果使用了输出到日志文件logfile则不会出现修改提示。
    接下来nid将使用新的dbid(或者dbname)逐个修改控制文件,数据文件(包括只读文件,正常脱机文件)的头部。
    修改成功后自动关闭数据库并退出。
    注:对于只读文件,正常脱机文件本文未作测试。其次应确保数据库处于归档状态,可正常归档以及不存在需要recover的数据文件。

3、修改步骤


  a、全备数据库,如果是热备(rman or os)应确保所有的归档日志以及联机日志可用
  b、删除dbconsole([ID 863800.1]有此要求,如用到dbconsole,应考虑按此操作)
  c、启动需要修改的数据库到mount状态(startup mount)
  d、如果使用spfile文件启动数据库,备份spfile文件到pfile用于后续修改db_name
  e、发布nid命令
nid target=sys/password                                         # 此方式是仅仅修改dbid
nid target=/ dbname=new_dbname [setname=yes]                    # / 表明连接到当前环境的sid,且使用操作系统认证
nid target=sys/password dbname=new_dbname [setname=yes]         # setname=yes 仅仅修改数据库名字,如果省略,则两者同时修改
nid target=sys/pwdd@conn_string dbname=new_dbname [setname=yes] # 使用连接串连接到远程主机并修改</span>
  f、修改Oracle 参数文件 pfile(使用先前从spfile备份的)中的参数db_name,如果仅仅修改dbid,跳过此步骤
  g、使用新的参数文件pfile启动到mount状态(如果修改了dbname,如果仅仅是dbid,则直接使用原来的pfile或spfile启动) 
  h、使用open resetlogs方式打开数据库(修改非dbid,直接打开即可)
  i、重建当前数据库的 Oralce 密码文件及将pfile文件转换成spfile文件
  j、修改相应的监听器的配值,包括listener.ora以及tnsnames.ora
  k、修改全局dbname,如果有用到的话。ALTER DATABASE RENAME GLOBAL_NAME TO <newname>.<domain>;
  l、重建dbconsole      $ emca -config dbcontrol db -repos recreate
  m、全备数据库

4、演示修改dbname 


 a. 启动到 Mount 状态
--查看当前数据库名
SQL> show parameter name
​
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
cell_offloadgroup_name               string
db_file_name_convert                 string
db_name                              string      oracle1
db_unique_name                       string      oracle1
global_names                         boolean     FALSE
instance_name                        string      oracle1
lock_name_space                      string
log_file_name_convert                string
processor_group_name                 string
service_names                        string      oracle1
​
--启动数据库到mount 状态
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount;
ORACLE instance started.
​
Total System Global Area  409194496 bytes
Fixed Size                  2253744 bytes
Variable Size             314575952 bytes
Database Buffers           88080384 bytes
Redo Buffers                4284416 bytes
Database mounted.
SQL> select dbid,name,open_mode from v$database;
​
      DBID NAME      OPEN_MODE
---------- --------- --------------------
 617378827 ORACLE1   MOUNTED 
b. nid 修改 DBNAME
[oracle@oracle1 ~]$ nid target=/ dbname=orcl
​
DBNEWID: Release 11.2.0.4.0 - Production on Sun Feb 11 13:33:43 2018
​
Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.
​
Connected to database ORACLE1 (DBID=617378827)
​
Connected to server version 11.2.0
​
Control Files in database:
    /u01/app/oracle/oradata/oracle1/control01.ctl
    /u01/app/oracle/fast_recovery_area/oracle1/control02.ctl
​
Change database ID and database name ORACLE1 to ORCL? (Y/[N]) => y
​
Proceeding with operation
Changing database ID from 617378827 to 1495594423                                                #DBID 变化
Changing database name from ORACLE1 to ORCL                                                      #DBNAME 变化
    Control File /u01/app/oracle/oradata/oracle1/control01.ctl - modified                        #修改控制文件
    Control File /u01/app/oracle/fast_recovery_area/oracle1/control02.ctl - modified
    Datafile /u01/app/oracle/oradata/oracle1/system01.db - dbid changed, wrote new name          #修改数据文件DBID/DBNAME
    Datafile /u01/app/oracle/oradata/oracle1/sysaux01.db - dbid changed, wrote new name
    Datafile /u01/app/oracle/oradata/oracle1/undotbs01.db - dbid changed, wrote new name
    Datafile /u01/app/oracle/oradata/oracle1/users01.db - dbid changed, wrote new name
    Datafile /u01/app/oracle/oradata/oracle1/example01.db - dbid changed, wrote new name
    Datafile /u01/app/oracle/oradata/oracle1/t01.db - dbid changed, wrote new name
    Datafile /u01/app/oracle/oradata/oracle1/t16.db - dbid changed, wrote new name
    Datafile /u01/app/oracle/oradata/oracle1/temp01.db - dbid changed, wrote new name
    Control File /u01/app/oracle/oradata/oracle1/control01.ctl - dbid changed, wrote new name    #修改控制文件DBID/DBNAME
    Control File /u01/app/oracle/fast_recovery_area/oracle1/control02.ctl - dbid changed, wrote new name
    Instance shut down                                                                           #关闭实例
​
Database name changed to ORCL.                                                            #数据库名已变更为orcl
Modify parameter file and generate a new password file before restarting.                 #重启前请 修改参数文件,创建新密码文件
Database ID for database ORCL changed to 1495594423.                                      #DBID 已变更为 1495594423
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.                          #数据库已关闭,请以Resetlogs方式打开
Succesfully changed database name and ID.                                                 #变更完成
DBNEWID - Completed succesfully.
c. 创建密码文件,修改参数文件 并启动数据库
[oracle@oracle1 ~]$ cd $ORACLE_HOME/dbs
[oracle@oracle1 dbs]$ orapwd help=y
Usage: orapwd file=<fname> entries=<users> force=<y/n> ignorecase=<y/n> nosysdba=<y/n>
​
  where
    file - name of password file (required),
    password - password for SYS will be prompted if not specified at command line,
    entries - maximum number of distinct DBA (optional),
    force - whether to overwrite existing file (optional),
    ignorecase - passwords are case-insensitive (optional),
    nosysdba - whether to shut out the SYSDBA logon (optional Database Vault only).
    
  There must be no spaces around the equal-to (=) character.
[oracle@oracle1 dbs]$ orapwd file=orapworcl password=oracle
[oracle@oracle1 dbs]$  sqlplus / as sysdba
​
SQL*Plus: Release 11.2.0.4.0 Production on Sun Feb 11 13:55:27 2018
​
Copyright (c) 1982, 2013, Oracle.  All rights reserved.
​
Connected to an idle instance.
​
SQL> create pfile from spfile;
​
File created.
​
SQL> quit
Disconnected
[oracle@oracle1 dbs]$ ls initoracle1.ora 
initoracle1.ora
[oracle@oracle1 dbs]$ mv initoracle1.ora initorcl.ora 
[oracle@oracle1 dbs]$ vi initorcl.ora                    #修改 pfile 中的db_name
*.db_name='orcl'    # 修该 Pfile 文件中的db_name
[oracle@oracle1 dbs]$ sqlplus / as sysdba
​
SQL*Plus: Release 11.2.0.4.0 Production on Sun Feb 11 14:07:25 2018
​
Copyright (c) 1982, 2013, Oracle.  All rights reserved.
​
Connected to an idle instance.
​
SQL> create spfile from pfile;                            # 通过pfile 创建 spfile,然后通过spfile 启动
​
File created.
SQL> startup        
ORACLE instance started.
​
Total System Global Area  409194496 bytes
Fixed Size                  2253744 bytes
Variable Size             314575952 bytes
Database Buffers           88080384 bytes
Redo Buffers                4284416 bytes
Database mounted.
ORA-01589: must use RESETLOGS or NORESETLOGS option for database open
​
SQL> alter database open RESETLOGS;                    #以 RestLogs 方式打开数据库
​
Database altered.
SQL> show parameter name                                #数据库名已变更
​
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
cell_offloadgroup_name               string
db_file_name_convert                 string
db_name                              string      orcl
db_unique_name                       string      orcl
global_names                         boolean     FALSE
instance_name                        string      orcl
lock_name_space                      string
log_file_name_convert                string
processor_group_name                 string
service_names                        string      orcl
​
SQL> select dbid,name,open_mode from v$database;
​
      DBID NAME      OPEN_MODE
---------- --------- --------------------
1495594423 ORCL      READ WRITE
SQL> set line 200 pages 200
SQL> col value for a80
SQL> col name for a30
SQL> select * from v$diag_info;                
​
   INST_ID NAME                           VALUE
---------- ------------------------------ --------------------------------------------------------------------------------
         1 Diag Enabled                   TRUE
         1 ADR Base                       /u01/app/oracle
         1 ADR Home                       /u01/app/oracle/diag/rdbms/orcl/orcl
         1 Diag Trace                     /u01/app/oracle/diag/rdbms/orcl/orcl/trace
         1 Diag Alert                     /u01/app/oracle/diag/rdbms/orcl/orcl/alert
         1 Diag Incident                  /u01/app/oracle/diag/rdbms/orcl/orcl/incident
         1 Diag Cdump                     /u01/app/oracle/diag/rdbms/orcl/orcl/cdump
         1 Health Monitor                 /u01/app/oracle/diag/rdbms/orcl/orcl/hm
         1 Default Trace File             /u01/app/oracle/diag/rdbms/orcl/orcl/trace/orcl_ora_102730.trc
         1 Active Problem Count           0
         1 Active Incident Count          011 rows selected.
# ADR 在新的位置,其他的数据文件等位置都不变,之前的ADR,参数文件,密码文件 不会自动删除,需手工删除。
# 如果配置了静态监听,则需要修改监听文件指向新的数据库
# 如果配置了TNS 文件,同样需要修改
# 如果需要使用dbconsole,需重建
​
SQL> SELECT GROUP#,SEQUENCE#,ARCHIVED,STATUS FROM V$LOG;
​
    GROUP#  SEQUENCE# ARC STATUS
---------- ---------- --- ----------------
         1          1 NO  CURRENT                # Sequence 重置为1
         2          0 YES UNUSED                 # 在线重做日志重建,置于UNUSED状态。
         3          0 YES UNUSED

 

参考: