Oracle 上下文中的 TNS:listener 是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2134617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 02:21:27  来源:igfitidea点击:

What is a TNS:listener in the Context of Oracle?

oracleconnection-stringtnsnamestns

提问by Alan Storm

Borderline ServerFault question, but figured I'd try here first since I've had luck with Oracle questions in the past.

Borderline ServerFault 问题,但我想我会先在这里尝试,因为我过去曾遇到过 Oracle 问题。

I'm trying to connect to an oracle database from PHP, and I'm getting the following error.

我正在尝试从 PHP 连接到 oracle 数据库,但出现以下错误。

ORA-12505: TNS:listener does not currently know of SID given in connect descriptor

This is the error that PHP reports, and the error that shows up in Oracle's listener.log.

这是 PHP 报告的错误,也是 Oracle 的 listener.log 中显示的错误。

My immediate problem is fixing this error. The larger question I'd like answered is how does Oracle connection model work?

我的直接问题是修复这个错误。我想回答的更大的问题是 Oracle 连接模型是如何工作的?

This is in a development environment that's running on my local windows machine and has been working up until now. Unfortunately, the environment was handed to me (I didn't set it up) and the people who didset it up are unavailable to help me debug it.

这是在我的本地 Windows 机器上运行的开发环境中,并且一直工作到现在。不幸的是,环境被移交给我(我没有设置它),谁的人没有设置它是不可用的帮我调试。

If I was getting a similar error with MySQL or PostgreSQL (two systems I'm more familiar with), I'd check to ensure that a database process was running, and then attempt to connect manually to the database using the username/password/connection string. Unfortunately, I'm not familiar with the Oracle tools on windows (other than SQL Developer) and I don't know what a TNS:listener or SID are in the context of Oracle (I have vague ideas, but vague ideas rarely help when you're debugging something like this)

如果我在使用 MySQL 或 PostgreSQL(我更熟悉的两个系统)时遇到类似错误,我会检查以确保数据库进程正在运行,然后尝试使用用户名/密码手动连接到数据库/连接字符串。不幸的是,我不熟悉 Windows 上的 Oracle 工具(SQL Developer 除外),我不知道在 Oracle 上下文中 TNS:listener 或 SID 是什么(我有模糊的想法,但模糊的想法很少有帮助时你正在调试这样的东西)

Any general advice would be appreciated.

任何一般性建议将不胜感激。

Updates per Comments:

每条评论的更新:

There's a number of entires in my tnsnames.ora file, the relevant entry being

我的 tnsnames.ora 文件中有许多内容,相关条目是

OBS2 =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = steel-ae39650)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = OBS2)
    )
  )

This is not reflected in the list of instances when I run

当我运行时,这不会反映在实例列表中

    LSNRCTL> services

So I think my next question is, how do I attempt to manually start up the OBS2 instance?

所以我想我的下一个问题是,如何尝试手动启动 OBS2 实例?

采纳答案by Mike Atlas

A TNS name is like an alias to your service instance. The TNS listener service acts as a sort of lookup service for you in this regard. It will fail with that error message if the actual service you're trying to connect to via a TNS name isn't valid.

TNS 名称就像您的服务实例的别名。在这方面,TNS 侦听器服务充当一种查找服务。如果您尝试通过 TNS 名称连接的实际服务无效,它将失败并显示该错误消息。

You can then test out to see if the TNS listener sees the service correctly using the command line tool:

然后,您可以使用命令行工具测试 TNS 侦听器是否正确看到服务:

%>lsnrctl services

Which should output something like the following:

应该输出如下内容:

Service "myservice" has 1 instance(s).
  Instance "myinstance", status READY, has 1 handler(s) for this service...
    Handler(s):
      "D000" established:0 refused:0 current:0 max:1002 state:ready
         DISPATCHER <machine: LOCALHOST, pid: 12345>
         (ADDRESS=(PROTOCOL=tcp)(HOST=LOCALHOST)(PORT=6789))

Can you please post the relevant TNS entry (in the tnsnames.orafile)? It is located in ORAHOME\client or db\ADMIN\NETWORK. If you have both client and server, make sure both copies of the tnsnames.orafile have correct values, just to be safe.

您能否发布相关的 TNS 条目(在tnsnames.ora文件中)?它位于 ORAHOME\client 或 db\ADMIN\NETWORK。如果您同时拥有客户端和服务器,请确保tnsnames.ora文件的两个副本都具有正确的值,以确保安全。

Here's an example of a proper TNS name definition in tnsnames.oracalled 'mydb':

以下是tnsnames.ora名为“mydb”的正确 TNS 名称定义示例:

myDbAlias =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 12345)(QUEUESIZE = 100))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = myservice)
    )
  )

回答by tbone

Just wanted to add to this, as I recently had a similar connection issue that drove me nuts until I figured out what was happening.

只是想补充一下,因为我最近遇到了一个类似的连接问题,这让我发疯,直到我弄清楚发生了什么。

First, the keywords SID and SERVICE_NAME are not exactly the same. This was my first wrong assumption. In many environments you can interchange SID and SERVICE_NAME, but not always, it depends.

首先,关键字 SID 和 SERVICE_NAME 并不完全相同。这是我第一个错误的假设。在许多环境中,您可以互换 SID 和 SERVICE_NAME,但并非总是如此,这取决于。

That said, your error gives away the problem: you're specifying SIDin a connection string instead of the SERVICE_NAMEthat tnsnames successfully uses.

也就是说,您的错误暴露了问题:您在连接字符串中指定了SID,而不是tnsnames 成功使用的SERVICE_NAME

So, if you are specifying the connect string in your code, try using SERVICE_NAME keyword in the connect string (*or, if you've already using SERVICE_NAME and cannot connect, try using SID keyword*).

因此,如果您在代码中指定连接字符串,请尝试在连接字符串中使用 SERVICE_NAME 关键字(*或者,如果您已经使用了 SERVICE_NAME 并且无法连接,请尝试使用 SID 关键字*)。

Overly simplistic answer I know, but easy to try and may save someone some headaches.

我知道答案过于简单,但很容易尝试,可能会让人头疼。

Hope that helps.

希望有帮助。

回答by symcbean

Mike Atlas' answer is fairly comprehensive, but note that you can connect to 10g (or later) DBs which don't have a published tnsname using [//]host_name[:port][/service_name]

Mike Atlas 的回答相当全面,但请注意,您可以使用 [//]host_name[:port][/service_name] 连接到没有发布 tnsname 的 10g(或更高版本)数据库

HTH

HTH

C.

C。