Sub topic 8
Connecting from SQLcl with CONNECT
This chapter covers the CONNECT command in SQLcl. It assumes you know what a host, port, and service name are, and that you have SQLcl installed and can start it from a terminal. Connect identifiers (Easy Connect, TNS alias, JDBC URL) were covered earlier, so here we focus on how SQLcl uses them.
Two ways to start a connection
SQLcl can connect at launch time or after it has started. Both paths end in the same place: a session with one user, on one service, on one database.
The first path passes the connection on the command line. SQLcl connects as soon as it starts, and prompts for the password:
sql hr@//dbhost.example.com:1521/orclpdb1.example.comThe second path starts SQLcl without a connection, then connects from the prompt. The /nolog option tells SQLcl not to attempt a login:
sql /nolog
SQL> connect hr@//dbhost.example.com:1521/orclpdb1.example.com
Password? (**********?) ********
Connected.Prefer the second path on shared machines. A password typed on the command line can appear in shell history and in the process list that other users can see. The CONNECT prompt reads the password without echoing it, and nothing is stored.
The CONNECT syntax
The general form is:
connect [username[/password]]@connect_identifier [as sysdba|sysoper|...]The parts are:
usernameis the database account, such ashr.passwordis optional. Leave it out and SQLcl prompts you for it.connect_identifieris whatever locates the database. SQLcl accepts an Easy Connect string, a TNS alias, or a full connect descriptor.- The optional
asclause requests a privileged role. That topic has its own chapter.
SQLcl also accepts conn as an abbreviation.
Easy Connect
Easy Connect has the form //host:port/service_name. Port defaults to 1521 when omitted, so these two identifiers are equivalent:
connect hr@//dbhost.example.com:1521/orclpdb1.example.com
connect hr@//dbhost.example.com/orclpdb1.example.comThe service name is the part people most often get wrong. Use a service name that the listener actually registers, not the instance name (SID). If you only have a SID, ask the DBA for a service name instead.
TNS alias
A TNS alias is a short name defined in tnsnames.ora. This entry maps the alias ORCLPDB to a host, port, and service name:
ORCLPDB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = dbhost.example.com)(PORT = 1521))
(CONNECT_DATA = (SERVICE_NAME = orclpdb1.example.com))
)With that entry in place you connect with:
connect hr@ORCLPDBSQLcl must be able to find the file. It looks in the directory named by the TNS_ADMIN environment variable first. You can also set it for a single run:
sql -T /nologIf you are unsure which file SQLcl is reading, set TNS_ADMIN explicitly in the shell before starting it. That removes the guesswork when you have several Oracle clients installed.
Passing the password inline
connect hr/MyPassword@ORCLPDB works, but it has the same exposure problems as the command-line form. It also lands in the SQLcl command history. Use it only for throwaway test databases.
Verify the session
A successful Connected. message tells you the login worked. It does not tell you that you reached the database you meant to reach. Run a query that reports the user, service, and server host of the current session:
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;Check all three values against what you intended. This matters when a TNS alias points at a different host from the one you remember, or when the same user exists in both a test and a production database.
SQLcl also has a show command that reports session details:
show user
show connectionshow user prints the current user. show connection prints details of the active connection, including the connect string in use.
Saving connections
Typing a long identifier every time is tedious. The -save option stores the connection under a name in SQLcl's connection store. -savepwd also stores the password:
SQL> connect -save orclpdb_hr -savepwd hr@//dbhost.example.com:1521/orclpdb1.example.com
SQL> connmgr list
SQL> connect -name orclpdb_hrThe steps are:
connect -save name ...connects and stores the connection undername.connmgr listshows every saved connection.connect -name namereconnects using the stored details.
Saved passwords are stored in encrypted form, but treat the connection store as sensitive. Anyone who can read your user profile on that machine may be able to use the saved connection. Leave out -savepwd if you would rather be prompted each time.
Connecting to a cloud database
Autonomous Database requires a wallet, a zip file that holds the connection descriptors and the credentials for TLS. Download it from the cloud console, then point SQLcl at it with set cloudconfig:
SQL> set cloudconfig /path/to/Wallet_MYDB.zip
SQL> connect admin@mydb_highThe names mydb_high, mydb_medium, and mydb_low are service aliases defined inside the wallet. They map to different resource priorities. SQLcl reads them from the zip, so you do not need to unpack it or edit tnsnames.ora.
Running SQLcl in scripts
For automation, run SQLcl without prompts that can block:
sql -S -L hr@orclpdb @report.sql-Sis silent mode. It suppresses the banner and prompts.-Llimits the login to one attempt. Without it, a failed login waits for a new username and password, and a scheduled job hangs.@report.sqlruns the script after connecting.
A script run this way still needs a password source. Do not put it in the script. Better options are a saved connection that SQLcl reads from the connection store, a wallet, or a secret passed in by your scheduler. The script is then safe to commit to version control.
Common connection failures
Each error below points at a different layer. Reading the error code tells you where to look first.
| Error | Meaning | First thing to check |
|---|---|---|
| ORA-12541: TNS:no listener | Nothing is listening at that host and port | Host name, port, whether the listener is running, firewall |
| ORA-12514: listener does not currently know of service requested | The listener answered but does not know that service name | Spelling of the service name; whether the database has registered with the listener |
| ORA-12154: could not resolve the connect identifier specified | SQLcl could not turn the alias into an address | Alias spelling; `TNS_ADMIN` and the location of `tnsnames.ora` |
| ORA-01017: invalid username/password; logon denied | The server was reached but rejected the credentials | Username, password, case, expired or locked account |
Putting it together
A reliable routine for a new connection:
- Start with
sql /nolog. - Connect with Easy Connect first, because it has the fewest moving parts.
- Run the
sys_contextquery and confirm the user, service, and host. - Save the connection with
-saveonce it works. - Switch to a TNS alias or wallet alias only when you need one.
Exercise
A scheduled job runs sql -S hr@orclpdb @report.sql. The password was changed, and the job now runs for hours without finishing. What option is missing, and why does it cause that behavior?
Show answer
The -L option is missing. When the login fails, SQLcl does not exit. It prompts for a new username and password, and an unattended job has nobody to answer, so it waits. With -L, SQLcl makes one login attempt and exits if it fails, so the scheduler sees the failure. The command should be sql -S -L hr@orclpdb @report.sql, with the password supplied from a saved connection, a wallet, or a secret rather than typed in the script.
Exercise
You run connect hr@//dbhost.example.com:1521/orcl and receive ORA-12514. The host and port are correct. What is the likely cause, and how do you fix it?
Show answer
The listener is running and reachable, but it does not have a service registered under the name orcl. Get the correct service name from the DBA, or from the listener's registered services, and use it in the Easy Connect string, for example //dbhost.example.com:1521/orclpdb1.example.com. Using an instance name (SID) where a service name is required is a common cause.