如何创建远程和本地是同一服务器的数据库链接

发布于 2024-09-04 03:44:31 字数 168 浏览 9 评论 0原文

我需要创建一个数据库链接,该链接是指向同一服务器上的架构的链接。是否有一个特殊的关键字可以用于此目的? (如 local 或 localhost )

我不完全确定 tnsnames 中服务器的名称是什么,这可能是我的问题的一部分。

这是针对复杂的情况,其中涉及需要数据库链接才能继续的脚本。

I have a need to create a database link that is a link to a schema on the same server. Is there a special keyword to use for this? ( like local or localhost )

I am not entirely sure what the name of the server is in tnsnames and that may be part of my problem.

This is for a complicated situation which involves a script that needs a database link to continue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寂寞美少年 2024-09-11 03:44:31

DB 链接机制通过 TNS,因此只需为本地数据库定义一个 TNS 条目并在链接中使用它。

您的客户端 TNSNAMES.ORA 文件应包含一个类似于以下内容的条目:

YourDBAlias =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = yourHOST)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = YourDB)
    )
  )

确保该条目也存在于数据库服务器上的 TNSNAMES.ORA 文件中。

然后,创建数据库链接如下:

CREATE [PUBLIC] DATABASE LINK yourLinkName
       CONNECT TO theSchema IDENTIFIED BY thePW
       USING 'YourDBAlias';

这应该可以解决问题(假设您使用的是 TNS 命名)。

此外,如果您不确定数据库的 TNS 名称,在 SQL*Plus 中您可以发出以下命令:

show parameter service_names

这将显示数据库在其侦听器中注册的名称。您应该能够从中找到 TNSNAMES.ORA 文件中的相应条目。

The DB link mechanism goes through TNS, so just define a TNS entry for your local database and use that in your link.

Your client TNSNAMES.ORA files should contain an entry that looks something like:

YourDBAlias =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = yourHOST)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = YourDB)
    )
  )

Make sure this entry also exists in the TNSNAMES.ORA file on your database server.

Then, create the database link as:

CREATE [PUBLIC] DATABASE LINK yourLinkName
       CONNECT TO theSchema IDENTIFIED BY thePW
       USING 'YourDBAlias';

This should do the trick (assuming you're using TNS naming).

Additionally, if you're not sure what your TNS Name is for the database, in SQL*Plus you can issue the command:

show parameter service_names

This will show you the name(s) that the database is registered with it's listener as. You should be able to find the corresponding entry in the TNSNAMES.ORA file from that.

自演自醉 2024-09-11 03:44:31

如果您无法修改TNSNAMES.ORA,您可以使用简单连接语法,甚至适用于数据库链接。
假设侦听器位于默认端口上,则以下 SQL 将获取连接字符串

select utl_inaddr.get_host_address||':1521/'||sys_context('USERENV','INSTANCE_NAME') from Dual

If you can't amend TNSNAMES.ORA you can use the Easy Connect syntax even for DB Links.
Assuming the listener is on the default port, then the following SQL will get the conneection string

select utl_inaddr.get_host_address||':1521/'||sys_context('USERENV','INSTANCE_NAME') from dual

z祗昰~ 2024-09-11 03:44:31

如果“同一服务器上的架构”指的是同一数据库中的架构,则可能会遇到问题。 (例如,如果脚本将架构之外的任何内容视为外部数据库以扁平化后续 SQL 操作)。

Oracle 处理环回链接的方式有些不同,如果使用链接的数据库全局名称,您可能会收到 ORA-02082 错误(“环回数据库链接必须有连接限定符”)。如果收到此错误,则必须将链接命名为不同的名称,例如“loopback”,但这也要求将 global_names 数据库参数设置为 false。否则您将收到“ORA-02085:数据库链接 someName 连接到 someOtherName”

You may have a problem if by "schema on the same server" you mean a schema in the same database. (For example, if the script was treating anything outside of the schema as an external database to flatten subsequent SQL operations).

Oracle treats loopback links somewhat differently, and you may receive ORA-02082 errors ("a loopback database link must have a connection qualifier") if using the database global name for the link. If you receive this error, you have to name the link something different, like "loopback", but this also requires that the global_names database parameter is set to false. Otherwise you'll receive "ORA-02085: database link someName connects to someOtherName"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文