Oracle "ORA-12154: TNS:could not resolve the connect identifier specified": how to fix it
Fix Oracle ORA-12154: the client can't find your connect identifier. Check tnsnames.ora, TNS_ADMIN and sqlnet.ora, an @ in the password, or use Easy Connect.
by OhSudo ··
Not sure this is your cause?Find it step by step: Can't connect to the database →
ORA-12154: TNS:could not resolve the connect identifier specifiedNewer clients word it differently, and tell you more:
ORA-12154: Cannot connect to database. Could not find alias PRODDB in /opt/oracle/network/admin/tnsnames.ora.This is the client’s error. The connection hasn’t left your machine: the client was given a name like PRODDB, and couldn’t work out which server and service it means. So the database, the listener and the firewall aren’t the problem yet. Look at your client’s configuration.
- Clientconnect to PRODDBlooks up the name(fails)
- tnsnames.orathe one this client reads: no PRODDB(breaks here)so
- Database servernever contacted
TL;DR
tnsping PRODDB # which files it reads, and what it findsecho $TNS_ADMIN # the folder that should hold tnsnames.orasqlplus app@//db01.example.com:1521/PROD # Easy Connect: no tnsnames.ora neededIf that last one connects, the database is fine and your problem is in tnsnames.ora, or in where the client looks for it.
1. Skip the file: try Easy Connect
Name the server and service directly. It’s the quickest test:
sqlplus app@//db01.example.com:1521/PRODThe format is //host:port/service_name. If it works, keep reading to fix the alias, or just use this form in your application. Note that PROD is the service name, not necessarily the SID.
A different error here means you got past ORA-12154, and the rest of the path is what’s wrong (see the end of this article).
2. Which tnsnames.ora does the client read?
tnsping PRODDBUsed parameter files:/opt/oracle/network/admin/sqlnet.ora
TNS-03505: Failed to resolve nameThe client looks for tnsnames.ora in this order:
- the folder in the
TNS_ADMINenvironment variable; network/adminin the Oracle home;- and for Instant Client, its own
network/adminsubfolder.
So the file you’re editing may simply not be the one being read. Set it explicitly:
export TNS_ADMIN=/opt/oracle/network/adminOn Windows, set it in System Properties → Environment Variables, and restart the application. tnsping comes with the full client; with Instant Client you may not have it, and Easy Connect from section 1 is the quickest test.
3. Is the entry right?
A correct entry:
PRODDB = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = db01.example.com)(PORT = 1521)) (CONNECT_DATA = (SERVICE_NAME = PROD) ) )Things that break it:
- The alias differs from what you typed.
PRODDBin the file andPROD_DBin the connection string don’t match. - A domain is added. With
NAMES.DEFAULT_DOMAIN = example.cominsqlnet.ora, the client looks forPRODDB.example.com. Remove that line, or add the domain to the alias. - Unbalanced brackets, or a stray character from a copy and paste. One missing
)can invalidate the entries after it. - The alias doesn’t start at the beginning of the line. Leading spaces can make the parser treat it as a continuation of the previous entry.
- Wrong encoding or a wrong line ending from an editor, more rarely.
4. sqlnet.ora may turn the file off
sqlnet.ora decides where names are looked up:
NAMES.DIRECTORY_PATH = (TNSNAMES, EZCONNECT)If TNSNAMES isn’t in that list, your file is ignored. And if EZCONNECT is missing, the //host:port/service form fails with this same error.
5. An @ in the password
A classic one. The client splits the connection string at @:
sqlplus app/P@ssw0rd@PRODDB # the client reads "ssw0rd@PRODDB" as the identifier: ORA-12154Quote the password, and let your shell pass the quotes through:
sqlplus 'app/"P@ssw0rd"@PRODDB'Better still, leave the password out and let the tool ask for it: sqlplus app@PRODDB. It also won’t end up in your shell history. In a JDBC or ODBC URL, the password goes in its own field.
6. On Windows: several Oracle homes, 32 and 64 bits
A machine with more than one Oracle client may use a different one from what you think. where sqlplus shows which is first in PATH. A 32-bit application needs the 32-bit client, with its own tnsnames.ora, even on 64-bit Windows; this catches a lot of Excel, Power BI and older tools. Setting TNS_ADMIN makes them all read the same file.
Shell, we have a problem!
Similar, but different: these come after the name was resolved, so the problem has moved. ORA-12541: no listener: nothing listens on that host and port, so check lsnrctl status, and the Port Lookup entry for 1521. ORA-12514: listener does not currently know of service: the listener is up, and the service name is wrong or not registered. ORA-12170: connect timeout: a firewall, see “Connection timed out” vs “Connection refused”. ORA-01017: invalid username/password: you reached the database. Once you’re connected, a missing table is ORA-00942. If the alias resolves and nothing answers, see ORA-12541.
Found this useful? Share it.
Found a mistake? Let us know at contact@ohsudo.com.