Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a Database Link where remote and local are the same server

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.

like image 837
Brian G Avatar asked Jun 02 '10 18:06

Brian G


People also ask

What is shared database link?

Shared database links enable you to limit the number of network connections required between the local server and the remote server.

What is a global database link?

A database link connection allows local users to access data on a remote database. For this connection to occur, each database in the distributed system must have a unique global database name in the network domain. The global database name uniquely identifies a database server in a distributed system.


3 Answers

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.

like image 105
DCookie Avatar answered Oct 13 '22 22:10

DCookie


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

like image 31
Gary Myers Avatar answered Oct 13 '22 22:10

Gary Myers


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"

like image 22
dpbradley Avatar answered Oct 13 '22 22:10

dpbradley