Sub topic 2
Using a TNS alias and tnsnames.ora in SQL Developer
This chapter assumes you know the four parts of an Oracle connection: host, port, service name, and credentials. It also assumes a Java runtime is installed and that you can open a terminal.
Using a TNS alias in SQL Developer
A TNS alias is a short name, such as ORCLPDB, that stands for a full connect descriptor. Oracle client tools look up aliases in a file named tnsnames.ora.
ORCLPDB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = dbhost.example.com)(PORT = 1521))
(CONNECT_DATA = (SERVICE_NAME = orclpdb1.example.com))
)To use the alias in SQL Developer:
- Choose New Connection and set Connection Type to TNS.
- Pick ORCLPDB from the Network Alias list.
- Enter the username and password, then click Test. The status should read Success.
SQL Developer reads the folder set in the TNS_ADMIN environment variable, or the custom folder set in the connection's Advanced options.
Installing and starting SQLcl
SQLcl is a command-line client that runs SQL and PL/SQL. It uses the same connect identifiers as SQL Developer. Download the zip file from Oracle, unzip it, and add its bin folder to your PATH. SQLcl requires Java 11 or later.
Start SQLcl without connecting:
sql /nologConnecting from SQLcl with CONNECT
Connect directly with an alias:
sql hr@ORCLPDBOr connect from inside a session:
SQL> connect hr@ORCLPDB
Password? (**********?) ********
Connected.SQLcl prompts for the password, so it does not appear in your command history.
Verifying who and where you are connected
Run this query after connecting:
select sys_context('USERENV', 'SESSION_USER') as session_user,
sys_context('USERENV', 'SERVICE_NAME') as service_name,
sys_context('USERENV', 'SERVER_HOST') as server_host
from dual;Compare the output with the alias you meant to use. A mismatch means you reached a different database or service.
Common errors
| Error | Usual cause |
|---|---|
| ORA-12154 | Alias not found in the tnsnames.ora the client reads |
| ORA-12514 | Listener does not know the service name |
| ORA-12541 | No listener at the host and port |
| ORA-01017 | Wrong username or password |
Exercise
You run sql hr@ORCLPDB and get ORA-12154, but the entry in tnsnames.ora looks correct. What do you check first?
Show answer
Check that TNS_ADMIN points to the folder that contains the file you edited, and that the alias spelling matches exactly, including case in your tool's lookup.