Oracle 服务名/实例名,Service_name 和Sid的区别
Service_name 和Sid的区别
Service_name:该参数是由oracle8i引进的。在8i以前,使用SID来表示标识数据库的一个实例,但是在Oracle的并行环境中,
1、一个数据库对应多个实例:
这样就需要多个网络服务名,设置繁琐。为了方便并行环境中的设置,引进了Service_name参数,该参数对应一个数据库,而不是一个实例,
而且该参数有许多其它的好处。该参数的缺省值为Db_name. Db_domain,即等于Global_name。 sid是数据库实例的名字,每个实例各不相同。
2、一个数据库可以对应多个Service_name
以便实现更灵活的配置。该参数与SID没有直接关系,即不必Service name 必须与SID一样。
一、SID
是用来标识这个数据库内部每个实例的名字(一个数据库可以有多个实例(如RAC))
给程序开发连接用,比如连接串: jdbc:oracle:thin:@//10.121.51.22:1521:sidname //注意是:sidname,不是/sidname // linux下登录演示 [root@shdb02 ~]# su - oracle //输出当前默认的实例sid [oracle@shdb02 ~]$ echo $ORACLE_SID htsby2 //设置当前要登录的实例sid [oracle@shdb02 ~]$ export ORACLE_SID=fpm2 //输出当前的实例sid [oracle@shdb02 ~]$ echo $ORACLE_SID fpm2 [oracle@shdb02 ~]$ sqlplus / as sysdba SQL*Plus: Release 11.2.0.4.0 Production on Mon Mar 14 11:05:37 2022 Copyright (c) 1982, 2013, Oracle. All rights reserved. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP, Data Mining and Real Application Testing options //查询当前实例的数据库(数据库对应多个实例) SQL> select name from v$database; NAME --------- FPM //查询当前登录的实例sid SQL> select instance_name from v$instance; INSTANCE_NAME ---------------- fpm2 //查询实例的相关参数 SQL> show parameter instance NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ active_instance_count integer cluster_database_instances integer 2 instance_groups string instance_name string fpm2 instance_number integer 2 instance_type string RDBMS open_links_per_instance integer 4 parallel_instance_group string parallel_server_instances integer 2
二、SERVICE_NAME
是这个数据库对外的名称,外面的人要想连接我这个数据库,你就在客户端的连接串里写上service_name。
一般给客户端pl/sql用,举例: username@10.128.51.22:1521/fpm //注意是 /fpm 而不是:fpm //实例验证
[root@shdb02 ~]# su - oracle
[oracle@shdb02 ~]$ export ORACLE_SID=fpm2 //fpm2是实例sid [oracle@shdb02 ~]$ sqlplus / as sysdba SQL*Plus: Release 11.2.0.4.0 Production on Thu Mar 10 22:43:36 2022 Copyright (c) 1982, 2013, Oracle. All rights reserved. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP, Data Mining and Real Application Testing options //查询该实例的数据库名,数据库和实例是一对多的关系,也就是1个数据库对应多个实例。 SQL> select name from v$database; NAME --------- FPM //查看当前的实例sid SQL> select instance_name from v$instance; INSTANCE_NAME ---------------- fpm2 //查询当前的数据库服务名 SQL> show parameter service_name; NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ service_names string fpm
三、 Java JDBC Thin Driver 连接 Oracle有三种方法:
1、格式一: JDBC 使用服务名 ServiceName: jdbc:oracle:thin:@//<host>:<port>/<service_name> Example: jdbc:oracle:thin:@//192.168.2.1:1521:my_service_name 注意这里的格式,@后面有//, 这是与使用SID的主要区别。 这种格式是Oracle 推荐的格式,因为对于oracle集群来说,每个节点的SID 是不一样的,但是SERVICE_NAME 确可以包含所有节点。 2、格式二: JDBC 使用实例 SID: jdbc:oracle:thin:@<host>:<port>:<SID> 例子:jdbc:oracle:thin:@//10.121.51.22:1521:my_sid 格式三:Oracle JDBC 使用 TNSName: jdbc:oracle:thin:@<TNSName> Example: jdbc:oracle:thin:@tna
特别要注意 /和:的问题,对于1和2来说
查询数据库名:
[root@shdb02 ~]# su - oracle
[oracle@shdb02 ~]$ export ORACLE_SID=fpm2 //fpm2是实例名 [oracle@shdb02 ~]$ sqlplus / as sysdba SQL*Plus: Release 11.2.0.4.0 Production on Thu Mar 10 22:43:36 2022 Copyright (c) 1982, 2013, Oracle. All rights reserved. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP, Data Mining and Real Application Testing options //查询该实例的数据库名,数据库和实例是一对多的关系,也就是1个数据库对应多个实例。 SQL> select name from v$database; NAME --------- FPM SQL> select instance_name from v$instance; INSTANCE_NAME ---------------- fpm2 //查询当前的数据库服务名 SQL> show parameter service_name; NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ service_names string fpm