oracle 数据库网络连接 配置文件Tnsnames.ora

ぐ巨炮叔叔 提交于 2020-04-25 08:14:58

TNSNAMES.ORA is a SQL*Net configuration file that defines databases addresses for establishing connections to them. This file normally resides in the ORACLE HOME\NETWORK\ADMIN directory.


Configuring TNSNAMES.ora

Add the following entry in your TNSNAMES.ORA file and change the values shown in brackets to suit your environment:

<addressname> =
 (DESCRIPTION =
   (ADDRESS_LIST =
     (ADDRESS = (PROTOCOL = TCP)(Host = <hostname>)(Port = <port>))
   )
 (CONNECT_DATA =
   (SERVICE_NAME = <service_name>)
 )
)

Here is a completed example:

ORA11 =
 (DESCRIPTION = 
   (ADDRESS_LIST =
     (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))
   )
 (CONNECT_DATA =
   (SERVICE_NAME = ORA11)
 )
)

The TNSNAMES.ORA files are located on both client and server systems.

tnsnames.ora 配置文件在客户端和服务端都有。

If you make configuration changes on the server ($ORACLE_HOME/network/admin/tnsnames.ora) ensure you can connect to the database through the listener if you are logged on to the server. If you make configuration changes on the client (c:\oracle\product\10.2.0\db_1\network\admin\tnsnames.ora) ensure you can connect from your client workstation to the database through the listener running on the server.

SQL*Net Easy Configurator

One can also use the SQL*Net Easy Configurator GUI (which is installed when you install the Oracle Client Software) to change TNSNAMES.ORA entries. Most people prefer to edit the TNSNAMES.ORA file with an editor and plug in the required settings. However, others might prefer the GUI. Note the file is usually found in the ORACLE HOME/network/admin/ directory.

SQL*Net Easy Configurator will guide you through a set of screens (wizard) to add as many entries as you like for each database you need to connect to. Also supply an alias name as prompted to identify each database (the alias name and database name can be different).


tnsnames.ora SID VS SERVICE_NAME区别

Hello Tom, 

We use tnsnames.ora . 

1) What is the difference between SID entry and SERVICE_NAME in the tnsnames.ora 

2) When I put SID the connection is successful, when I put SERVICE_NAME it says tns could not resolve service name 

So This works 
MYDB.world = 
(DESCRIPTION = 
(ADDRESS_LIST = 
(ADDRESS = (PROTOCOL = TCP)(HOST = MYHOST.COM)(PORT = 1521)) 

(CONNECT_DATA = 
(SID = MYDB) 
(SERVER = DEDICATED) 




But this doesnt 

MYDB.world = 
(DESCRIPTION = 
(ADDRESS_LIST = 
(ADDRESS = (PROTOCOL = TCP)(HOST = MYHOST.COM)(PORT = 1521)) 

(CONNECT_DATA = 
(SERVICE_NAME = MYDB) 
(SERVER = DEDICATED) 


and we said...

A service name is more flexible than a SID would be. 


A database can dynamically register with a listener using one or more service names. In fact, more than one database can register with a listener using the same service name (think about a clustered environment where you have multiple instances that all are the same database under the covers). 

A database on the other hand has a single SID. And a single SID goes to a single database. It is a pure 1:1 relationship. 

A service is a many to many relationship. 


Service names are used with dynamic registration - the data registers with the listener after it starts up. Once it does that, you can connect. 


With the SID - that is more like telling the listener "I want you to connect to this specific database, I know the 'address', here you go" 

With the SERVICE - you are asking the listener to put you in touch with a database that can service your request, a database that registers using that service. 

总结一下,就是SID代表唯一的目标数据库实例,但是service_name 代表数据库提供的连接服务,可以是多个数据库提供同一个连接服务。

tnsnames.ora VS listener.ora区别


That you cannot connect using the service name MYDB means your database is not registering with that listener (see the Net Admin guide for dynamic registration) or if it is registered with that listener (lsnrctl services is a command you can use to see the registered services) then it is registering with a different name. 


Oracle acts as a Client-Server software. That means you have two main ends, the Client who must somehow get to the server, and the server who must accept connexion requests from clients.

When the client attempts to connect to the server, you give him a "Service". The service is mainly 4 info: Host for the server, Protocol and port (the language to speak) ad SID (name of the instance/database). Those 4 infos are usually and by default locates in a tnsnames.ora. This is called: Translation Named Server - errrrrr Service - errrr Ser* :)

On the server side you must have one or more active processes (daemons) who are waiting for the client to attempt a connection. Those services are called Listeners. In most cases only one listener is required. This listener is configured via a listener.ora file. This file includes the process config, mainly: Host for which it listens, protocol and port, list of SID for which the process can establish a connexion.

Furthermore, if you're connecting to the database on the server computer, and it's the default database, you don't need to tell where you're going when you're the client. Default Database. You don't even need to have a listener.

Please review the Net Service administrator's Guide (http://www.oracle.com/pls/db92/db92.to_pdf?pathname=network.920%2Fa96580.pdf&remark=docindex) for more info.

Hope this helps.

Yoann

总结一下,觉得 tnsnames.ora 是用于客户端接Oracleserver用的,而listener.ora则用于Oracle监听等待各客户端连接的配置文件。

这一切都与Oracle是一个client/server软件结构有关。



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!