如果接触数据库有些时间了,可能会碰到很多关于数据库相关的名字,比如ORACLE_SID,db_name,instance_name,db_unique_name等等。可能一下子都有些糊涂了,就一股脑儿认为都应该是一致的,其实不然。如果你接触的环境比较单一,可能会有这种错觉。
我们来简单对比一下单实例和多实例下的数据库中的这些名词。
首先来看看单实例的。
[ora11g@rac1 ~]$ echo $ORACLE_SID
TEST01
[ora11g@rac1 ~]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.3.0 Production on Mon Nov 24 03:40:26 2014
Copyright (c) 1982, 2011, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> show parameter name --查看数据库中涉及到name的参数
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_file_name_convert string
db_name string TEST01
db_unique_name string TEST01
global_names boolean FALSE
instance_name string TEST01
lock_name_space string
log_file_name_convert string
processor_group_name string
service_names string TEST01
这么看好像都是一致的,没有任何区别。
再来看看多实例的环境。
-bash-4.1$ echo $ORACLE_SID
RACDB1
-bash-4.1$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.3.0 Production on Mon Nov 24 03:42:52 2014
Copyright (c) 1982, 2011, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Real Application Clusters, Oracle Label Security, OLAP,
Data Mining and Real Application Testing options
SQL> show parameter name --查看数据库中涉及到name的参数
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_file_name_convert string
db_name string RACDB
db_unique_name string RACDB
global_names boolean FALSE
instance_name string RACDB1
lock_name_space string
log_file_name_convert string
processor_group_name string
service_names string RACDB
SQL>
从这个例子就能很明显的看出来instance_name和db_name的不同。db_name代表数据库名,这个是唯一的。长度最长为8位,根据oracle的体系结构,一个数据库可以对应多个实例,那么对应的多个实例名称就不可能都相同,在rac环境中,instance_name就可以对应多个不同的实例名,单实例环境中instance_name和db_name事一致的。
对于ORACLE_SID,这个是基于操作系统级的变量,而且是大小写敏感的。它的名字和instance_name是对应的。
既然db_name是数据库名,那么要修改就是很困难而且有一定得风险的。不能直接通过参数文件直接去修改的。需要重建控制文件。
SQL> show parameter db_name
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_name string TEST01
SQL> alter system set db_name=test01 scope=spfile;
alter system set db_name=test01 scope=spfile
*
ERROR at line 1:
ORA-32017: failure in updating SPFILE
ORA-32016: parameter "db_name" cannot be updated in SPFILE
继续来看看service_name相关的一些东西。
查看监听的情况,可以看到rac有连个监听,其中一个是通过IP漂移来动态控制的。LISTENER_SCAN1,但是不管监听的名称如何,对应的service_name都和db_name是一致的。
-bash-4.1$ ps -ef|grep tns
root 13 2 0 Nov19 ? 00:00:00 [netns]
ora11g 2333 1 0 Nov19 ? 00:00:03 /u03/ora11g/product/11.2.0/dbhome_1/bin/tnslsnr list -inherit
grid 22803 1 0 03:34 ? 00:00:00 /u04/app/11.2.0/grid/bin/tnslsnr LISTENER -inherit
grid 22807 1 0 03:34 ? 00:00:00 /u04/app/11.2.0/grid/bin/tnslsnr LISTENER_SCAN1 -inherit
grid 23134 21458 0 03:46 pts/0 00:00:00 grep tns
-bash-4.1$ tnsping racdb
TNS Ping Utility for Linux: Version 11.2.0.3.0 - Production on 24-NOV-2014 03:47:18
Copyright (c) 1997, 2011, Oracle. All rights reserved.
Used parameter files:
Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = rac-scan)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = RACDB)))
OK (10 msec)
-bash-4.1$ lsnrctl status racdb
LSNRCTL for Linux: Version 11.2.0.3.0 - Production on 24-NOV-2014 03:45:28
Copyright (c) 1991, 2011, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=rac-scan)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=RACDB)))
STATUS of the LISTENER
------------------------
Alias LISTENER_SCAN1
Version TNSLSNR for Linux: Version 11.2.0.3.0 - Production
Start Date 24-NOV-2014 03:34:31
Uptime 0 days 0 hr. 10 min. 57 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /u04/app/11.2.0/grid/network/admin/listener.ora
Listener Log File /u04/app/11.2.0/grid/log/diag/tnslsnr/rac1/listener_scan1/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=LISTENER_SCAN1)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.3.100)(PORT=1521)))
Services Summary...
Service "RACDB" has 1 instance(s).
Instance "RACDB1", status READY, has 1 handler(s) for this service...
Service "RACDBXDB" has 1 instance(s).
Instance "RACDB1", status READY, has 1 handler(s) for this service...
The command completed successfully
对于db_unique_name,这个是在dataguard中需要设置的一个参数,在10g开始启用,关于为什么有这么一个参数,其实可以这么理解,如果某个主库有一个备库,采用dataguard来实现,并且在同一台机器上,那么就可以使用db_unique_name来在dg环境中为db设置一个唯一的值。