November 8, 2006

Oracle TNS connection with tnsnames.ora. Examples

Installed Oracle XE or 10g? Want to to connect but get error messages like;
ORA-12154: TNS:could not resolve the connect identifier specified?
Try checking your tnsnames.ora file. A sample tnsnames.ora file should be provided when you installed
the Oracle databas server. Can't find it? Here is a sample from the XE installation.

# tnsnames.ora Network Configuration File:


XE =
(DESCRIPTION =
(ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST =192.168.XX.XX)(PORT = 1521)))
(CONNECT_DATA =
(SID = XE)
)
)

EXTPROC_CONNECTION_DATA =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC_FOR_XE))
)
(CONNECT_DATA =
(SID = PLSExtProc)
(PRESENTATION = RO)
)
)


Port 1521/tcp is one of Oracle default ports for the TNS listener. TNS stands for Transparent Network Substrate. The TNS listener is responsible for managing network connections to the Oracle database.


The next step is to switch to your oracle user.
# su - oracle

From the prompt, you could now try and connect to your database with the SQLPLUS tool.
If you have default installation of Oracle XE 10g, try to log in with the hr account.

$ ./sqlplus

If you get the error message ORA-12162: TNS:net service name is incorrectly specified,
you have forgotten to specify Oracle's SID. You will need to provide the SID to sqlplus to be able to connect properly. The SID in this example is XE.

$ ./sqlplus hr/hr@XE

or

$ ./sqlplus /NOLOG


SQL*Plus: Release 10.2.0.1.0 - Production on Wed Nov 8 13:56:00 2006

Copyright (c) 1982, 2005, Oracle. All rights reserved.

SQL> connect hr@XE
Enter password:


Connected to:
Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production

Make sure you have the tnsnames.ora file in your path. Under /etc for example.
/etc/tnsnames.ora

If you are unsure where sqlplus looks for you tnsnames .ora file. Try running the strace command with the trace option, and log it to a file for analysis.

$ strace -ft ./sqlplus > /tmp/sqlplus_strace

Ok, happy Oracle:ing.