Home Database Mysql Tutorial 剖析Oracle的网络结构轻松解决连接问题

剖析Oracle的网络结构轻松解决连接问题

Jun 07, 2016 pm 03:45 PM
oracle Analyze structure network solve easy connect question

剖析Oracle的网络结构轻松解决连接问题 发布时间:2007.09.26 05:01 来源:赛迪网 作者:xiaoqiao Oracle的网络结构: 首先我们来看以下三个配置文件: listener.ora、sqlnet.ora、tnsnames.ora (注释:都放在$ORACLE_HOME\network\admin目录下) 三个文件的

剖析Oracle的网络结构轻松解决连接问题

 
发布时间:2007.09.26 05:01     来源:赛迪网    作者:xiaoqiao

Oracle的网络结构:

首先我们来看以下三个配置文件:

listener.ora、sqlnet.ora、tnsnames.ora  
(注释:都放在$ORACLE_HOME\network\admin目录下)
Copy after login

三个文件的作用和使用

#--- sqlnet.ora-----作用类似于linux或者其他unix的nsswitch.conf文件,通过这个文件来决定怎么样找一个连接中所出现的连接字符串。

例:在客户端输入

sqlplus sys/oracle@orcl
Copy after login

假如我的sqlnet.ora如下所示:

SQLNET.AUTHENTICATION_SERVICES= (NTS) 
NAMES.DIRECTORY_PATH= (TNSNAMES,HOSTNAME)
Copy after login

那么,客户端就会首先在tnsnames.ora文件中找orcl的记录.如果没有相应的记录则尝试把orcl当作一个主机名,通过网络的途径去解析它的ip地址然后去连接这个ip上GLOBAL_DBNAME=orcl这个实例,当然我这里orcl并不是一个主机名,示例:

NAMES.DIRECTORY_PATH= (TNSNAMES)
Copy after login

那么客户端就只会从tnsnames.ora查找orcl的记录,括号中还有其他选项,如LDAP等并不常用。

#--- Tnsnames.ora------这个文件类似于unix 的hosts文件,提供的tnsname到主机名或者ip的对应,只有当sqlnet.ora中类似

NAMES.DIRECTORY_PATH= (TNSNAMES) 这样,也就是客户端解析连接字符串的顺序中有TNSNAMES是,才会尝试使用这个文件。

例子中有两个,ORCL 对应的本机,SALES对应的另外一个IP地址,里边还定义了使用主用服务器还是共享服务器模式进行连接。

#你所要连接的时候输入得TNSNAME

ORCL = 
  (DESCRIPTION = 
(ADDRESS_LIST = 
#下面是这个TNSNAME对应的主机,端口,协议 
      (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521)) 
    ) 
(CONNECT_DATA =
Copy after login
#使用专用服务器模式去连接需要跟服务器的模式匹配,
如果没有就根据服务器的模式 
#自动调节 
(SERVER = DEDICATED) 
#对应service_name,SQLPLUS>;show parameter service_name; 
#进行查看 
      (SERVICE_NAME = orcl) 
    ) 
  ) 
#下面这个类似 
SALES = 
  (DESCRIPTION = 
    (ADDRESS_LIST = 
      (ADDRESS = (PROTOCOL = TCP)
(HOST = 192.168.188.219)(PORT = 1521)) 
    ) 
    (CONNECT_DATA = 
      (SERVER = DEDICATED) 
      (SERVICE_NAME = sales) 
    ) 
  ) 


#---------------------- 
客户端完了我们来看服务器端 
listener.ora------listener监听器进程的配置文件
Copy after login


关于listener进程就不多说了,接受远程对数据库的接入申请并转交给oracle的服务器进程。所以如果不是使用的远程的连接,listener进程就不是必需的,同样的如果关闭listener进程并不会影响已经存在的数据库连接。

Listener.ora文件的例子

#listener.ora Network Configuration File: 
#E:\oracle\product\10.1.0\Db_2\NETWORK\ADMIN\listener.ora 
# Generated by Oracle configuration tools.
Copy after login

#下面定义LISTENER进程为哪个实例提供服务

#这里是ORCL,并且它对应的ORACLE_HOME和GLOBAL_DBNAME

#其中GLOBAL_DBNAME不是必需的除非使用HOSTNAME做数据库连接

SID_LIST_LISTENER = 
  (SID_LIST = 
    (SID_DESC = 
      (GLOBAL_DBNAME = boway) 
      (ORACLE_HOME = E:\oracle\product\10.1.0\Db_2) 
      (SID_NAME = ORCL) 
    ) 
  )
Copy after login

#监听器的名字,一台数据库可以有不止一个监听器

#再向下面是监听器监听的协议,ip,端口等,这里使用的tcp1521端口,并且使#用的是主机名

LISTENER =         
  (DESCRIPTION = 
    (ADDRESS = (PROTOCOL = TCP)(HOST = boway)(PORT = 1521)) 
  )
Copy after login

上面的例子是一个最简单的例子,但也是最普遍的。一个listener进程为一个instance(SID)提供服务。

监听器的操作命令

$ORACLE_HOME/bin/lsnrctl start,其他诸如stop,status等。具体敲完一个lsnrctl后看帮助。

上面说到的三个文件都可以通过图形的配置工具来完成配置

$ORACLE_HOME/netca 向导形式的

$ORACLE_HOME/netmgr

本人比较习惯netmgr,

profile 配置的是sqlnet.ora也就是名称解析的方式

service name 配置的是tnsnames.ora文件

listeners配置的是listener.ora文件,即监听器进程

具体的配置可以尝试一下然后来看一下配置文件。

这样一来总体结构就有了,是

当你输入sqlplus sys/oracle@orcl的时候

1. 查询sqlnet.ora看看名称的解析方式,发现是TNSNAME

2. 则查询tnsnames.ora文件,从里边找orcl的记录,并且找到主机名,端口和service_name

3. 如果listener进程没有问题的话,建立与listener进程的连接。

4. 根据不同的服务器模式如专用服务器模式或者共享服务器模式,listener采取接下去的动作。默认是专用服务器模式,没有问题的话客户端就连接上了数据库的server process。

5. 这时候网络连接已经建立,listener进程的历史使命也就完成了。

#---------------

几种连接用到的命令形式

1.sqlplus / as sysdba 这是典型的操作系统认证,不需要listener进程

2.sqlplus sys/oracle 这种连接方式只能连接本机数据库,同样不需要listener进程

3.sqlplus sys/oracle@orcl 这种方式需要listener进程处于可用状态。最普遍的通过网络连接。

以上连接方式使用sys用户或者其他通过密码文件验证的用户都不需要数据库处于可用状态,操作系统认证也不需要数据库可用,普通用户因为是数据库认证,所以数据库必需处于open状态。

然后就是

#-------------

平时排错可能会用到的

1.lsnrctl status查看服务器端listener进程的状态

LSNRCTL>; help 
The following operations are available 
An asterisk (*) denotes a 
modifier or extended command: 
start         stop      status 
services      version   reload 
save_config   trace     change_password 
quit          exit      set* 
show* 
LSNRCTL>; status 
 :em11:
Copy after login

2.tnsping 查看客户端sqlnet.ora和tnsname.ora文件的配置正确与否,及对应的服务器的listener进程的状态。

C:\>;tnsping orcl 

TNS Ping Utility for 32-bit 
Windows: Version 10.1.0.2.0 - Production on 16-8月 - 
2005 09:36:08 

Copyright (c) 1997, 2003, Oracle.  All rights reserved. 

Used parameter files: 
E:\oracle\product\10.1.0\Db_2\network\admin\sqlnet.ora 

Used TNSNAMES adapter to resolve the alias 
Attempting to contact 
(DESCRIPTION = (ADDRESS_LIST = 
(ADDRESS = (PROTOCOL = TCP) 
(HOST = 127.0.0.1)(PORT = 1521))) 
(CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_ 
NAME = orcl))) 
OK (20 msec)
Copy after login

3.

SQL>;show sga 查看instance是否已经启动

SQL>; select open_mode from v$database; 
查看数据库是打开还是mount状态。 
OPEN_MODE 
---------- 
READ WRITE  


#-----------------
Copy after login

使用hostname访问数据库而不是tnsname的例子

使用tnsname访问数据库是默认的方式,但是也带来点问题,那就是客户端都是需要配置tnsnames.ora文件的。如果你的数据库服务器地址发生改变,就需要重新编辑客户端这个文件。通过hostname访问数据库就没有了这个麻烦。

需要修改

服务器端listener.ora

#监听器的配置文件listener.ora 
#使用host naming则不再需要tnsname.ora文件做本地解析 
# listener.ora Network Configuration File: 
d:\oracle\product\10.1.0\db_1\NETWORK\ADMIN\listener.ora 
# Generated by Oracle configuration tools. 
SID_LIST_LISTENER = 
  (SID_LIST = 
    (SID_DESC = 
  #    (SID_NAME = PLSExtProc) 
(SID_NAME = orcl) 
(GLOBAL_DBNAME = boway) 
      (ORACLE_HOME = d:\oracle\product\10.1.0\db_1) 
  #    (PROGRAM = extproc) 
    ) 
  ) 

LISTENER = 
  (DESCRIPTION_LIST = 
    (DESCRIPTION = 
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC)) 
    ) 
    (DESCRIPTION = 
      (ADDRESS = (PROTOCOL = TCP)
(HOST = boway)(PORT = 1521)) 
    ) 
  )
Copy after login

客户端sqlnet.ora 如果确认不会使用TNSNAME访问的话,可以去掉TNSNAMES。

# sqlnet.ora Network Configuration 
File: d:\oracle\product\10.1.0\db_1\NETWORK\ADMIN\sqlnet.ora 
# Generated by Oracle configuration tools. 

SQLNET.AUTHENTICATION_SERVICES= (NTS) 

NAMES.DIRECTORY_PATH= (HOSTNAME)
Copy after login

Tnsnames.ora文件不需要配置,删除也可以。

下面需要解决的是网络和操作系统的配置问题。(解析我的主机名)

连接示例:

sqlplus sys/oracle@boway
Copy after login

这样,就会连接boway服务器了。(listener来确定你所要连接的service_name)


参考:

在Oracle的网络结构中解决连接问题  谷歌


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
What to do if the oracle can't be opened What to do if the oracle can't be opened Apr 11, 2025 pm 10:06 PM

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

How to solve the problem of closing oracle cursor How to solve the problem of closing oracle cursor Apr 11, 2025 pm 10:18 PM

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

How to create cursors in oracle loop How to create cursors in oracle loop Apr 12, 2025 am 06:18 AM

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

How to export oracle view How to export oracle view Apr 12, 2025 am 06:15 AM

Oracle views can be exported through the EXP utility: Log in to the Oracle database. Start the EXP utility, specifying the view name and export directory. Enter export parameters, including target mode, file format, and tablespace. Start exporting. Verify the export using the impdp utility.

What to do if the oracle log is full What to do if the oracle log is full Apr 12, 2025 am 06:09 AM

When Oracle log files are full, the following solutions can be adopted: 1) Clean old log files; 2) Increase the log file size; 3) Increase the log file group; 4) Set up automatic log management; 5) Reinitialize the database. Before implementing any solution, it is recommended to back up the database to prevent data loss.

What steps are required to configure CentOS in HDFS What steps are required to configure CentOS in HDFS Apr 14, 2025 pm 06:42 PM

Building a Hadoop Distributed File System (HDFS) on a CentOS system requires multiple steps. This article provides a brief configuration guide. 1. Prepare to install JDK in the early stage: Install JavaDevelopmentKit (JDK) on all nodes, and the version must be compatible with Hadoop. The installation package can be downloaded from the Oracle official website. Environment variable configuration: Edit /etc/profile file, set Java and Hadoop environment variables, so that the system can find the installation path of JDK and Hadoop. 2. Security configuration: SSH password-free login to generate SSH key: Use the ssh-keygen command on each node

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

How to stop oracle database How to stop oracle database Apr 12, 2025 am 06:12 AM

To stop an Oracle database, perform the following steps: 1. Connect to the database; 2. Shutdown immediately; 3. Shutdown abort completely.

See all articles