I always prefer to use TNSNAMES.ORA to connect to my database server. It is one of the most convenient ways for database servers with a fixed hostname or IP address. For example, if TNSNAMES.ORA has the following entry:

ORCL =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = geek.mydomain.co.in)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl)
    )
  )

TNS looks into “TNSNAMES.ORA” file to find out the meaning of “orcl” and connects Oracle Database Server Machine “geek.mydomain.co.in” on port 1521 “orcl” service when we fire following command like:

C:Usersnimish.garg>sqlplus scott/[email protected]
SQL*Plus: Release 11.2.0.3.0 Production on Tue Sep 30 14:24:03 2014
Copyright (c) 1982, 2011, Oracle.  All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL>

There are times when we do not want to modify our TNSNAMES.ORA maybe because we might want to connect a particular database service only for one time. So what are the other ways possible to Connect to Oracle Database without using TNSNAMES.ORA? Well, there are two ways for this:

EZConnect

EZConnect is Oracle’s easy connect naming method. EZConnect eliminates the need for service name lookups in TNSNAMES.ORA files when connecting to an Oracle database across a TCP/IP network.

Syntax of EZConnect:

sqlplus username/[email protected][//]host[:port][/service_name]

or

CONNECT username/[email protected][//]host[:port][/service_name]

Any of these forms would work too. Note that the password is supplied on the same line.

SQLPLUS username/[email protected]//hostname:PORT/service_name.example.com
SQLPLUS username/[email protected]//hostname/sales.us.example.com   

Example:

C:Usersnimish.garg>sqlplus scott/[email protected]:1521/orcl
SQL*Plus: Release 11.2.0.3.0 Production on Tue Sep 30 14:36:00 2014
Copyright (c) 1982, 2011, Oracle.  All rights reserved.

Connected to:
Oracle Database 11g Release 11.2.0.3.0 - 64bit Production

SQL>

To enable EZConnect “sqlnet.ora” should have naming methods “EZConnect” specified in NAMES.DIRECTORY_PATH Parameter. “sqlnet.ora” is located in $ORACLE_HOME/network/admin.

Example:

NAMES.DIRECTORY_PATH=(EZConnect, tnsnames)

TNS Connect String

The TNS Connect String also known as Connect Descriptor is a type of connect identifier. It defines the parameters that need the Oracle Net Service to connect to a database service.

Syntax of TNS Connect String:

sqlplus "username/[email protected](DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=hostname)(PORT=port))(CONNECT_DATA=(SERVER=dedicated)(SERVICE_NAME=servicename)))"

In the above syntax lower case letters must be replaced with the actual values. Please remember there should not be any space in the connect string, also no carriage return, and in UNIX put the single quote instead of a double quote.

Example:

C:Usersnimish.garg>sqlplus "scott/[email protected](DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=geek.mydomain.co.in)(PORT=1521))(CONNECT_DATA=(SERVER=dedicated)(SERVICE_NAME=orcl)))"

SQL*Plus: Release 11.2.0.3.0 Production on Tue Sep 30 15:05:13 2014
Copyright (c) 1982, 2011, Oracle.  All rights reserved.

Connected to:
Oracle Database 11g Release 11.2.0.3.0 - 64bit Production

SQL>

Troubleshooting

EZCONNECT expects the password to be supplied on the same line. If it is not entered, the EZCONNECT string is not parsed correctly.

SQLPLUS [email protected]//hostname:1521/service_name.example.com
password:

This will fail with ORA-12504. It is necessary to use escape \ surrounding the EZCONNECT string and the use of double quotes:

Either use:

SQLPLUS username/[email protected]//hostname:PORT/service_name.example.com

Or to enter the password on the subsequent line use:

SQLPLUS [email protected]"[email protected]:PORT/service name"
password: