search
HomeDatabaseMysql Tutorial在Oracle网络结构解决连接问题

在Oracle网络结构解决连接问题

正在看的ORACLE教程是:在Oracle网络结构解决连接问题。最近看到好多人说到tns或者数据库不能登录等问题,就索性总结了下面的文档。
  
  首先来说Oracle的网络结构,往复杂处说能加上加密、LDAP等等。。这里不做讨论,重点放在基本的网络结构也就是我们最常用的这种情况
  
  三个配置文件
  
  listener.ora、sqlnet.ora、tnsnames.ora ,都是放在$ORACLE_HOME\network\admin目录下。
  
  重点:三个文件的作用和使用
  
  #-----------------------
  
  sqlnet.ora-----作用类似于linux或者其他unix的nsswitch.conf文件,通过这个文件来决定怎么样找一个连接中出现的连接字符串,
  
  例如我们客户端输入
  
  sqlplus sys/oracle@orcl
  
  假如我的sqlnet.ora是下面这个样子
  
  SQLNET.AUTHENTICATION_SERVICES= (NTS)
  
  NAMES.DIRECTORY_PATH= (TNSNAMES,HOSTNAME)
  
  那么,客户端就会首先在tnsnames.ora文件中找orcl的记录.如果没有相应的记录则尝试把orcl当作一个主机名,通过网络的途径去解析它的ip地址然后去连接这个ip上GLOBAL_DBNAME=orcl这个实例,当然我这里orcl并不是一个主机名
  
  如果我是这个样子
  
  NAMES.DIRECTORY_PATH= (TNSNAMES)
  
  那么客户端就只会从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 =
  
  #使用专用服务器模式去连接需要跟服务器的模式匹配,如果没有就根据服务器的模式
  
  #自动调节
  
  (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监听器进程的配置文件
  
  关于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.
  
  #下面定义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)
  
  )
  
  )
  
  #监听器的名字,一台数据库可以有不止一个监听器
  
  #再向下面是监听器监听的协议,ip,端口等,这里使用的tcp1521端口,并且使#用的是主机名
  
  LISTENER =
  
  (DESCRIPTION =
  
  (ADDRESS = (PROTOCOL = TCP)(HOST = boway)(PORT = 1521))
  
  )
  
  上面的例子是一个最简单的例子,但也是最普遍的。一个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进程
  

[1]

正在看的ORACLE教程是:在Oracle网络结构解决连接问题。  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
  
  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)
  
  3.
  
  SQL>show sga 查看instance是否已经启动
  
  SQL> select open_mode from v$database; 查看数据库是打开还是mount状态。
  
  OPEN_MODE
  
  ----------
  
  READ WRITE
  
  #-----------------
  
  使用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))
  
  )
  
  )
  
  客户端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)
  
  Tnsnames.ora文件不需要配置,删除也无所谓。
  
  下面就是网络和操作系统的配置问题了,怎么样能够解析我的主机名的问题了
  
  可以通过下面的方式连接
  
  sqlplus sys/oracle@boway
  
  这样的话,会连接boway这台服务器,并且listener来确定你所要连接的service_name。

[2]

Statement
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
How to identify and optimize slow queries in MySQL? (slow query log, performance_schema)How to identify and optimize slow queries in MySQL? (slow query log, performance_schema)Apr 10, 2025 am 09:36 AM

To optimize MySQL slow query, slowquerylog and performance_schema need to be used: 1. Enable slowquerylog and set thresholds to record slow query; 2. Use performance_schema to analyze query execution details, find out performance bottlenecks and optimize.

MySQL and SQL: Essential Skills for DevelopersMySQL and SQL: Essential Skills for DevelopersApr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

Describe MySQL asynchronous master-slave replication process.Describe MySQL asynchronous master-slave replication process.Apr 10, 2025 am 09:30 AM

MySQL asynchronous master-slave replication enables data synchronization through binlog, improving read performance and high availability. 1) The master server record changes to binlog; 2) The slave server reads binlog through I/O threads; 3) The server SQL thread applies binlog to synchronize data.

MySQL: Simple Concepts for Easy LearningMySQL: Simple Concepts for Easy LearningApr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

MySQL: A User-Friendly Introduction to DatabasesMySQL: A User-Friendly Introduction to DatabasesApr 10, 2025 am 09:27 AM

The installation and basic operations of MySQL include: 1. Download and install MySQL, set the root user password; 2. Use SQL commands to create databases and tables, such as CREATEDATABASE and CREATETABLE; 3. Execute CRUD operations, use INSERT, SELECT, UPDATE, DELETE commands; 4. Create indexes and stored procedures to optimize performance and implement complex logic. With these steps, you can build and manage MySQL databases from scratch.

How does the InnoDB Buffer Pool work and why is it crucial for performance?How does the InnoDB Buffer Pool work and why is it crucial for performance?Apr 09, 2025 am 12:12 AM

InnoDBBufferPool improves the performance of MySQL databases by loading data and index pages into memory. 1) The data page is loaded into the BufferPool to reduce disk I/O. 2) Dirty pages are marked and refreshed to disk regularly. 3) LRU algorithm management data page elimination. 4) The read-out mechanism loads the possible data pages in advance.

MySQL: The Ease of Data Management for BeginnersMySQL: The Ease of Data Management for BeginnersApr 09, 2025 am 12:07 AM

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

When might a full table scan be faster than using an index in MySQL?When might a full table scan be faster than using an index in MySQL?Apr 09, 2025 am 12:05 AM

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment